栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Servlet常用对象二(jsp课程第四课)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Servlet常用对象二(jsp课程第四课)

理论: Tomcat服务器为每个web项目都创建一个ServletContext对象
任何一个web项目只有一个ServletContext对象 ServletContext对象在tomcat服务器启动时创建对象,
tomcat服务器关闭时销毁对象,cpu会将该对象进行释放内存 作用:让同一个项目中的所有Servlet共享数据。
1.共享静态数据
2.共享动态数据
3.共享文件数据
 
package com.servlet;

import java.io.IOException;

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


@WebServlet("/Servlet1")
public class Servlet1 extends HttpServlet {
	private static final long serialVersionUID = 1L;

    
    public Servlet1() {
        // TODO Auto-generated constructor stub
    }

	
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置 发送给浏览器的文本信息HTML文本格式
		//response.setContentType("text/html");
		response.setContentType("text/html");
		//设置发送给浏览器文本utf-8
		//response.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		//因为浏览器是使用get请求
		//所以Servlet do-get
		//servlet1相打篮球
		//1获取ServletContext对象
		ServletContext  sc=getServletContext();
		//获取web.xml文件中的镜头数据
		String a=sc.getInitParameter("lq");
		//获得servlet4共享的排球
		String b= (String) sc.getAttribute("pq");
		//删除排球
		sc.removeAttribute("pq");
		//给浏览器发送数据
		response.getWriter().append("Servlet1获得静态数据用来访问lq:"+a);
		response.getWriter().append("Servlet1获得静态数据用来访问pq:"+b);
		
	}

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

}
package com.servlet;

import java.io.IOException;

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


@WebServlet("/Servlet2")
public class Servlet2 extends HttpServlet {
	private static final long serialVersionUID = 1L;

    
    public Servlet2() {
        // TODO Auto-generated constructor stub
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//设置 发送给浏览器的文本信息HTML文本格式
				//response.setContentType("text/html");
				response.setContentType("text/html");
				//设置发送给浏览器文本utf-8
				//response.setCharacterEncoding("UTF-8");
				response.setCharacterEncoding("UTF-8");
				//因为浏览器是使用get请求
				//所以Servlet do-get
				//servlet1相打篮球
				//1获取ServletContext对象
				ServletContext  sc=getServletContext();
				//获取web.xml文件中的镜头数据
				String a=sc.getInitParameter("zq");
				//给浏览器发送数据
				response.getWriter().append("Servlet2获得静态数据用来访问zq:"+a);
				
	}

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

}
package com.servlet;

import java.io.IOException;

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


@WebServlet("/Servlet3")
public class Servlet3 extends HttpServlet {
	private static final long serialVersionUID = 1L;

    
    public Servlet3() {
        // TODO Auto-generated constructor stub
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//设置 发送给浏览器的文本信息HTML文本格式
				//response.setContentType("text/html");
				response.setContentType("text/html");
				//设置发送给浏览器文本utf-8
				//response.setCharacterEncoding("UTF-8");
				response.setCharacterEncoding("UTF-8");
				//因为浏览器是使用get请求
				//所以Servlet do-get
				//servlet1相打篮球
				//1获取ServletContext对象
				ServletContext  sc=getServletContext();
				//获取web.xml文件中的镜头数据
				String a=sc.getInitParameter("lq");
				//给浏览器发送数据
				response.getWriter().append("Servlet3获得静态数据用来访问lq:"+a);
	
	
	}

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

}
package com.servlet;

import java.io.IOException;

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


@WebServlet("/Servlet4")
public class Servlet4 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
    public Servlet4() {
        super();
        // TODO Auto-generated constructor stub
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//servlet4共享班级 共享数据
		//共享动态数据
		response.setContentType("text/html");
		//设置发送给浏览器文本utf-8
		//response.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		//获取ServletContext 对象
		ServletContext sc=getServletContext();
		//将排球共享数据 setAttribute方法共享数据。
		//在java中object是任何数据类型的父类。
		//身份的多种类型;向上转型
		sc.setAttribute("pq", "排球");
		//身份的多种类型;向上转型(强制转型)
		//获得动态数据
		//疯狂java讲义
		//getimitParameter只能获得静态数据
		 String  a= (String) sc.getAttribute("pq");
		response.getWriter().append("Servlet4获得动态数据 "+a);
	}

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

}
package com.servlet;

import java.io.IOException;
import java.io.InputStream;

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


@WebServlet("/Servlet5")
public class Servlet5 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
    public Servlet5() {
        super();
        // TODO Auto-generated constructor stub
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response, byte[] bytes) throws ServletException, IOException {
		// 获得text.txt完整路径
		ServletContext sc=getServletContext();
		//指定文件名称获取完整路径
		String path=sc.getRealPath("text.txt");
		//控制台打印输出文件完整路径
		System.out.println(path);
		//将text.txt读取出来
		//IO inputStream /outputStream输出流。
		//网上下载的图片以自己为主场输入
		//总结:始终以自己为主场,观察数据流向。
		InputStream is=sc.getResourceAsStream("/text.txt");
		//分析:1假设text.txt一部的小说
		//当读去大量数据,分段读取,部分读取
		//1024个字节 512个字 1kb=1024
		byte[] bytes1 =new byte[1024];
		//分析:2假设text.txt
		//当少量数据数据非常少的时刻
		//定义一各变量 ,用于记录字节数 判断>1024
		int  count=-1;  //-1标记读取结束
		//定义一个字符串读取部分
		String sum="";
		
		//循环读取
		while ((count =is.read(bytes1 ))!=-1) {
			//读出bytes,0.数据转字符串
			String s =new String(bytes1 ,0,count);
			sum+=s;
		}
		System.out.println(sum);
		
		
		//response.getWriter().append("Served at: ").append(request.getContextPath());
	}

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

}
lo流

按操作数据单位分为:字节流、字符流
对于文本文件(.txt,.java,.c,.cpp),使用字符流处理
对于非文本文件(.jpg,.mp3,.mp4,.avi,.doc,.ppt,…),使用字节流处理

按数据的流向分为:输入流、输出流
输入input:读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中。
输出output:将程序(内存)数据输出到磁盘、光盘等存储设备中。

按流的角色分为:节点流、处理流
节点流:直接从数据源或目的地读写数据。

处理流:不直接连接到数据源或目的地,而是“连接”在已存在的流(节点流或处理流)之上,通过对数据的处理为程序提供更为强大的读写功能。
 

总结

ServletContext 作用   同一个项目共享数据。
1  共享静态数据
2  共享动态数据
3  共享文件数据

第二点

response.setContentType("text/html");
        //设置发送给浏览器文本utf-8
        //response.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

//获取ServletContext 对象
        ServletContext sc=getServletContext();

 * sc.setAttribute("pq", "排球"); 接收对象 

* 删除对象 sc.removeAttribute("pq");

第三点

// 获得text.txt完整路径
        ServletContext sc=getServletContext();
        //指定文件名称获取完整路径
        String path=sc.getRealPath("text.txt");
        //控制台打印输出文件完整路径
        System.out.println(path);
        //将text.txt读取出来
        //IO inputStream /outputStream输出流。
        //网上下载的图片以自己为主场输入
        //总结:始终以自己为主场,观察数据流向。
        InputStream is=sc.getResourceAsStream("/text.txt");
        //分析:1假设text.txt一部的小说
        //当读去大量数据,分段读取,部分读取
        //1024个字节 512个字 1kb=1024
        byte[] bytes1 =new byte[1024];
        //分析:2假设text.txt
        //当少量数据数据非常少的时刻
        //定义一各变量 ,用于记录字节数 判断>1024
        int  count=-1;  //-1标记读取结束
        //定义一个字符串读取部分
        String sum="";
        
        //循环读取
        while ((count =is.read(bytes1 ))!=-1) {
            //读出bytes,0.数据转字符串
            String s =new String(bytes1 ,0,count);
            sum+=s;
        }
        System.out.println(sum);

InputSteamReader
输出流OutputSteamWriter

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/315485.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号