ServletContext对象

一个web容器可以多个Servlet交互,比如我在淘宝登陆,我的登陆信息会被带到淘宝的很多页面,跳到其他页面也有登陆信息,这个操作肯定不是它自己做的,需要一个中间商,也就是ServletContext

web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用:

  • 共享数据:我在则合格Servlet中保存的数据,可以在另一个Servlet中拿到

        

package com.godairo.servlet;

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

public class HelloServlet extends HttpServlet {

	  @Override
	  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

			ServletContext context = this.getServletContext();
			String username = "小可爱"; //数据
			context.setAttribute("username",username); //将一个数据保存在了ServletContext中,名字为:username,它的值为:username
			System.out.println("Hello");
	  }

	  @Override
	  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			doGet(req, resp);
	  }
}
package com.godairo.servlet;

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

public class GetServlet extends HttpServlet {
	  @Override
	  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			ServletContext context = this.getServletContext();
			String username = (String) context.getAttribute("username");

			response.setContentType("text/html");
			response.setCharacterEncoding("utf-8");
			response.getWriter().print("名字:" + username);
	  }

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


    
    
        hello
        com.godairo.servlet.HelloServlet
    
    
    
        hello
        /hello
    

    
        getusername
        com.godairo.servlet.GetServlet
    

    
        getusername
        /getusername
    

测试访问结果:先输入hello保存数据,然后输入getusername读取

ServletContext对象_第1张图片

ServletContext对象_第2张图片

  • 获取初始化参数


        url
        jdbc:mysql://localhost:3306/mybatis
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			ServletContext context = this.getServletContext();

			String url = context.getInitParameter("url");
			response.getWriter().print(url);
	  }

ServletContext对象_第3张图片

  • 请求转发 

        

@Override
	  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			ServletContext context = this.getServletContext();
			System.out.println("进入了ServletDemo04");
			//RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp");  //转发的请求路径
			//requestDispatcher.forward(request,response); //调用forward实现请求转发;
			context.getRequestDispatcher("/gp").forward(request,response);
	  }
  • 读取资源文件

   ProPerties

  • 在java目录下新建properties
  • 在resources目录下新建properties

     发现:都被打包到了同一个路径下,这个丼是classes路径,我们俗称这个路径为类路径classpath

     思路:需要一个文件流。  

package com.godairo.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ServletDemo05 extends HttpServlet {
	  @Override
	  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
			Properties properties = new Properties();
			properties.load(is);
			String username = properties.getProperty("username");
			String password = properties.getProperty("password");
			resp.getWriter().print(username+":"+password);
			
	  }

	  @Override
	  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			doGet(req, resp);
	  }
}
username=godairo
password=123456

 访问测试即可!

之后的共享数据会用session和request去做

获取初始化基本也没用这种方法

请求转发用request

读取资源文件会用类加载去做

这里是打基础

你可能感兴趣的:(Servlet学习,java,开发语言,后端)