aspose file tools*
The moose likes Servlets and the fly likes problem with RequestDispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "problem with RequestDispatcher" Watch "problem with RequestDispatcher" New topic
Author

problem with RequestDispatcher

Viv Singh
Ranch Hand

Joined: Nov 08, 2008
Posts: 73
Hi,

I have a problem with the RequestDispatcher:

I have a page: http://localhost:8080/tool/portal/page1.jsp

In this page I have a form which gets posted to a servlet called serv1

This serv1 does some operations and executes the following code:



The problem now is that I get redirected to

http://localhost:8080/supporttool/serv1

rather then to:

http://localhost:8080/supporttool/portal/serv1

What am I doing wrong here?
Mohamed Inayath
Ranch Hand

Joined: Nov 22, 2004
Posts: 124
Try this :



This is will redirect to page.jsp.
Viv Singh
Ranch Hand

Joined: Nov 08, 2008
Posts: 73
No, that does not work either. Tried that already.

For some strange reason this works:


However, i want to use the requestdispatcher as i dont want to send the status as part of the url.
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
I dont understand your code. Why is it re dispatching/forwarding to the same page?


Cheers!
RSR
Viv Singh
Ranch Hand

Joined: Nov 08, 2008
Posts: 73
Well, on that page there is a HTML form which the user fills out etc. After the user submits it he should get redirected to the same page with now showing a message "your request has successfully been updated".

The user might enter new details and submit the form again etc.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

try this RequestDispatcher view = req.getRequestDispatcher(request.getContextPath()+"/portal/page1.jsp");
Viv Singh
Ranch Hand

Joined: Nov 08, 2008
Posts: 73
Hi,

I have tried this as well, if I use this approach I get the following:

HTTP Status 404 - /tool/portal/page1.jsp

type Status report

message /tool/portal/page1.jsp

description The requested resource (/tool/portal/page1.jsp) is not available.


Although getContextPath() returns "/tool".

I am confused
Vinoth Thirunavukarasu
Ranch Hand

Joined: Dec 18, 2008
Posts: 164

Hi,
I think the bellow example will help you
webpage


Java Best Practices
Linux Best Practices
Amortization Calculator

Viv Singh
Ranch Hand

Joined: Nov 08, 2008
Posts: 73
Thanks, but this is what I am already doing. The problem is with the URL, I get redirected to a "wrong path".
Vinoth Thirunavukarasu
Ranch Hand

Joined: Dec 18, 2008
Posts: 164

Instead of giving relative path specify with a full path.
Viv Singh
Ranch Hand

Joined: Nov 08, 2008
Posts: 73
I have tried the following:



And I get the following error message:

"
HTTP Status 404 - /http:/localhost:8080/tool/portal/page1.jsp

type Status report

message /http:/localhost:8080/tool/portal/page1.jsp

description The requested resource (/http:/localhost:8080/tool/portal/page1.jsp) is not available.
Apache Tomcat/5.0.30
"

And I have also tried this:



And I get this error message:

"
HTTP Status 404 - /tool/portal/page1.jsp

type Status report

message /tool/portal/page1.jsp

description The requested resource (/tool/portal/page1.jsp) is not available.
Apache Tomcat/5.0.30
"
Duc Vo
Ranch Hand

Joined: Nov 20, 2008
Posts: 254
Viv Singh wrote:Hi,

I have a problem with the RequestDispatcher:

I have a page: http://localhost:8080/tool/portal/page1.jsp

In this page I have a form which gets posted to a servlet called serv1

This serv1 does some operations and executes the following code:



The problem now is that I get redirected to

http://localhost:8080/supporttool/serv1

rather then to:

http://localhost:8080/supporttool/portal/serv1

What am I doing wrong here?

Viv, that is what supposed to happen. You did a server forward not a client redirect hence you will have the content of "portal/page1.jsp" at the original servlet uri at serv1. It is just basic HTML, you'll need to tell the browser to redirect to the intended page using response object as what you did in the other post. So if you want the browser to redirect, use HttpServletResponse.sendRedirect().

The major different between RequestDispatcher.forward() and HttpServletResponse.sendRedirect() is

RequestDispatcher.forward() - forward to request to other resource and it's required that the response has not been committed when the method is triggered -- meaning the server execute the forwarded resource and response to the client, this process is transparent from the browser. So as far as the browser concern, it has the response from the server from the requested uri "/serv1" though the content is executed from /portal/page1.jsp

HttpServletResponse.sendRedirect() - added redirect status code and the new URL to the response header -- when the browser received this response, it will redirect the page to the defined URL.

In short, if you want redirect, it must happen at the browser --> must be from response object.


“Everything should be as simple as it is, but not simpler.” Albert Einstein
Viv Singh
Ranch Hand

Joined: Nov 08, 2008
Posts: 73
Thanks for you reply. Could you please give me an example of how to implement that. The thing is that I want to send a message back to the client which should be displayed in the HTML file.

I tried the following and therefore used RequestDispatch initially:



I dont want to send the message as part of the url as posted before.

Thanks in advance.
Duc Vo
Ranch Hand

Joined: Nov 20, 2008
Posts: 254
Viv Singh wrote:
I dont want to send the message as part of the url as posted before.

You have to use response.sendRedirect() to achieve that, now to pass the message back from servlet, you'll have to use query string on the redirecting uri.
Anyway, why don't you want the client to see the servlet uri?
Viv Singh
Ranch Hand

Joined: Nov 08, 2008
Posts: 73
Duc Vo wrote:
Viv Singh wrote:
I dont want to send the message as part of the url as posted before.

You have to use response.sendRedirect() to achieve that, now to pass the message back from servlet, you'll have to use query string on the redirecting uri.
Anyway, why don't you want the client to see the servlet uri?


I don't want to pass the error message e.g. "Your request could not be processed" in the URL like page1.jsp?message=Your%20request........
Viv Singh
Ranch Hand

Joined: Nov 08, 2008
Posts: 73
Is there any other way of solving the problem of not sending the error message string as part of the url?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

Duc Vo wrote:You have to use response.sendRedirect() to achieve that

Hogwash.

Is the forward working yet? Until the forward is working, there's no hope of moving forward.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Viv Singh
Ranch Hand

Joined: Nov 08, 2008
Posts: 73
E.g.:



works perfectly fine. However, rather than sending the status as part of the url (query string?), is there any other way?

thanks in advance.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

Viv Singh wrote:Hi,



The problem now is that I get redirected to
http://localhost:8080/supporttool/serv1
rather then to:
http://localhost:8080/supporttool/portal/serv1

Sam: Yes this is how it is supposed to work - The URL you see will the servlet URL in the Form and has nothing to do with the RequestDispatcher URL.
The strange part is that your application name changes from http://localhost:8080/tool/portal/page1.jsp to http://localhost:8080/supporttool/serv1. That indeed would be strange!



For some strange reason this works:
resp.sendRedirect("portal/page1.jsp?status="+status);

Sam: Yes strange indeed because this is how it is supposed to work


Answered inline .....
To sum up - I would suggest that you look at you form in page1.jsp. Somewhere you would have action="/serv1". Changing that to "serv1" without the '/' might do the trick ... (Not a 100% - I just rather never go to a JSP page directly but always through a mapping in the web.xml file.)

Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

Make up your mind. Do you want to forward or redirect?
Viv Singh
Ranch Hand

Joined: Nov 08, 2008
Posts: 73
Bear Bibeault wrote:Make up your mind. Do you want to forward or redirect?


I just want to "get back" to page1.jsp and would like to show a message on that page after the request was handled by the servlet.
Duc Vo
Ranch Hand

Joined: Nov 20, 2008
Posts: 254
Viv Singh wrote:
I just want to "get back" to page1.jsp and would like to show a message on that page after the request was handled by the servlet.

I would guess the best option for you know is to use redirect and pass message back to the original page using query string as you did before.
Alternatively, if you don't want to pass back the whole message, you can pass back the message key via query string, then on the displaying page generate the message depending on the key (i.e. using resource bundle or message tag - a.k. fmt:message).
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

Duc Vo wrote:
Viv Singh wrote:
I just want to "get back" to page1.jsp and would like to show a message on that page after the request was handled by the servlet.

I would guess the best option for you know is to use redirect and pass message back to the original page using query string as you did before

I completely disagree. There's no need for the overhead of a redirect.

Simply forward to the JSP page as per usual after a servlet controller has finished its task and pass the message invisibly as a request-scoped variable.

I don't understand why this needs to be made any harder than it needs to be.
Viv Singh
Ranch Hand

Joined: Nov 08, 2008
Posts: 73
Bear Bibeault wrote:
Simply forward to the JSP page as per usual after a servlet controller has finished its task and pass the message invisibly as a request-scoped variable.


How can I pass the message invisibly as a request-scoped variable?
Would you please be so kind and give a short example?

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

Do you know what a scoped variable is? How can you be writing JSPs without knowing about scoped variables?

Creating a request-scoped variable:


Referencing a scoped variable in a JSP:

Duc Vo
Ranch Hand

Joined: Nov 20, 2008
Posts: 254
Bear Bibeault wrote:
I completely disagree. There's no need for the overhead of a redirect.

I understand why you disagree, Bear. It's not a good practice.
But base on the original requirement from above posts (presumably you've read them), how to you tell the browser to move from servlet url to the jsp url without a redirect? And how request scope survive between redirect requests?
I tried to explain to Viv before but she(?) insisted in getting redirected back to the original jsp.
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
I'm not very experienced with redirects, so can't tell anything about that, but why shouldn't you be able to just go back from the servlet to the same jsp you were on before? I tried this, and it works:

Small jsp (ok.jsp):


and in the start servlet:


Be sure that you use the ".." in front of the servlet name in the jsp (relative path, unless you want to use absolute paths). When you leave them out, you are directed to the root application (with context path "/") and will probably run into a html 404. Maybe you didn't even get sent to your servlet...did you check it actually runs?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with RequestDispatcher