【Java.Web】JSP —— 语法基础 —— 请求转发

请求转发

JSP和Servlet一样,也能进行请求转发。


JSP采用<jsp:forward>标签来实现请求转发,转发的目标组件可以为HTML文件,JSP文件或者Servlet。

<jsp:forward>的语法为:

<jsp:forward page="转发的目标组件的绝对/相对URL" />

<jsp:forward>标签中的page属性既可以为相对路径(不以“/”开头),也可以为绝对路径(以“/”开头)。


  • JSP与Servlet请求转发一样,JSP源组件和目标组件共享HttpServletRequest对象和HttpServletResponse对象;JSP源组件中所有输出数据都不会被发送到客户端
  • 对于Servlet源组件调用response.sendRedirect(String location)方法进行请求转发之后的代码也会被执行。但是,JSP源组件中<jsp:forward>标签之后的代码不会被执行

转发源组件还可以通过<jsp:param>标签向转发目标组件传递额外的请求参数。在目标组件中通过request.getParameter(参数名)方法来读取源JSP传过来的请求参数。



在jsp中,同样可以使用在servlet中通过RequestDispatch的forward方法进行请求的转发。



示例:

源JSP:redirect_source.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Redirect Source JSP</title>
</head>
<body>
    <p>
        This is output of redirect_source.jsp before forward.
    </p>
    <% System.out.println("This is output of redirect_source.jsp before forward."); %>
    
    <%-- Redirect the request --%>
    <jsp:forward page="redirect_destination.jsp">
        <jsp:param name="username" value="new user"/>
        <jsp:param name="password" value="new password"/>
    </jsp:forward>
    
    <p>
        This is output of redirect_source.jsp after forward.
    </p>
    <% System.out.println("This is output of redirect_source.jsp after forward."); %>
    
</body>
</html>


目标JSP:redirect_destination.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; utf-8">
<title>Redirect Destination JSP</title>
</head>
<body>
    <p>
        This is output of redirect_destination.jsp.<br/>
        The username is <%= request.getParameter("username") %> <br/>
        The password is <%= request.getParameter("password") %> <br/>
    </p>
</body>
</html>


【Java.Web】JSP —— 语法基础 —— 请求转发_第1张图片


在浏览器中请求如下的URL:

http://localhost:8080/base-webapp/jsp/redirect_source.jsp

在浏览器中显示如下:


在console中的输出如下:

This is output of redirect_source.jsp before forward.

对于response的输出,只有目标组件JSP中的内容被输出。并且只有<jsp:forward>之前的代码被执行,而后面的代码未被执行(第二个system.out.println为输出到console)。


修改1:

在源JSP中,使用RequestDispatch的forward方法来代替<jsp:forward>标签:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Redirect Source JSP</title>
</head>
<body>
    <p>
        This is output of redirect_source.jsp before forward.
    </p>
    <% System.out.println("This is output of redirect_source.jsp before forward."); %>
    
    <%-- Redirect the request --%>
    <%-- 
    <jsp:forward page="redirect_destination.jsp">
        <jsp:param name="username" value="new user"/>
        <jsp:param name="password" value="new password"/>
    </jsp:forward>
    --%>
    <% application.getRequestDispatcher("/jsp/redirect_destination.jsp").forward(request, response); %>
    
    <p>
        This is output of redirect_source.jsp after forward.
    </p>
    <% System.out.println("This is output of redirect_source.jsp after forward."); %>
    
</body>
</html>

再次访问上面的URL:

浏览器显示为:


console中显示为:

This is output of redirect_source.jsp before forward.
This is output of redirect_source.jsp after forward.


与在Servlet中请求转发的情形一样,forward方法后面的代码同样被执行。





你可能感兴趣的:(java,Web)