码迷,mamicode.com
首页 > 编程语言 > 详细

JavaWeb基础 ServletContext get/setAttribute 保存与读取数据

时间:2017-10-22 00:30:38      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:好好学习   add   windows7   todo   _id   ips   static   ppi   etc   

礼悟:
     好好学习多思考,尊师重道存感恩。叶见寻根三二一,江河湖海同一体。
          虚怀若谷良心主,愿行无悔给最苦。读书锻炼强身心,诚劝且行且珍惜。




       javaEE:7           
       javaSE:1.8
          JSTL:1.2.2  
      server:tomcat 8.5
    explorer:Firefox        
             os:windows7 x64
            ide:MyEclipse 2017



工程目录结构
技术分享

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

	<servlet>
		<servlet-name>SavaDataServletContext</servlet-name>
		<servlet-class>jizuiku.web.servlet.SavaDataServletContext</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>SavaDataServletContext</servlet-name>
		<url-pattern>/SavaDataServletContext</url-pattern>
	</servlet-mapping>

	<servlet>
		<servlet-name>ReadDataServletContext</servlet-name>
		<servlet-class>jizuiku.web.servlet.ReadDataServletContext</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>ReadDataServletContext</servlet-name>
		<url-pattern>/ReadDataServletContext</url-pattern>
	</servlet-mapping>

	
</web-app>

 

ReadDataServletContext类的代码

package jizuiku.web.servlet;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * ServletContext 读取数据
 * 
 * @author 给最苦
 * @version V17.10.21
 */
public class ReadDataServletContext extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("读取数据");
		
		ServletContext application = this.getServletContext();
		String nameValue = (String)application.getAttribute("name");
		System.out.println(nameValue);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}
}

 

SavaDataServletContext类

package jizuiku.web.servlet;

import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * ServletContext 保存数据
 * 
 * @author 给最苦
 * @version V17.10.21
 * */
public class SavaDataServletContext extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		ServletContext application = this.getServletContext();
		application.setAttribute("name", "给最苦");
		// 如果再次对name赋值的话,会覆盖
		application.setAttribute("name", "博客园");
		
		System.out.println("数据成功保存在ServletContext中");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

 

效果展示
技术分享

 

 


学习资源:itcast和itheima视频库。如果您有公开的资源,可以分享给我的话,用您的资源学习也可以。
博文是观看视频后,融入思考写成的。博文好,是老师讲得好。博文坏,是 给最苦 没认真。   

JavaWeb基础 ServletContext get/setAttribute 保存与读取数据

标签:好好学习   add   windows7   todo   _id   ips   static   ppi   etc   

原文地址:http://www.cnblogs.com/jizuiku/p/7706821.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!