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

查看:20
本文介绍了在不丢失请求参数的情况下将重定向发送到另一个 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 请求,因此将调用 servlet 实例的 doGet() 方法,您可以在那里依次加载产品并转发到以通常方式显示它们的 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天全站免登陆