This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sending response code to original source request after re-directs/re-submits

 
ravi D shankar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

My scenario is that I have a request which is sent in a specific url format to a servlet which downloads a pdf file and streams it back to the browser.
In the servlet, if the url format is different from what is expected but contains all params(a legacy url format bookmarked) then we forward it to a jsp and re-submit to the servlet in the required url format.
This was done because when a pdf opens in the browser, adobe reader couldnt save it using its file name so we have to change the url format so that it takes the name before "?param string" from the new url visible in address bar (forwarding doesn't change url).
So its like request->servlet->jsp->servlet-> servlet streams file to browser.
Since I re-submit in a specific url format which has to be visible in browser address bar, the original request pointing to the source is lost.
Now, if the file to download doesn't exist, we need to set the response code as 404 but that response code doesn't reach the original source which requested it.
If the original source is a standalone class opening a HTTPUrlConnection , it will receive empty headers and responsecode as -1 if the file doesn't exist.

So the requirement is to ensure that we should be able to change the url in address bar and also send the 404 response code if the file doesn't exist to the original source which requested it.

Any ideas?

Thanks
 
Timothy Schmelter
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ravi,

Instead of using a forward in your JSP, use a redirect implemented in JSP scriptlet code or, even better, catching that condition in your servlet before it gets to your JSP. Either way you do it, though, you are able to then properly respond with a 404 to the properly-formatted bad request.

Scriptlet or servlet, the code differs mainly in how you obtain a handle to the HttpServletResponse. It is automatically included as part of the JSP context. In a servlet, you declare it as part of your servlet's doGet(HttpServletRequest request, HttpServletResponse response) method.

Servlet example


Implementing the redirect in the servlet also saves you a round trip to the intermediary JSP:


--Tim
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic