Java学习路程(javaEE)之ServletConfig对象、ServletContext对象、response 响应和request 请求

一.ServletConfig对象

1.配置信息需要在web.xml进行配置,是以键值对形式配置 key-value,并且在Servlet初始化配置
2.获取ServletConfig对象
方法一:声明成员变量保存ServletConfig

public class Demo01 extends HttpServlet{
	//声明变量
	private ServletConfig config;
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		//接收参数中的配置对象
		this.config = config;
	}
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//遍历获取ServletConfig的值
		Enumeration values = this.config.getInitParameterNames();
		while (values.hasMoreElements()) {
			String name = values.nextElement();
			System.out.println(name +"="+ this.config.getInitParameter(name));
			方法二:通过父类中的方法直接获取ServletConfig对象
			ServletConfig servletConfig = this.getServletConfig();
			String value = servletConfig.getInitParameter("lisi");
		}
	}
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}

Servlet初始化配置


  	demo02
  	com.lanou3g.Demo02
  	
  	
  	
  		lisi
  		hello
  	
  	
  	
  		zhangsan
  		world
  	
  	
  		zhang
  		123
  	
  
  
  	demo02
  	/demo02
  

二.ServletContext(application域)

1.域对象:
在一定范围内有效的对象
2.作用范围:
当前工程都有效并且整个工程有且只有一个ServletContext对象
3.域对象共有的方法:
1).setAttribute
2).getAttribute
3).removeAttribute
4.域对象的作用:
1).存值取值
2).进行单例传值
3).可以获取全局配置信息
4).可以获取项目中所有资源在服务器上的绝对路径
5).可以进行请求转发

public class Demo03 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		  获取context域方式一: 通过ServletContext对象获取
		ServletContext application = this.getServletConfig().getServletContext();
		//存值
		application.setAttribute("name", "lisi");
		 获取全局配置信息
		String value = application.getInitParameter("zhangfei");
		System.out.println(value);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

Servlet配置

 
  	demo03
  	com.lanou3g.Demo03
  
  
  	demo03
  	/demo03
  

使用域对象取值

public class Demo04 extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletContext application = this.getServletConfig().getServletContext();
		//取值
		Object value = application.getAttribute("name");
		System.out.println(value);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

Servlet设置

 
  	demo04
  	com.lanou3g.Demo04
  
  
  	demo04
  	/demo04
  

获取项目中所有资源在服务器上的绝对路径

public class Demo05 extends HttpServlet{
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		获取context域
		ServletContext application = this.getServletContext();
		获取服务器上的真实路径
		String path1 = application.getRealPath("/WEB-INF/classes/a.properties");
		读取文件
		Properties properties = new Properties();
		FileInputStream fis = new FileInputStream(path1);
		读取
		properties.load(fis);
		System.out.println(properties.getProperty("key"));
		关闭资源
	    fis.close();
	}
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}

Servlet配置


  	demo05
  	com.lanou3g.Demo05
  
  
  	demo05
  	/demo05
  

Context域进行请求转发

public class Demo06 extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("急缺钱");
		System.out.println("找别人借");
		ServletContext application = this.getServletContext();
		//获取请求转发器
		//注意请求转发只能转发站内的路径并且传入的地址相对于工程的
		RequestDispatcher dispatcher = application.getRequestDispatcher("/demo07");
		//发送转发请求
		dispatcher.forward(request, response);
		System.out.println("借钱完毕");
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

public class Demo07 extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("借你一个亿");
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	doGet(request, response);
	}
}

打印结果:

急缺钱
找别人借
借你一个亿
借钱完毕

注意:
1.用户只发送了一次请求,网址没有发生变化
2.只能转发站内

三.response 响应

1.响应包含的内容
响应行 响应状态码 200(成功) 302(重定向)
响应头 告知浏览器信息
响应体 响应的内容

public class Demo08 extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    修改编码格式并设置响应头以什么编码格式解析响应
		response.setContentType("text/html;charset=UTF-8");
		利用response获取字符流和字节流
		PrintWriter writer = response.getWriter();
		writer.write("李四");
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

下载图片

public class Demo09 extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		ServletContext application = this.getServletContext();
		//获取图片在服务器上的真实路径
		String path = application.getRealPath("/WEB-INF/classes/哈哈.png");
		//通过file类获取文件名
		File file1 = new File(path);
		String name = file1.getName();
		//设置图片名的编码格式 iso-8859-1
		name = new String(name.getBytes(), "iso-8859-1");
		//通过设置响应头 告诉浏览器 要下载   content-disposition attachment;filename=图片名(下载头)
		response.setHeader("content-disposition", "attachment;filename=" + name);
		//设置下载内容的格式(web.xml里查找资源后缀的格式)
		response.setHeader("Content-type", "image/png");
		File file = new File(path);
		//使用字节流读取图片
		FileInputStream fis = new FileInputStream(file);
		//使用响应中的字节流,将图片响应回浏览器
		ServletOutputStream sos = response.getOutputStream();
		byte[] b = new byte[1024];
		int len = 0;
		while ((len = fis.read(b)) != -1) {
			 sos.write(b, 0, len);
		}	
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

2.请求重定向和刷新头

public class Demo10 extends HttpServlet{
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("借钱");
		//可以进行站内重定向也可以进行站外重定向  相对于8080 后的斜杠(需要带上工程名)
		response.setHeader("location", "/sh-web-02/demo11");//站内
		// 添加重定向的状态码
		response.setStatus(302);
		 System.out.println("借钱完毕");
		 
		// 刷新头  3秒后刷新页面
		response.setHeader("refresh", "3;url=/sh-web-02/demo11");// 站内
		
		// 每隔一秒刷新界面
		response.setIntHeader("refresh", 1);
	}
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}

public class Demo11 extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("钱借到了");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

重定向结果
1.重定向发送了两次请求(网址改变)
2.选执行完第一次请求的方法在进行第二次请求

借钱
借钱完毕
钱借到了

四.request 请求

public class Demo01 extends HttpServlet{
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 获取请求的方式
		String method = request.getMethod();
		System.out.println(method);
		// 获取用户请求的URL(统一资源定位符)
		StringBuffer requestURL = request.getRequestURL();
		System.out.println(requestURL);//  http://localhost:8080/sh-web-02/demo12
		// 获取用户URI(统一资源标识符)
		String requestURI = request.getRequestURI();
		System.out.println(requestURI);//  /sh-web-02/demo12
		
		// 获取相对路径
		String contextPath = request.getContextPath();
		System.out.println(contextPath);//  /sh-web-02
		// 获取用户请求的参数
		请求地址:
		http://localhost:8080/sh-web-02/demo12?username=lisi&password=123
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println(username);
		System.out.println(password);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}

你可能感兴趣的:(Java学习路程(javaEE)之ServletConfig对象、ServletContext对象、response 响应和request 请求)