(1)新建一个类名为DemoServletConfig01的Servlet,该Servlet路径映射为/config01 (2)新建一个类名为DemoServletConfig02的Servlet,该Servlet路径映射为/config02 (3)分别在这两个Servlet中获取Servlet配置对象ServletCnfig并打印出来看它们是否是同一个对象 (4)给Servlet类DemoServletConfig01配置初始化参数:{"name":"张三","sex":"男"},并分别在上面两个Servlet中获取它们
时间: 2023-12-06 12:03:38 浏览: 61
好的,这里是代码实现:
DemoServletConfig01.java
```
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class DemoServletConfig01 extends HttpServlet {
private ServletConfig servletConfig;
private String name;
private String sex;
public void init(ServletConfig servletConfig) throws ServletException {
this.servletConfig = servletConfig;
this.name = servletConfig.getInitParameter("name");
this.sex = servletConfig.getInitParameter("sex");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html><head><title>DemoServletConfig01</title></head>");
out.println("<body><h1>这是DemoServletConfig01的内容!</h1>");
out.println("<p>ServletConfig对象是否相同:" + (servletConfig == getServletConfig()) + "</p>");
out.println("<p>姓名:" + name + "</p>");
out.println("<p>性别:" + sex + "</p>");
out.println("</body></html>");
out.close();
}
}
```
DemoServletConfig02.java
```
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class DemoServletConfig02 extends HttpServlet {
private ServletConfig servletConfig;
private String name;
private String sex;
public void init(ServletConfig servletConfig) throws ServletException {
this.servletConfig = servletConfig;
this.name = servletConfig.getInitParameter("name");
this.sex = servletConfig.getInitParameter("sex");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html><head><title>DemoServletConfig02</title></head>");
out.println("<body><h1>这是DemoServletConfig02的内容!</h1>");
out.println("<p>ServletConfig对象是否相同:" + (servletConfig == getServletConfig()) + "</p>");
out.println("<p>姓名:" + name + "</p>");
out.println("<p>性别:" + sex + "</p>");
out.println("</body></html>");
out.close();
}
}
```
web.xml
```
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>config01</servlet-name>
<servlet-class>DemoServletConfig01</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>张三</param-value>
</init-param>
<init-param>
<param-name>sex</param-name>
<param-value>男</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>config01</servlet-name>
<url-pattern>/config01</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>config02</servlet-name>
<servlet-class>DemoServletConfig02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>config02</servlet-name>
<url-pattern>/config02</url-pattern>
</servlet-mapping>
</web-app>
```
说明:
1. DemoServletConfig01和DemoServletConfig02都实现了init()方法,在这个方法中获取ServletConfig对象,并获取初始化参数。
2. 在doGet()方法中,展示ServletConfig对象是否相同,并展示获取到的初始化参数。
3. 在web.xml中给DemoServletConfig01配置了两个初始化参数,其中一个参数为“name”,值为“张三”,另一个参数为“sex”,值为“男”。
4. 当请求访问/config01时,容器会调用DemoServletConfig01的doGet()方法,展示ServletConfig对象是否相同,并展示获取到的初始化参数;同理,当请求访问/config02时,容器会调用DemoServletConfig02的doGet()方法,展示ServletConfig对象是否相同(这里不同于DemoServletConfig01),并展示没有获取到初始化参数。
希望这些代码可以帮助您解决问题。
阅读全文