• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

problem with RequestDispatcher

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this :



This is will redirect to page.jsp.
 
Viv Singh
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont understand your code. Why is it re dispatching/forwarding to the same page?
 
Viv Singh
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this RequestDispatcher view = req.getRequestDispatcher(request.getContextPath()+"/portal/page1.jsp");
 
Viv Singh
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 164
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think the bellow example will help you
webpage
 
Viv Singh
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, but this is what I am already doing. The problem is with the URL, I get redirected to a "wrong path".
 
Vt Guru
Ranch Hand
Posts: 164
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of giving relative path specify with a full path.
 
Viv Singh
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
"
 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Viv Singh
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any other way of solving the problem of not sending the error message string as part of the url?
 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Viv Singh
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.)
 
Bear Bibeault
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make up your mind. Do you want to forward or redirect?
 
Viv Singh
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



Just try to forward your request to path "/"

It will give you idea how RequestDispatcher expecting path. Then provide URI of your jsp

For example on forwarding request on "/", you will be taken to root of webapp, if your jsp is inside WEB-INF directory, you have to specify your path like this

"/WEB-INF/pages/page.jsp"
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic