在不丢失请求参数的情况下将重定向发送到另一个servlet / JSP。 [英] Sending redirect to another servlet/JSP without loosing the request parameters.

查看:120
本文介绍了在不丢失请求参数的情况下将重定向发送到另一个servlet / JSP。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在servlet的doPost()方法中指定重定向到另一个servlet。

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

我正在使用

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

这是错误的,因为我的产品的doGet()方法中的参数没有被调用和初始化。

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 :/

推荐答案

你需要使用 HttpServletResponse#sendRedirect() 发送重定向。假设servlet映射到 / products 的URL模式:

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");

这样,将指示webbrowser在给定的URL上触发新的HTTP GET请求,从而 doGet()将调用servlet实例的方法,您可以在其中加载产品并转发到以常规方式显示它们的JSP。

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.

这篇关于在不丢失请求参数的情况下将重定向发送到另一个servlet / JSP。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆