JAVAWeb开发之Servlet-18.Servlet共享变量与变量的作用域
? setAttribute
? getAttribute
–变量的作用域
? ServletContext
? HttpSession
? HttpServletRequest
–实例
? 测试变量的作用域
–无论对象的作用域如何,共享变量和获得变量的
方法都是一致的
? 共享变量
–setAttribute(“varName”,obj);
? 获得变量
–getAttribute(“varName”);
–在Servlet中有三个作用域
? ServletContext
–范围最大,应用程序级别的,整个应用程序都能访问
? HttpSession
–次之,会话级别的,在当前的浏览器中都能访问
? HttpServletRequest
–范围最小,请求级别,请求结束,变量的作用域也结束
–测试变量的作用域
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ScopeServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public ScopeServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1
ServletContext sc = this.getServletContext();
sc.setAttribute("sc_name", "sc_value");
// 2
HttpSession session = request.getSession();
session.setAttribute("session_name", "session_value");
// 3
request.setAttribute("request_name", "request_value");
String sc_value = (String) sc.getAttribute("sc_name");
String session_value = (String) session.getAttribute("session_name");
String request_value = (String) request.getAttribute("request_name");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(sc_value);
out.print(session_value);
out.println(request_value);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ScopeServlet2 extends HttpServlet {
/**
* Constructor of the object.
*/
public ScopeServlet2() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext sc = this.getServletContext();
HttpSession session = request.getSession();
String sc_value = (String) sc.getAttribute("sc_name");
String session_value = (String) session.getAttribute("session_name");
String request_value = (String) request.getAttribute("request_name");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println(sc_value);
out.println(session_value);
out.println(request_value);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
测试第二个页面获取共享变量
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ScopeServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public ScopeServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1
ServletContext sc = this.getServletContext();
sc.setAttribute("sc_name", "sc_value");
// 2
HttpSession session = request.getSession();
session.setAttribute("session_name", "session_value");
// 3
request.setAttribute("request_name", "request_value");
request.getRequestDispatcher("/servlet/ScopeServlet3").forward(request, response);
/*
String sc_value = (String) sc.getAttribute("sc_name");
String session_value = (String) session.getAttribute("session_name");
String request_value = (String) request.getAttribute("request_name");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(sc_value);
out.print(session_value);
out.println(request_value);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
**/
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ScopeServlet3 extends HttpServlet {
/**
* Constructor of the object.
*/
public ScopeServlet3() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext sc = this.getServletContext();
HttpSession session = request.getSession();
String sc_value = (String) sc.getAttribute("sc_name");
String session_value = (String) session.getAttribute("session_name");
String request_value = (String) request.getAttribute("request_name");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println(" <h1>");
out.println("ScopeServlet3");
out.println(" </h1>");
out.println(sc_value);
out.println(session_value);
out.println(request_value);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ScopeServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public ScopeServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1
ServletContext sc = this.getServletContext();
sc.setAttribute("sc_name", "sc_value");
// 2
HttpSession session = request.getSession();
session.setAttribute("session_name", "session_value");
// 3
request.setAttribute("request_name", "request_value");
//request.getRequestDispatcher("/servlet/ScopeServlet3").forward(request, response);
/*String sc_value = (String) sc.getAttribute("sc_name");
String session_value = (String) session.getAttribute("session_name");
String request_value = (String) request.getAttribute("request_name");**/
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println(" <a href=/Servlet_Scope/servlet/ScopeServlet3>");
out.println(" ScopeServlet3");
out.println(" </a>");
/*out.print(sc_value);
out.print(session_value);
out.println(request_value);**/
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
JAVAWeb开发之Servlet-18.Servlet共享变量与变量的作用域相关推荐
- JDBC+Servlet+JSP整合开发之30-JDBC、Servlet、JSP的MVC
–Servlet 的优势与弊端 –JSP 的优势与弊端 –MVC 设计模式 –实例 ?使用MVC实现学生信息的添加.显示 -----------------------------START----- ...
- JAVAWEB开发之HttpServletResponse和HttpServletRequest详解(上)(各种乱码、验证码、重定向和转发)
HttpServletResponse简介 Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象 request和re ...
- JAVAWEB开发之Spring详解之——Spring的入门以及IOC容器装配Bean(xml和注解的方式)、Spring整合web开发、整合Junit4测试
Spring框架学习路线 Spring的IOC Spring的AOP,AspectJ Spring的事务管理,三大框架的整合 Spring框架概述 什么是Spring? Spring是分层的Java ...
- JavaWeb开发之CookieSession
会话简单理解为:用户打开一个浏览器,点击多个超链接访问服务器的web资源,然后关闭浏览器,整个过程称为是一次会话.每个用户与服务器进行交互过程中,产生一些各自的数据,程序想要把这些数据进行保存,就需要 ...
- JAVAWEB开发之JSTL标签库的使用、 自定义EL函数、自定义标签(带属性的、带标签体的)
JSTL JSTL简介: JSTL的全称:JSP Standard Tag Library,JSP标准标签库 JSTL的作用: 提供给Java Web开发人员一个标准通用的标签函数库 和EL ...
- JavaWeb开发之Ajax省市联动无刷新分页
我们在上一节课已经和大家讲解过Ajax的使用,大家可以去看下该篇文章,今天也主要是一个拓展延伸,使用Ajax去完成无刷新省市联动和无刷新分页. 案列:使用Ajax完成无刷新分页 数据库数据 crea ...
- JAVAWEB开发之JSP、EL、及会话技术(Cookie和Session)的使用详解
Servlet的缺点 开发人员要十分熟悉JAVA 不利于页面调试和维护(修改,重新编译) 很难利用网页设计工具进行页面设计(HTML内容导入到servlet中,用PrintWriter的对象进行输出) ...
- JAVAWEB开发之SpringMVC详解(二)——高级开发、数据回显、参数绑定集合、图片上传、json交互、validation校验、异常处理、RESTful支持、拦截器
知识回顾 springmvc框架 用户请求url到DispatcherServlet前端控制器,相当于中央调度器,降低系统各组件之间的耦合度. DispatcherServlet前端控制器通过Hand ...
- JAVAWEB开发之HttpServletResponse和HttpServletRequest详解(下)(各种乱码、验证码、重定向和转发)
HttpServletRequest获取请求头信息 (1)获取客户机请求头 String getHeader(String name) Enumeration<String> getHe ...
- JAVAWEB开发之Session的追踪创建和销毁、JSP详解(指令,标签,内置对象,动作即转发和包含)、JavaBean及内省技术以及EL表达式获取内容的使用
Session的追踪技术 已知Session是利用cookie机制的服务器端技术,当客户端第一次访问资源时 如果调用request.getSession() 就会在服务器端创建一个由浏览器独享的ses ...
最新文章
- 网络应用 axIos +vue的应用
- 数据分析工具Pandas(3):Pandas的对齐运算
- WheelView实现省市区三级联动(数据库实现版本号附带完整SQL及数据)
- python3 requests模块
- LBWE 和SBIW的关系
- mysql不使用自增_自增ID有什么坏处?什么样的场景下不使用自增ID?
- HighNewTech之QAB:重新温读张首晟教授2018年8月演讲PPT《量子计算, 人工智能与区块链》
- Windows驱动开发学习笔记(五)—— SSDT HOOK
- MySQL binlog三种模式
- 阿里达摩院python_阿里达摩院出品的735集的python教程
- java中math方法语句,下列有关Java中标准类Math的random()方法的说法中,正确的是。 - 上学吧学历考试...
- Linux常出现的面试试题
- JTS(Geometry)工具类
- CentOS 6.2安装配置pacemaker
- 龙腾P2P流媒体点播系统商业计划书
- Mac下安装keras
- word中删除多余的空白页
- 一文读懂多帧超分辨率来龙去脉2019
- 快速排序---(面试碰到过好几次)
- 贪吃蛇大作战中的“马太效应”