属性操作相关方法:
Object getAttribute(String name) ;
Enumeration getAttributeNames() ;
void removeAttribute(String name) ;
void setAttribute(String name, Object o)
pageContext,request,session,application 对象都有这些方法。这四个对象也称为域对象。
pageContext :属性的作用范围仅限于当前JSP页面
request:属性的作用范围仅限于同一个请求
session:属性的作用范围限于一次会话(浏览器打开直到关闭,称为一次会话)
application:属性的作用范围限于当前WEB应用。只要在一处设置属性,其他各处都可以获取到。
测试:
index.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>page1</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>
<%request.setAttribute("name", "sasa");
application.setAttribute("addr", "beijing");
pageContext.setAttribute("gender", "man");
session.setAttribute("age", "18");
%>
<br><br>
request:<%=request.getAttribute("name") %>
<br><br>
application:<%=application.getAttribute("addr") %>
<br><br>
pageContext:<%=pageContext.getAttribute("gender") %>
<br><br>
session:<%=session.getAttribute("age") %>
<br><br>
<a href="/MyLab2/index2.jsp">index2</a>
<a href="index3">index3</a>
</body>
</html>
index2.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>page2</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>
<%--request.setAttribute("name", "sasa");
application.setAttribute("addr", "beijing");
pageContext.setAttribute("gender", "man");
session.setAttribute("age", "18");
--%>
<br><br>
request:<%=request.getAttribute("name") %>
<br><br>
application:<%=application.getAttribute("addr") %>
<br><br>
pageContext:<%=pageContext.getAttribute("gender") %>
<br><br>
session:<%=session.getAttribute("age") %>
</body>
</html>
index3Servlet:
package com.sa.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import org.apache.catalina.core.ApplicationContext;
public class Index3Servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
//1. 在servlet中无法获得pageContext对象
PrintWriter out=resp.getWriter();
String name=(String) req.getAttribute("name");
out.println("name:"+name);
String addr=(String) getServletContext().getAttribute("addr");
out.println("addr:"+addr);
HttpSession session=req.getSession();
String age= (String) session.getAttribute("age");
out.println("age"+age);
}
}