JSP Forward and calling servlets (Passing on 2 session variables)
I am trying to redirect a 'HTTPrequest' from a JSP page to a servlet (located in a package); passing on variables that are stored in a session object. I had the idea to use:
<jsp:forward page"/servletName">
<jsp:param name="var1" value="<%=beanID.getVar1()%>" />
<jsp:param name="var2" value="<%=beanID.getVar2()%>" />
</jsp:forward>
In the servlet you can find a doPost with an @override annotation. With the following code:
public class servletName extends HttpServlet{
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
var1 = request.getParameter("var1").toString();
var2 = request.getParameter("var2").toString();
// do more with the variables.
}
When running the project, the parameters get sent to the JSP where the redirect / forward is called. After the variables have been stored (and where the servlet has to be called [JSP:forward]) the app returns a 404 Page does not exist开发者_JS百科.
Does anybody have an idea? If clarification is needed, please tell me.
Thanks in advance! B.
The 404 will occur whenever the URL is plain wrong, or there's actually nothing which sits behind the URL. In case of servlets, the last can happen when the servlet is not declared in web.xml
, or when the URL does not match servlet's url-pattern
, or when the construction and initialization of the servlet failed (this one should be visible in server logs however).
It may help to take a look in [servlets]
tag info page. There's a Hello World example which may help in giving the new insights about using JSP/servlets.
Note that you aren't redirecting here. You are forwarding to a servlet. The servlet will obtain exactly the same request/response as the JSP. A redirect basically instructs the client to create a new request.
first of all you don't need to send those variables as parameter you could access the session object within the servlet (request.getSession()). Are you sure that the servlet is being called ? Maybe you could show little more code what happens in the servlet.
精彩评论