English 中文(简体)
forward from servlet in higher directory to jsp in subfolder
原标题:

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

问题回答

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

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.

req.getRequestDispatcher("../ïndex.jsp").forward(req,resp);




相关问题
Convert typed-in Text to lowercase

I ve got an index.jsp with [snip] <% String name = request.getParameter("name"); String pass = request.getParameter("pass"); String globalname = "webeng"; String globalpass = "2009"; ...

session transfer issue from Tomcat to ASP.Net

I am using Tomcat to host JSP and using IIS 7.0 to host aspx (C# + .Net 3.5 + VSTS 2008), and I have some session transfer issue from JSP page to ASPX page. JSP page is in one domain and all other ...

Setting the default value in Struts2

I am setting the value(kind of default value) for a drop down select value from action class in a page(given below). When the page loads the value is beig displayed but the other elements of the ...

Evaluate dynamically constructed JSP at runtime

I have a requirement where in the JSP page itself is created by the user and stored in the database. When viewing results we need to render this JSP to the client, evaluating all tags inside this JSP. ...

How to Pack/Encrypt/Unpack/Decrypt a bunch of files in Java?

I m essentially trying to do the following on a Java/JSP-driven web site: User supplies a password Password is used to build a strongly-encrypted archive file (zip, or anything else) containing a ...

JSP exception - class not found (tomcat)

I m setting up an existing application on a new Tomcat 5.5 server connecting to a Postgres database (running on Debian Lenny). When I access it I get a series of stack traces with the following root ...

ArrayList to Table in JSP

I have an ArrayList and i am trying to display it in a table ..... ArrayList rows = .... ..... <table cellspacing="1" cellpadding="4" border="3"> <tr> <TH>...

热门标签