• 周六. 10 月 5th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Java Web Learning (5) — ServletContext object / read configuration file through ServletContext obje

King Wang

1 月 3, 2022

JavaWeb Study ( 5、 … and )—ServletContext object / adopt servletContext Object read configuration file

01 ServletContext object

WEB Container at startup , It will WEB The application creates a corresponding `ServletContext` object , It represents the current web application . One WEB All in the application `Servlet` Share the same `ServletContext` object ,`Servlet` Objects can pass through `ServletContext` Object to communicate .ServletContext Objects are often referred to as `context Domain object `.

Case study 1: Two Servlet adopt ServletContext Object to realize data sharing

public class ServletTest01 extends javax.servlet.http.HttpServlet{

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

// Set the encoding format , Avoid random code 
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
// adopt ServletContext Set properties 
ServletContext servletContext = this.getServletContext();
String name = "Curry";
servletContext.setAttribute("username",name);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

doGet(req,resp);
}
}
public class ServletTest02 extends HttpServlet {

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

// Avoid random code 
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
// obtain ServletContext object 
ServletContext servletContext = this.getServletContext();
// adopt ServletContext get attribute 
String username = (String) servletContext.getAttribute("username");
PrintWriter writer = resp.getWriter();
writer.print("Servlet1 The message is :"+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">
<servlet>
<servlet-name>ServletTest01</servlet-name>
<servlet-class>com.hooi.demo01.ServletTest01</servlet-class>
</servlet>
<servlet>
<servlet-name>ServletTest02</servlet-name>
<servlet-class>com.hooi.demo01.ServletTest02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletTest01</servlet-name>
<url-pattern>/test01</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ServletTest02</servlet-name>
<url-pattern>/test02</url-pattern>
</servlet-mapping>
</web-app>

Be careful : You need to run /test02 Set properties and run /test03 get attribute

Running results
 Insert picture description here

Case 2 : adopt servletContext Object to read the site profile

stay src New under the directory resources Catalog , stay resources Create under directory db.properties The contents are as follows :

driver=com.mysql.jdbc.Driver
username=root
password=123456
url=jdbc:mysql://localhost:3306/smbms
public class ServletTest03 extends HttpServlet {

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

// Set encoding 
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/document");
// adopt ServletContext obtain out The real path under the directory 
//out In the catalog classes The catalog is equivalent to src, therefore resources It's just classes The next level of the 
String realPath = this.getServletContext().getRealPath("/WEB-INF/classes/resources/db.properties");
// obtain properies object 
Properties properties = new Properties();
// Get file input stream 
FileInputStream fileInputStream = new FileInputStream(realPath);
// Used to properties Object load file stream 
properties.load(fileInputStream);
// Get configuration details 
String driver = properties.getProperty("driver");
String username = properties.getProperty("username");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
// Response returns profile details 
PrintWriter writer = resp.getWriter();
writer.println("driver:"+driver);
writer.println("username:"+username);
writer.println("password:"+password);
writer.println("url:"+url);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

doPost(req,resp);
}
}
<servlet>
<servlet-name>ServletTest03</servlet-name>
<servlet-class>com.hooi.demo02.ServletTest03</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletTest03</servlet-name>
<url-pattern>/test03</url-pattern>
</servlet-mapping>

Running results :
 Insert picture description here

发表回复