一个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);
}
}
<?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="4.0"
metadata-complete="true">
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.godairo.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>getusername</servlet-name>
<servlet-class>com.godairo.servlet.GetServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getusername</servlet-name>
<url-pattern>/getusername</url-pattern>
</servlet-mapping>
</web-app>
测试访问结果:先输入hello保存数据,然后输入getusername读取
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String url = context.getInitParameter("url");
response.getWriter().print(url);
}
????????
@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
读取资源文件会用类加载去做
这里是打基础
|