• Post Reply Bookmark Topic Watch Topic
  • New Topic

Encoding problem in requestDispatcher()...- Help Required.  RSS feed

 
Suresh Selvaraj
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'd like to know if there is any way to Encode a URL using the requestDispathcher().
Here is the code.
1.
getServletContext().getRequestDispatcher("/positioning.jsp#3472").forward(req,res);
I get Null Pointer Exception.
But, it works FINE when I use, res.encodeRedirectUrl(String url);
Code:
res.sendRedirect(res.encodeRedirectUrl("/positioning.jsp#3472"));

Per our project Standards, I need to use only requestDispatcher().
I even tried encoding the "#3472" and appending it to the URL. Still it doesn't work.
Code 1:
String encodedUrl=URLEncoder.encode("/positioning.jsp?#3472");
getServletContext().getRequestDispatcher(encodedUrl).forward(req,res);

Any suggestions?
Thanks,
S.Suresh
 
Steve Granton
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The null pointer exception is likely to be being caused by the fact that you do not have a resource called 'positioning.jsp#3472' - your resource is 'positioning.jsp'. The contain fails to find the resource and a null pointer is thrown when it tries to action the forward(). The parameter for the getRequestDispatcher is a resource and not an encoded url.
I think you need to ask yourself the question why do I want to encode the url? The encodeUrl() is used to append the SessionID onto the end of urls that are produced by the application. This SessionID can be used to track the user's session if the client browser doesn't accept cookies.
Since the request dispatcher is used to forward a request to another 'resource' WITHIN the application then the next resource will also have access to the client session through the Request object. To the client this is treated as one request no matter how many times you forward before producing a response.
If you use response.sendRedirect(url) then it is wise to encode the url as the mechanics are as follows:
The method sends a temporary redirect response back to the client - HTTP response code 302. If the url if relative then it is converted to an absolute url.
The client then makes a new request for the url. This results in an entirely new request being serviced by the web app and consequently the web app no longer has access to the client session. It uses the SessionID (or cookie if enabled) to try to map the client request to an existing session.
I would advice against using sendRedirect() except where you mean to indicate to the client that a resource has moved as it results in increased network traffic.
I hope this helps,
Cheers,
Steve
[ April 16, 2002: Message edited by: Steve Granton ]
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The presence of the anchor (#) is a problem for either the container (but perhaps it shouldn't be) or the browser.

Here is a solution I figured out for someone with the same problem.

http://www.coderanch.com/t/354228/Servlets/java/request-forwarding
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!