java - ServletContext
ServletContext:
web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用;
作用:共享数据---->我们在这个servlet保存的数据可以在另外一个servlet中拿到:
public class Helloservlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("Hello,servlet"); //this.getInitParameter()初始化参数 //this.getServletConfig()servlet配置 //this.getServletContext()servlet上下文 ServletContext context=this.getServletContext(); String username="java_小白";//数据 context.setAttribute("username",username);//将一个数据保存在了servletContext中,名字为:username = 值为 username } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } }
我们在helloservlet中创建了一个ServletContext对象,通过ServletContext的setAttribute方法,把一个数据(username)放入了这个对象之中;用于被公共取用
public class Getservlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context1=this.getServletContext(); String username =(String) context1.getAttribute("username"); resp.setContentType("text/hrml"); resp.setCharacterEncoding("utf-8"); resp.getWriter().print("名字:"+username); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } }
我们继续在Getservlet中创建了一个ServletContext对象,不同的是我们使用了ServletContext的getAttribute方法;用于拿到username中的值,并且将它输出来
值得注意的是,这个时候的返回值是object类,所以在我们知道返回值是String类型的时候我们可以将它强制转换为String,才不发生错误
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="3.1" metadata-complete="true"> <servlet> <servlet-name>hello</servlet-name> <servlet-class>top.lostyou.servlet.Helloservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <servlet> <servlet-name>getc</servlet-name> <servlet-class>top.lostyou.servlet.Getservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>getc</servlet-name> <url-pattern>/getc</url-pattern> </servlet-mapping> </web-app>
我们在编辑好了servlet后,每编辑好一个我们都要取对应的XML中注册
分别是 servlet 和 servlet-mapping 两个
resp.setContentType("text/hrml");
resp.setCharacterEncoding("utf-8");
以上两个代码是用于处理servlet的乱码问题的,比如:中文输出为????
二.获得初始化参数
<!--web应用配置的一些初始化参数放在了context对象当中,可以通过servlet中的getInitParameter方法拿到这些初始化参数--> <context-param> <param-name>url</param-name> <param-value>jdbc:myseq://localhost:3306/mybatis</param-value> </context-param> <servlet> <servlet-name>gp</servlet-name> <servlet-class>top.lostyou.servlet.ServletDemo03</servlet-class> </servlet> <servlet-mapping> <servlet-name>gp</servlet-name> <url-pattern>/gp</url-pattern> </servlet-mapping>
我们将初始化的信息放在<context-param>下,就可以通过ServletContext对象中的方法拿到这个初始化的信息
public class ServletDemo03 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context = this.getServletContext(); String url=context.getInitParameter("url"); resp.getWriter().print("url="+url); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } }
这里我们使用的市另外的一个对象方法getServletContext,用于去拿我们在尾部容器下的初始化信息
三.请求转发
<servlet-name>sd4</servlet-name> <servlet-class>top.lostyou.servlet.ServletDemo04</servlet-class> </servlet> <servlet-mapping> <servlet-name>sd4</servlet-name> <url-pattern>/sd4</url-pattern> </servlet-mapping>
public class ServletDemo04 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context = this.getServletContext(); //RequestDispatcher dispatcher=context.getRequestDispatcher("/gp");//将要转发到那个路径上 //dispatcher.forward(req,resp);//调用forward实现请求转发 context.getRequestDispatcher("/gp").forward(req,resp);//简写调用方法 } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } }
这次使用的方法是ServletContext对象的getRequestDispatcher方法,这是请求转发的方法,在这个方法下写出路径后,程序就会在XML中寻找这个路径,然后执行
四.读取资源文件
不管是在Java目录下还是在resources目录下新建properties,都被打包在了一个目录下:classes,我们俗称这个路径为:classpath
读取资源文件我们所需要用到的思路:需要一个文件流
public class SerletDemo05 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { java.io.InputStream is = this.getServletContext().getResourceAsStream("/WEB-IF/classes/db.propertis"); Properties prop =new Properties(); prop.load(is); String user =prop.getProperty("username"); String ped =prop.getProperty("password"); resp.getWriter().print(user+":"+ped); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } }
通过ServletContext对象中的getResourceAsStream的方法,我们需要传入获得路径的地址:相对地址,然后再返回一个io流
在通过Properties对象加载这个流,然后通过properties对象的getProperty方法取出我们的资源
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build>
如果Java目录下我们导出包不成功的话,我们就需要在pom.xml中新增这个依赖就可以了