关于request.setAttribute多页传值,多页取值

request的setAttribute方法确实能够多页传值和多页取值的,因为request完成的是服务器跳转,在这期间所有设置的内容会被保存下来。下面的例子包含4个页面,1page.jsp和2page.jsp完成的是页面多次传值,3page.sjp和4page.jsp完成的是页面多次取值,直到页面显示出来,url一直是1page.jsp,说明这是服务器跳转。

1page.jsp

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档title>
head>
  <%
    request.setAttribute("name","zhangsan");
  %>
<body>
  <jsp:forward page="2page.jsp"/>
body>
html>

2page.jsp

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档title>
head>

<body>
  <jsp:forward page="3page.jsp"/>
body>
html>

3page.jsp

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档title>
head>

<body>
  <%=request.getAttribute("name")%>
    <jsp:forward page="4page.jsp"/>
body>
html>

4page.jsp

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档title>
head>

<body>
  <%=request.getAttribute("name")+" in 4page.jsp"%>
body>
html>

你可能感兴趣的:(SSH)