This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Problem with RequestDispatcher and relative links Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem with RequestDispatcher and relative links" Watch "Problem with RequestDispatcher and relative links" New topic
Author

Problem with RequestDispatcher and relative links

M Burke
Ranch Hand

Joined: Jun 25, 2004
Posts: 388
This is what is happening...

I have a jsp (index.jsp) that has a link to a servlet. Click on the link and the servlet runs, then it sends the request back to the same jsp.
But after it comes back, the link url has changed.

So it changes from this...
//localhost:8080/DSWeb/servlet/UserLanguage

To this...
//localhost:8080/DSWeb/servlet/servlet/UserLanguage

Which is invalid. Any ideas?

The link looks like this in code...
href="servlet/UserLanguage"

And the servlet forward looks like this...
request.getRequestDispatcher("\index.jsp").forward(request, response);

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

What is the URL Mapping of the Servlet in the web.xml? You need to be using the value of the URL mapping in your JSP.


Mohamed Sanaulla | My Blog
Gaurav Sagar
Ranch Hand

Joined: Sep 08, 2010
Posts: 97

M Burke wrote:This is what is happening...


On clicking the link in your jsp, you should get directed to http://localhost:8080/DSWeb/servlet/UserLanguage (provided your index.jsp is in the DSWeb directory directory and not any of its sub-directories).
If all is right till now, then your request should be handled by the specified servlet "UserLanguage", which would forward your request to "index.jsp" with the url as: http://localhost:8080/DSWeb/servlet/index.jsp (since you are using a relative url).
This now is the root of your problem, since there is no index.jsp present there.
Had you specified a forward slash in your request-dispatcher like
it will forward to the valid index.jsp

Note: I am taking the deployment path in your web.xml of your servlet as "/servlet/UserLanguage".

Regards,
Gaurav
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

You should never use URL-relative URLs. They are fragile and will almost always leads to problems. Any URL starting with .. is a red flag for a problem waiting to happen.

Internal URLs should start with a / that represents the document root. External URLs (any that will be triggered by the browser) should start with the context path.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
M Burke
Ranch Hand

Joined: Jun 25, 2004
Posts: 388
Yes, I did use the / before index.jsp in the RequestDispatcher. When I first load the page with DSWeb/index.jsp it works fine. It's just when the page is redirected back to index.jsp from the servlet I have problems.

I tried something and it worked.

I changed this...
<servlet-mapping>
<servlet-name>UserLanguage</servlet-name>
<url-pattern>/servlet/UserLanguage</url-pattern>
</servlet-mapping>
<servlet-mapping>

to this...
<servlet-mapping>
<servlet-name>UserLanguage</servlet-name>
<url-pattern>/UserLanguage</url-pattern>
</servlet-mapping>
<servlet-mapping>

I am not sure why it works, but it does. If anyone can explain pathing and how I can make my paths full-proof, I would appreciate it.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

Using the artificial /servlet prefix is unnecessary and rather silly (a holdover from Invoker days no doubt).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with RequestDispatcher and relative links
 
Similar Threads
Filter not called
Localhost leads to nowhere
Servlet Mapping
getRequestDispatcher forwarding across applications
How to package a project and install in tomcat webapps?