0

Yes, yet another url shortener written in java, because I wanted my own, and because why not. Currently everything works, just not they way I want to to. In short, there is only one servlet mapped to "/" in the entire project. There are no frameworks involved, or anything fancy, this is just a basic Servlet "project". on doPost a new shortUrl is created, and you get a JSON response. On doGet, if the URL is "/*{any_valid_short_url}" then a redirect is sent (below).

response.sendRedirect("longUrlString")

The issue I am having is with the index page, when the same doGet is called, I check the requested path, if it is "/" then currently, I use a FileInputStream and stream the index.html page out via response.getOutputStream(), which is pretty hacky in my opinion. I would like to use a requestDispatcher instead, however when I do try to implement that (below) I get into a re-direct loop, and the servlet container (jetty or tomcat) stack overflow's.

getServletContext().getRequestDispatcher("/index.html").forward(req, res);

Is there something that I am mis-understanding about how this is being done? The project is currently hosted on my github page. https://github.com/justinmburrous/ShortUrl

  • What is getServletContext() returning? You should be using `req.getRequestDispatchter(...)` – Sotirios Delimanolis Mar 04 '13 at 19:32
  • Oops... but still the same issue, stack overflow, caused by a massive amount of re-directs. –  Mar 04 '13 at 21:47
  • Please, post the code where you get the RequestDispatcher. – Sotirios Delimanolis Mar 04 '13 at 21:54
  • Sure, so I have updated and commented what is working (but hacky), and the new request dispatcher attempt. I have tried multiple variations of index.html /index.html, and checked it's location. I have also tried welcome file, however with a catch all servlet like this that has issue. https://github.com/justinmburrous/ShortUrl/blob/request_dispatcher/src/com/jmb/shorturl/servlets/RedirectorServlet.java –  Mar 04 '13 at 22:48

2 Answers2

0

You need to make this check more narrower, because for all requests this condition is true and hence for all requests are forwarded to 'index.html' again and again

    if(requestedPath.equals("/")){

    //tried with multiple variations of /index.html, renamed to jsp, etc...
RequestDispatcher dispatcher = request.getRequestDispatcher("index.html");
dispatcher.forward(request, response);    
    }

Edit: Since your servlet is mapped as default servlet , all forwards or request will be handled by this Servlet. You should map the url to more specific range.

RequestDispatcher - forward - produces infinite loop

Community
  • 1
  • 1
Sudhakar
  • 4,823
  • 2
  • 35
  • 42
  • I am not sure I understand what you are saying. Currently (this is on the master branch without the RequestDispatcher) if you request http://jburr.co/ you get the index page (via the hacky output stream), however if you request jburr.co/c you get cnn.com, a test page I just added. So the if() that checks the requested path is working as it should. –  Mar 05 '13 at 19:13
0

Okay, I have figured this one out with thanks to How to access static resources when mapping a global front controller servlet on /*

My github page has the working code along with the Filter, modified servlet and web.xml for.

Community
  • 1
  • 1