views:

334

answers:

2

Hi. i have

root/logged/form.jsp
root/servlet
root/logged/form.jsp

I have jsp page logged/form.jsp this submits form to servlet action="../update". Now i want to add some parameters to request and forward it to logged/form.jsp but its not working and showing me form.jsp in root context only root/servlet. Please help what url should i forward my request to. I cannot use sendRedirect as has to retain request object.

I have tried with forward(logged/form.jsp) and forward(/logged/form.jsp) and forward(/form.jsp) in my servlet

A: 

Try always using absolute paths (starting with a / ) which are interpreted as relative to the context root.

Chris Harcourt
A: 

The /logged/form.jsp ought to be the right one. I suggest to read the appserver logs. Big chance that there's an IllegalStateException: response already committed inside.

Wait, wait, your actual problem is thus that you want to change the URL which the visitor sees in the address bar?

If so, then no, that isn't possible with a forward. I'd then suggest to solve the problem from the other side on. Just "hide" form.jsp in the /WEB-INF folder and use a servlet all the time to get/post the form.

Pseudo:

protected void doGet(request, response) {
    request.getRequestDispatcher("/WEB-INF/logged/form.jsp").forward(request, response);
}

protected void doPost(request, response) {
    doYourSubmitThingHere();
    request.getRequestDispatcher("/WEB-INF/logged/form.jsp").forward(request, response);
}

map this servlet on an url-pattern of /logged/form, replace the <form method="post" action="/servlet"> by <form method="post" action="/logged/form"> and then you can use/invoke it by http://example.com/logged/form.

You can also go a step further by adopting the page controller pattern and make use of HttpServletRequest#getPathInfo() to obtain the request path (and the JSP file's path) so that you don't need to boil a new servlet for every JSP.

BalusC
why to put logged folder in web-inf.I am not getting desired o/p with this. say from http://example.com/index.html when logged in i want to go to http://example.com/looged/form.jsp which submits form to servlet(http://example.com/servlet) and then it is forwaded back to http://example.com/looged/form.jsp what is the best way to that. I moved form.jsp from my root directoty to "logged" folder in my root dir. but my forwarding back to form.jsp from servlet is not working. forward("/looged/form.jsp")
Chava