JSP对属性的操作

属性操作相关方法:

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);
}
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值