Sending redirect to another servlet/JSP without loosing the request parameters.

和自甴很熟 提交于 2019-12-29 04:20:30

问题


How do i specify a redirection to another servlet, in the doPost() method of a servlet.

at the moment I'm using

request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response);

which is wrong since, my parameters in the doGet() method of products are not being called and initialized.

So I'm left with an empty products page after logging in :/


回答1:


You need to use HttpServletResponse#sendRedirect() to send a redirect. Assuming that the servlet is mapped on an URL pattern of /products:

response.sendRedirect("/products");

This way the webbrowser will be instructed to fire a new HTTP GET request on the given URL and thus the doGet() method of the servlet instance will be called where you can in turn load the products and forward to a JSP which displays them the usual way.




回答2:


In your doPost you can always call:

return doGet(request, response);


来源:https://stackoverflow.com/questions/5539176/sending-redirect-to-another-servlet-jsp-without-loosing-the-request-parameters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!