1. Servlet请求和响应、生命周期实验、初始化参数
时间: 2024-06-12 21:03:54 浏览: 107
Servlet请求和响应实验:
1. 编写一个Servlet程序,实现接收一个文本参数,将其转换为大写并输出到浏览器。
```java
@WebServlet("/toUpperCase")
public class ToUpperCaseServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String input = request.getParameter("input");
String output = input.toUpperCase();
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>转换结果</title></head>");
out.println("<body>");
out.println("<h1>转换结果:</h1>");
out.println("<p>输入:" + input + "</p>");
out.println("<p>输出:" + output + "</p>");
out.println("</body>");
out.println("</html>");
}
}
```
2. 在web.xml中配置Servlet映射。
```xml
<servlet>
<servlet-name>ToUpperCaseServlet</servlet-name>
<servlet-class>com.example.ToUpperCaseServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ToUpperCaseServlet</servlet-name>
<url-pattern>/toUpperCase</url-pattern>
</servlet-mapping>
```
3. 访问http://localhost:8080/toUpperCase?input=hello,可以看到输出结果为HELLO。
生命周期实验:
1. 编写一个Servlet程序,实现在Servlet初始化和销毁时输出日志。
```java
@WebServlet("/lifecycle")
public class LifecycleServlet extends HttpServlet {
public void init() {
System.out.println("Servlet 初始化");
}
public void destroy() {
System.out.println("Servlet 销毁");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>生命周期</title></head>");
out.println("<body>");
out.println("<h1>Servlet 生命周期</h1>");
out.println("<p>请查看控制台输出</p>");
out.println("</body>");
out.println("</html>");
}
}
```
2. 在web.xml中配置Servlet映射。
```xml
<servlet>
<servlet-name>LifecycleServlet</servlet-name>
<servlet-class>com.example.LifecycleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LifecycleServlet</servlet-name>
<url-pattern>/lifecycle</url-pattern>
</servlet-mapping>
```
3. 访问http://localhost:8080/lifecycle,可以看到控制台输出如下:
```
Servlet 初始化
```
4. 点击浏览器的“返回”按钮,再次访问http://localhost:8080/lifecycle,可以看到控制台输出如下:
```
Servlet 初始化
```
5. 停止Tomcat服务器,可以看到控制台输出如下:
```
Servlet 销毁
```
初始化参数实验:
1. 在web.xml中配置Servlet初始化参数。
```xml
<servlet>
<servlet-name>InitParamServlet</servlet-name>
<servlet-class>com.example.InitParamServlet</servlet-class>
<init-param>
<param-name>username</param-name>
<param-value>admin</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>123456</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>InitParamServlet</servlet-name>
<url-pattern>/initParam</url-pattern>
</servlet-mapping>
```
2. 编写一个Servlet程序,实现读取Servlet初始化参数并输出到浏览器。
```java
@WebServlet("/initParam")
public class InitParamServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = getInitParameter("username");
String password = getInitParameter("password");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>初始化参数</title></head>");
out.println("<body>");
out.println("<h1>初始化参数:</h1>");
out.println("<p>用户名:" + username + "</p>");
out.println("<p>密码:" + password + "</p>");
out.println("</body>");
out.println("</html>");
}
}
```
3. 访问http://localhost:8080/initParam,可以看到输出结果为:
```
初始化参数:
用户名:admin
密码:123456
```
4. 修改web.xml中的初始化参数,重新访问http://localhost:8080/initParam,可以看到输出结果已经改变。
阅读全文