servlet初始化参数的两种⽅法(1.配置l⽂件,2.使⽤注释)
⼀、servlet初始化参数
使⽤标签初始化的参数是被应⽤程序中所有的servlet所共享。但是有时候我们需要为某⼀个特定的servlet配置参数,这个时候我们就需要使⽤servlet初始化参数。下边介绍两种servlet参数初始化的⽅法。
(1)配置l⽂件
param name
在l⽂件中为当前web应⽤添加两个名为admin-email和admin-tel的ServletContext初始化参数,在ConfigDemoServlet中获取这两个参数的参数值,使⽤ServletContext对象将admin-email和admin-tel的参数值写⼊⽇志⽂件中,并查看⽇志⽂件。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="/2001/XMLSchema-instance" xmlns="java.sun/xml/ns/javaee" xsi:schemaLocation="java.sun/x ml/ns/javaee java.sun/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>homework01</display-name>
<!--配置servlet -->
<servlet>
<servlet-name>ConfigDemoServlet</servlet-name>
<servlet-class&figDemo.ConfigDemoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ConfigDemoServlet</servlet-name>
<url-pattern>/configDemo.do</url-pattern>
</servlet-mapping>
<!--配置整个web应⽤的初始化参数-->
<context-param>
<param-name>admin-email</param-name>
<param-value>123456@qq</param-value>
</context-param>
<context-param>
<param-name>admin-tel</param-name>
<param-value>1234567890</param-value>
</context-param>
</web-app>
(2)使⽤注解完成servlet初始化参数:
编写ConfigDemoServlet程序,通过注释为其添加初始化参数email、telephone,在Servlet中读取该servlet的名称和初始化参数email、telephone,并将这两个参数的值在响应在页⾯上。
下边代码所演⽰的是注释进⾏servlet的参数初始化:
@WebServlet(
name ="ConfigDemoServlet",
urlPatterns="/configDemo.do",
initParams ={
@WebInitParam(name="email", value="telephone")
})
⼆、完整代码如下:
figDemo;
import java.io.FileReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ConfigDemoServle
* servlet的注释初始化
*/
@WebServlet(
name ="ConfigDemoServlet",
urlPatterns="/configDemo.do",
initParams ={
@WebInitParam(name="email", value="telephone")
})
public class ConfigDemoServlet extends HttpServlet {
private static final long serialVersionUID =1L;
/
/ServletContext 的应⽤:作为整个web范围中的共享数据
private ServletContext servletContext;
/**
* @see HttpServlet#HttpServlet()
*/
public ConfigDemoServlet(){
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
//获取servlet参数
ServletConfig config =ServletConfig();
Enumeration<String> enumeration = InitParameterNames();
String name =(String) Element();//获取对应的值
String value11 = InitParameter(name);//利⽤名字获取值
//将信息打印在界⾯上
//获取l⽂件中的初始化参数值,并且显⽰在页⾯上
this.servletContext = ServletContext();
String adminEmail = InitParameter("admin-email");//利⽤名字获取值
String adminTel = InitParameter("admin-tel");//利⽤名字获取值
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub
doGet(request, response);
}
}
三、页⾯显⽰结果

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。