Servlet笔记4--ServletConfig接口和ServletContext接口

时间:2021-01-20 09:00:40

ServletConfig接口:

 Servlet笔记4--ServletConfig接口和ServletContext接口

 Servlet笔记4--ServletConfig接口和ServletContext接口

ServletContext接口:

Servlet笔记4--ServletConfig接口和ServletContext接口

Servlet笔记4--ServletConfig接口和ServletContext接口

  

  代码详解:

  (1)web.xml配置文件:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5"> <!-- web.xml文件中配置上下文参数,这些信息被自动封装到ServletContext对象中 -->
<context-param>
<param-name>username</param-name>
<param-value>admin</param-value>
</context-param> <context-param>
<param-name>password</param-name>
<param-value>123</param-value>
</context-param> <servlet>
<servlet-name>a</servlet-name>
<servlet-class>com.bjpowernode.javaweb.servlet.AServlet</servlet-class>
<!-- 初始化参数:被封装到ServletConfig对象中 -->
<init-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>
<init-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/bipowernode</param-value>
</init-param>
<init-param>
<param-name>user</param-name>
<param-value>root</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>123</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>a</servlet-name>
<url-pattern>/a</url-pattern>
</servlet-mapping> <servlet>
<servlet-name>b</servlet-name>
<servlet-class>com.bjpowernode.javaweb.servlet.BServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>b</servlet-name>
<url-pattern>/b</url-pattern>
</servlet-mapping>
</web-app>

    (2) 前端页面:

 <html>
<head>
<title>ServletConfig接口</title>
</head>
<body>
<a href="/prj-servlet-04/a">AServlet</a>
<br><br>
<a href="/prj-servlet-04/b">BServlet</a>
</body>
</html>

    (3)辅助实体类:

 package com.bjpowernode.javaweb.entity;

 public class User {

     private String usercode;
private String username; public String getUsercode() {
return usercode;
} public void setUsercode(String usercode) {
this.usercode = usercode;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} @Override
public String toString() {
return "User [usercode=" + usercode + ", username=" + username + "]";
} }

    (4) Servlet程序:

 package com.bjpowernode.javaweb.servlet;

 import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration; import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; import com.bjpowernode.javaweb.entity.User; public class AServlet implements Servlet { private ServletConfig config; public void init(ServletConfig config) throws ServletException {
this.config = config;
} public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter(); //获取ServletConfig
ServletConfig config = getServletConfig(); //获取Servlet Name
String servletName = config.getServletName();
out.print("<servlet-name>" + servletName + "</servlet-name>" + "<br><br>"); //获取ServletContext,后面讲什么是ServletContext
ServletContext application = config.getServletContext();
out.print("ASevlet's ServletContext = " + application + "<br><br>"); //通过当前Servlet初始化参数的name获取value
/*String driver = config.getInitParameter("driver");
String url = config.getInitParameter("url");
String user = config.getInitParameter("user");
String password = config.getInitParameter("password");*/ //获取当前Servlet所有初始化参数的name,然后再根据name获取value
@SuppressWarnings("unchecked")
Enumeration<String> anames = config.getInitParameterNames();
out.print("AServlet: <br>");
while(anames.hasMoreElements()){
String name = anames.nextElement();
String value = config.getInitParameter(name);
out.print(name + "=" + value);
out.print("<br>");
}
out.print("<br>"); //获取所有上下文(即当前Servlet的四周环境)初始化参数的name,然后再根据name获取value
@SuppressWarnings("unchecked")
Enumeration<String> snames = application.getInitParameterNames();
out.print("ServletContext: <br>");
while(snames.hasMoreElements()) {
String name = snames.nextElement();
String value = application.getInitParameter(name);
out.print(name + "=" + value);
out.print("<br>");
}
out.print("<br>"); //获取文件绝对路径,参数必须是WebRoot的下一级目录开始写
String realPath = application.getRealPath("/WEB-INF/resources/db.properties");
out.print(realPath + "<br>"); //创建User对象
User user = new User();
user.setUsercode("1231");
user.setUsername("jackson"); //向ServletContext范围中存储User数据
application.setAttribute("userObj", user);
application.setAttribute("time", new Date()); } public void destroy() { } public ServletConfig getServletConfig() {
return config;
} public String getServletInfo() {
return null;
} }
 package com.bjpowernode.javaweb.servlet;

 import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; public class BServlet implements Servlet { private ServletConfig config; public void init(ServletConfig config) throws ServletException {
this.config = config;
} public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter(); //init的时候已经获取过了config
out.println("BServlet's ServletConfig = " + config + "<br>"); ServletContext application = config.getServletContext(); Object obj1 = application.getAttribute("userObj");
Object obj2 = application.getAttribute("time");
out.print(obj1 + "<br>");
out.print(obj2 + "<br>");
} public void destroy() { } public ServletConfig getServletConfig() {
return config;
} public String getServletInfo() {
return null;
} }

Servlet笔记4--ServletConfig接口和ServletContext接口