首先介绍一下request共享:
test1.classTest:
package test1;

public class classTest {
private String name = null;
public classTest(){}
public void setName(String strName_p){
this.name=strName_p;
}
public String getName(){
return this.name;
}

}
MyJsp.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@page import="test1.classTest"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'MyJsp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>
<body>
<jsp:useBean id="stringBean" class="test1.classTest" scope="request"/>
<jsp:setProperty name="stringBean" property="name" value="request"/>
<%request.setAttribute("aa","aa"); %>
<jsp:include page="MyJsp1.jsp"></jsp:include>

</body>
</html>
MyJsp1.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@page import="test1.classTest"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'MyJsp1.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>
<body>

<br>
<%classTest aa= (classTest)request.getAttribute("stringBean") ;%>
<%=aa.getName() %>
</body>
</html>
RequestDispatcher dispatcher=request.getRequestDispatcher("../test1/MyJsp.jsp");
dispatcher.forward(request, response);
的形式
二session共享:
MyJsp.jsp:
<jsp:useBean id="stringBean" class="test1.classTest" scope="session"/>
<jsp:setProperty name="stringBean" property="name" value="session"/>
<%session.setAttribute("aa","aa"); %>
<a href="test2/MyJsp1.jsp">MyJsp1.jsp</a>
MyJsp1.jsp:
<br>
<%classTest aa= (classTest)session.getAttribute("stringBean") ;%>
<%=aa.getName() %>
<br>
<%=session.getAttribute("aa") %>
三application共享:
MyJsp.jsp:
<jsp:useBean id="stringBean" class="test1.classTest" scope="application"/>
<jsp:setProperty name="stringBean" property="name" value="application"/>
<%getServletContext().setAttribute("aa","aa"); %>
<a href="test2/MyJsp1.jsp">MyJsp1.jsp</a>
MyJsp1.jsp:
<br>
<%classTest aa= (classTest)getServletContext().getAttribute("stringBean") ;%>
<%=aa.getName() %>
<br>
<%=getServletContext().getAttribute("aa") %>
再介绍一下servlet和jsp页面的共享:
servlet1:
package test1;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class servlet1 extends HttpServlet {

/**
* Constructor of the object.
*/
public servlet1() {
super();
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Aa");
classTest value=new classTest();
String[] string={"aa","bb"};
value.setName("Request");
request.setAttribute("keyRequest", value);
request.setAttribute("aaa", "aaaa");
request.setAttribute("string", string);
classTest valueSession=new classTest();
valueSession.setName("Session");
HttpSession session=request.getSession();
session.setAttribute("keySession", valueSession);
session.setAttribute("bbb", "bbbb");
//session.setAttribute("string", string);
classTest valueApplication=new classTest();
valueApplication.setName("Application");
getServletContext().setAttribute("valueApplication", valueApplication);
getServletContext().setAttribute("ccc", "cccc");
RequestDispatcher dispatcher=request.getRequestDispatcher("../test1/MyJsp.jsp");
dispatcher.forward(request, response);
}

}
MyJsp.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'MyJsp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>
<body>
<jsp:useBean id="keyRequest" type="test1.classTest" scope="request"></jsp:useBean>
<jsp:getProperty name="keyRequest" property="name"/>
<br>

<jsp:useBean id="valueApplication" type="test1.classTest" scope="application"></jsp:useBean>
<jsp:getProperty name="valueApplication" property="name"/>
<br>
${aaa}
<br>
${bbb}
<br>
${ccc}
<br>
${keyRequest.name}
<br>
${string[0]}
<br>
<% test1.classTest aa=(test1.classTest)session.getAttribute("keySession"); %>
<%=aa.getName() %>
<br>
<%=keyRequest.getName() %>

</body>
</html>
稍改一下serlvet
替换成:
out.println("<a href='../test1/MyJsp.jsp'>MyJsp.jsp</a>");
再看MyJsp.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'MyJsp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>
<body>
<jsp:useBean id="keyRequest"
type="test1.classTest" scope="request"/>//注意type与class的区别
<jsp:getProperty name="keyRequest" property="name"/>
<jsp:useBean id="valueApplication"
type="test1.classTest" scope="application"/>
<jsp:getProperty name="valueApplication" property="name"/>//必须引入userBean
<br>
<br>
${aaa} //这样的写法简练,可以不需要前面的userBean,直接得到结果
<br>
${bbb}
<br>
${ccc}
<br>
${keyRequest.name}
<br>
${string[0]}
<br>
<% test1.classTest aa=(test1.classTest)session.getAttribute("keySession"); %>//这种形式不需要userBean但是较为麻烦
<%=aa.getName() %>
<br>
<%=keyRequest.getName() %>//这种形式引入上面的userBean
</body>
</html>
null Application
bbbb
cccc
Session
null
很明显request的结果就为null,session和application没有影响,和我们所说的jsp间的传递一样,
其实jsp也是sevlet。
test1.classTest:

















MyJsp.jsp:


































MyJsp1.jsp:

































- 如果用request,要注意request的范围仅限于同一个request对象;如果使用了form的"action"或者response.sendRedirect("xxx.jsp")等调转,则不再是同一个request对象,即用request.getAttribute("name")将取不到值。
- 换成request.getRequestDispatcher("xxx.jsp").forward(request,response);
才能共享request中数据
RequestDispatcher dispatcher=request.getRequestDispatcher("../test1/MyJsp.jsp");
dispatcher.forward(request, response);
的形式
二session共享:
MyJsp.jsp:




MyJsp1.jsp:





三application共享:
MyJsp.jsp:




MyJsp1.jsp:





再介绍一下servlet和jsp页面的共享:
servlet1:























































MyJsp.jsp:


















































稍改一下serlvet
替换成:

再看MyJsp.jsp

















































bbbb
cccc
Session
null
很明显request的结果就为null,session和application没有影响,和我们所说的jsp间的传递一样,
其实jsp也是sevlet。