servlet中的forward方法

本文介绍了Servlet中的`forward()`方法,用于将请求从一个Servlet转发到服务器上的其他资源,如JSP或HTML文件。该方法允许初步处理请求并由另一资源生成响应。在调用`forward()`之前,确保响应尚未提交,否则会引发异常。同时,请求和响应参数需为原始对象或它们的包装类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

forward

//验证码不正确,返回登录页面
System.out.println("验证码错误");
req.getRequestDispatcher("/pages/user/regist.html").forward(req, resp);

 

forward(动词)可以用作“转寄”或“转送”解。例如:

Please forward the enclosed letter to Mr J. Hall.

在商业文件里也用作“送”或“寄”解,如We shall forward the goods this afternoon和We forwarded you the invoice the day before yesterday。

有时竟用作不及物动词。例如:Please sign the card and forward together with a cheque for ¥20. 在一般文字里都该避免。

forward=send on

They arranged for the information to be sent on to us. 他们托人把信息转告我们。

public void forward(ServletRequest request,
                    ServletResponse response)
             throws ServletException,
                    java.io.IOException

Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. This method allows one servlet to do preliminary processing of a request and another resource to generate the response.

For a RequestDispatcher obtained via getRequestDispatcher(), the ServletRequest object has its path elements and parameters adjusted to match the path of the target resource.

forward should be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward.

The request and response parameters must be either the same objects as were passed to the calling servlet's service method or be subclasses of the ServletRequestWrapper or ServletResponseWrapper classes that wrap them.

将来自servlet的请求转发到服务器上的另一个资源(servlet、JSP文件或HTML文件)。该方法允许一个servlet对请求进行初步处理,并允许另一个资源生成响应。

对于通过getRequestDispatcher()获得的RequestDispatcher,ServletRequest对象将调整其路径元素和参数,以匹配目标资源的路径。

在响应提交到客户端之前(在刷新响应正文输出之前),应该调用forward。如果此方法已提交异常,则抛出异常。在转发之前,响应缓冲区中的未提交输出会自动清除。

请求和响应参数必须是传递给调用servlet的服务方法的相同对象,或者是封装它们的ServletRequestWrapper或ServletResponseWrapper类的子类。

Parameters:

request - a ServletRequest object that represents the request the client makes of the servlet

response - a ServletResponse object that represents the response the servlet returns to the client

Throws:

ServletException - if the target resource throws this exception

java.io.IOException - if the target resource throws this exception

java.lang.IllegalStateException - if the response was already committed

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值