请求转发-ServletConfig对象的使用案例
通过ServletConfig对象读取配置文件初始化参数
案例实现效果
1、设计思路(实现原理)
1) 创建ReqServlet3和ReqServlet4两个servlet类,
2) 在ReqServlet3中,将获取的用户id放在request域中,并请求转发到另一个ReqServlet4中,
3) 在ReqServlet4中,获取用户id,然后查询该用户信息,并响应,
4) 在web.xml中配置初始化参数
5)通过浏览器访问ReqServlet3,展示查询信息
创建ReqServlet3和ReqServlet4两个servlet类
package com.gd.servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author LFXIANG
* description TODO
* @title ReqServlet3
* date 2024/3/28 21:51
*/
public class ReqServlet3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletConfig servletConfig = this.getServletConfig();
// 取的用户id
String id = servletConfig.getInitParameter("id");
// 找到用户id
if (id!=null){
// 将获取的用户id放在request域中
req.setAttribute("id",id);
// 请求转发到另一个ReqServlet4中
req.getRequestDispatcher("/ReqServlet4").forward(req,resp);
}else {
req.getRequestDispatcher("无用户信息");
}
}
}
package com.gd.servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author LFXIANG
* description TODO
* @title ReqServlet4
* date 2024/3/28 21:53
*/
public class ReqServlet4 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取用户id,
Object id =req.getAttribute("id");
// 有用户id
if (id!=null){
ServletConfig servletConfig = this.getServletConfig();
// 然后查询该用户信息,并响应,
String username = servletConfig.getInitParameter("username");
String age = servletConfig.getInitParameter("age");
resp.setContentType("text/html;charset=utf-8");
resp.getWriter().write("查询编号为"+id+"的用户信息如下:<br>用户名为:"+username+"<br>用户年龄:"+age);
}else {
resp.getWriter().write("没有找到用户");
}
}
}
配置web.xml,设置初始化参数
<?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>ReqServlet3</servlet-name>
<servlet-class>com.gd.servlet.ReqServlet3</servlet-class>
<init-param>
<param-name>id</param-name>
<param-value>001</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>ReqServlet4</servlet-name>
<servlet-class>com.gd.servlet.ReqServlet4</servlet-class>
<init-param>
<param-name>username</param-name>
<param-value>张三</param-value>
</init-param>
<init-param>
<param-name>age</param-name>
<param-value>18</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ReqServlet3</servlet-name>
<url-pattern>/ReqServlet3</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ReqServlet4</servlet-name>
<url-pattern>/ReqServlet4</url-pattern>
</servlet-mapping>
</web-app>
运行测试
更多关于ServletConfig对象的内容可以看我文章
——ServletConfig读取配置文件与ServletContext实现数据共享使用介绍——