There are two types of initialization parameters for configuring the Servlet:
1. Use the annotation method @WebServlet
To set the initialization parameters of the Servlet in the annotation, you need to configure the values of name, urlPatterns, and inintParams
name is equivalent to <servlet-name> in the web.xml configuration file
urlPatterns is equivalent to <url-pattern>, used to specify the URL of the servlet
inintParams is equivalent to <init-pattern>, used to specify a set of servlet initialization parameters
@WebServlet(
name="/initParameterServlet", //name="/自定义"
urlPatterns={"/initParameterServlet"}, //urlPatterns={"/自定义"}
initParams={
@WebInitParam
(name="email",value = "123qq.com") //name="设置的参数名" value="设置的参数值"
})
If you need to configure multiple sets of parameters, you can do the following:
@WebServlet(
name="/InitParameterServlet",
urlPatterns={"/initParameterServlet"},
initParams={
@WebInitParam
(name="email",value = "[email protected]"),
@WebInitParam
(name="phone",value = "123456789"),
@WebInitParam
(name="参数名",value = "参数值")
})
The complete code of the servlet is as follows:
public class InitParameterServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletConfig config = this.getServletConfig();
String value = config.getInitParameter("email");
String value1 = config.getInitParameter("phone");
PrintWriter writer = resp.getWriter();
writer.println("your email is:"+value);
writer.println("your phone munber is:"+value1);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
Input in the address bar: http://hostname:port number/project name/value set by urlPatterns
The implementation is as follows:
2. Configure in the Web.xml file
<servlet-class>: The class address of the registered servlet, that is: package name.class name
Configuration initialization parameters are in
<context-param>
<param-name>Attribute name</param-name>
<param-value>Attribute value</param-value>
</context-param>
Write as many <context-param>s as you need to configure....</context-param>
Add the following code section to the Web.xml configuration file:
<servlet>
<servlet-name>InitParameterServlet1</servlet-name>
<servlet-class>com.servlet.InitParameterServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>InitParameterServlet1</servlet-name>
<url-pattern>/initParameterServlet1</url-pattern>
</servlet-mapping>
<!-- 配置InitParameterServlet1整个web应用的初始化参数 -->
<context-param>
<param-name>email</param-name>
<param-value>123qq.com</param-value>
</context-param>
The complete code of the servlet is as follows:
public class InitParameterServlet1 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = getServletContext();
String initParameter = servletContext.getInitParameter("email");
String initParameter1 = servletContext.getInitParameter("phone");
//后台
System.out.println("your email is:" + initParameter);
System.out.println("your phone number is:" + initParameter1);
//浏览器
PrintWriter writer = resp.getWriter();
writer.println("your email is" + initParameter);
writer.println("your phone number is" + initParameter1);
}
}
Same effect as above
For reference only, please forgive me if there are any deficiencies.