(6)新建一个类名为DemoServletContext02的Servlet,该Servlet路径映射为/context02 (7)分别在这两个Servlet中获取web应用上下文对象ServletContext并打印出来看它们是否是同一个对象 (8)给web应用配置初始化参数:{"loginid":"tom","loginpwd":"123"},并分别在上面两个Servlet中获取它们 (9)在DemoServletContext01中将键值对{"msg":"登录成功!"}存入ServletContext域对象中,在DemoServletContext02去获取上面这个键值对并输出 (10)分别在src、web及WEB-INF目录下新建三个配置文件jdbc.properties,文件内容分别为: jdbc01.driver=com.mysql.jdbc.Driver jdbc01.url=jdbc:mysql:///localhost/jdbc jdbc01.username=root jdbc01.password=123 jdbc02.driver=com.mysql.jdbc.Driver jdbc02.url=jdbc:mysql:///localhost/jdbc jdbc02.username=root jdbc02.password=123 jdbc03.driver=com.mysql.jdbc.Driver jdbc03.url=jdbc:mysql:///localhost/jdbc jdbc03.username=root jdbc03.password=123 (11)分别获取上面三个文件夹中的文件并输出其内容
时间: 2024-03-14 22:43:06 浏览: 81
6. 新建一个类名为 DemoServletContext02 的 Servlet,该 Servlet 路径映射为 /context02。
```java
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.io.IOException;
public class DemoServletContext02 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
System.out.println("DemoServletContext02 context: " + context);
}
}
```
在 web.xml 中设置 Servlet 路径映射:
```xml
<servlet>
<servlet-name>DemoServletContext02</servlet-name>
<servlet-class>DemoServletContext02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DemoServletContext02</servlet-name>
<url-pattern>/context02</url-pattern>
</servlet-mapping>
```
7. 分别在这两个 Servlet 中获取 web 应用上下文对象 ServletContext 并打印出来看它们是否是同一个对象
```java
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.io.IOException;
public class DemoServletContext01 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
System.out.println("DemoServletContext01 context: " + context);
}
}
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.io.IOException;
public class DemoServletContext02 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
System.out.println("DemoServletContext02 context: " + context);
}
}
```
在控制台中输出的结果应该是一样的,因为它们获取的是同一个 web 应用上下文对象。
8. 给 web 应用配置初始化参数:{"loginid":"tom","loginpwd":"123"},并分别在上面两个 Servlet 中获取它们。
在 web.xml 中添加以下配置:
```xml
<context-param>
<param-name>loginid</param-name>
<param-value>tom</param-value>
</context-param>
<context-param>
<param-name>loginpwd</param-name>
<param-value>123</param-value>
</context-param>
```
在 DemoServletContext01 和 DemoServletContext02 中获取这些初始化参数:
```java
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.io.IOException;
public class DemoServletContext01 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
String loginid = context.getInitParameter("loginid");
String loginpwd = context.getInitParameter("loginpwd");
System.out.println("DemoServletContext01 loginid: " + loginid);
System.out.println("DemoServletContext01 loginpwd: " + loginpwd);
}
}
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.io.IOException;
public class DemoServletContext02 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
String loginid = context.getInitParameter("loginid");
String loginpwd = context.getInitParameter("loginpwd");
System.out.println("DemoServletContext02 loginid: " + loginid);
System.out.println("DemoServletContext02 loginpwd: " + loginpwd);
}
}
```
9. 在 DemoServletContext01 中将键值对 {"msg":"登录成功!"} 存入 ServletContext 域对象中,在 DemoServletContext02 去获取上面这个键值对并输出。
```java
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.io.IOException;
public class DemoServletContext01 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
context.setAttribute("msg", "登录成功!");
System.out.println("DemoServletContext01: 键值对 {"msg":"登录成功!"} 已存入 ServletContext 域对象中。");
}
}
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.io.IOException;
public class DemoServletContext02 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
String msg = (String) context.getAttribute("msg");
System.out.println("DemoServletContext02: " + msg);
}
}
```
10. 分别在 src、web 及 WEB-INF 目录下新建三个配置文件 jdbc.properties,文件内容分别为:
jdbc01.driver=com.mysql.jdbc.Driver
jdbc01.url=jdbc:mysql:///localhost/jdbc
jdbc01.username=root
jdbc01.password=123
jdbc02.driver=com.mysql.jdbc.Driver
jdbc02.url=jdbc:mysql:///localhost/jdbc
jdbc02.username=root
jdbc02.password=123
jdbc03.driver=com.mysql.jdbc.Driver
jdbc03.url=jdbc:mysql:///localhost/jdbc
jdbc03.username=root
jdbc03.password=123
11. 分别获取上面三个文件夹中的文件并输出其内容。
```java
import java.io.InputStream;
import java.util.Properties;
public class DemoProperties {
public static void main(String[] args) {
// 读取 src 目录下的 jdbc.properties 文件
InputStream in1 = DemoProperties.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties prop1 = new Properties();
try {
prop1.load(in1);
System.out.println("jdbc01.driver: " + prop1.getProperty("jdbc01.driver"));
System.out.println("jdbc01.url: " + prop1.getProperty("jdbc01.url"));
System.out.println("jdbc01.username: " + prop1.getProperty("jdbc01.username"));
System.out.println("jdbc01.password: " + prop1.getProperty("jdbc01.password"));
} catch (IOException e) {
e.printStackTrace();
}
// 读取 web 目录下的 jdbc.properties 文件
InputStream in2 = DemoProperties.class.getClassLoader().getResourceAsStream("../jdbc.properties");
Properties prop2 = new Properties();
try {
prop2.load(in2);
System.out.println("jdbc02.driver: " + prop2.getProperty("jdbc02.driver"));
System.out.println("jdbc02.url: " + prop2.getProperty("jdbc02.url"));
System.out.println("jdbc02.username: " + prop2.getProperty("jdbc02.username"));
System.out.println("jdbc02.password: " + prop2.getProperty("jdbc02.password"));
} catch (IOException e) {
e.printStackTrace();
}
// 读取 WEB-INF 目录下的 jdbc.properties 文件
InputStream in3 = DemoProperties.class.getClassLoader().getResourceAsStream("../../jdbc.properties");
Properties prop3 = new Properties();
try {
prop3.load(in3);
System.out.println("jdbc03.driver: " + prop3.getProperty("jdbc03.driver"));
System.out.println("jdbc03.url: " + prop3.getProperty("jdbc03.url"));
System.out.println("jdbc03.username: " + prop3.getProperty("jdbc03.username"));
System.out.println("jdbc03.password: " + prop3.getProperty("jdbc03.password"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
阅读全文