JavaEE中关于ServletConfig的小结
在Servlet的配置文件中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数。当servlet配置了初始化参数后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,ServletConfig对象传递给servlet。进而,程序员通过ServletConfig对象就可以得到当前servlet的初始化参数信息。
示例代码如下:
package com.yyz.servletconfig;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletConfigDemo1 extends HttpServlet {
ServletConfig config;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取指定的初始化参数
String value = config.getInitParameter("xxx");
response.getOutputStream().write(value.getBytes());
//获取所有的初始化参数
Enumeration e = cofig.getInitParameterNames();
while(e.hasMoreElements()){
String name = (String) e.nextElement();
value = config.getInitParameter(name);
response.getOutputStream().write((name+"="+value+"<br/>").getBytes());
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
@Override
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
}
相应的web.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>ServletConfigDemo1</servlet-name>
<servlet-class>com.yyz.servletconfig.ServletConfigDemo1</servlet-class>
<init-param>
<param-name>xxx</param-name>
<param-value>yyy</param-value>
</init-param>
<init-param>
<param-name>name</param-name>
<param-value>yyz</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>yyy</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ServletConfigDemo1</servlet-name>
<url-pattern>/servlet/ServletConfigDemo1</url-pattern>
</servlet-mapping>
</web-app>
测试结果如下:
在上面的代码中,ServletConfigDemo1对象中有一个ServletConfig对象,其实这是不必要的。因为ServletConfigDemo1继承了HttpServlet,HttpServlet又继承了GenericServlet 。GenericServlet 已经在内部维护了一个ServletConfig对象。相关实现如下:
public abstract class GenericServlet
implements Servlet, ServletConfig, java.io.Serializable
{
… …
private transient ServletConfig config;
public ServletConfig getServletConfig() {
return config;
}
}
因而我们可以通过我们写的Servlet对象的getServletConfig()方法直接拿到ServletConfig对象,示例代码如下:
package com.yyz.servletconfig;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletConfigDemo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String value = this.getServletConfig().getInitParameter("name");
System.out.println(value);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>ServletConfigDemo2</servlet-name>
<servlet-class>com.yyz.servletconfig.ServletConfigDemo2</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>yyz</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ServletConfigDemo2</servlet-name>
<url-pattern>/servlet/ServletConfigDemo2</url-pattern>
</servlet-mapping>
</web-app>
上一篇:java枚举使用详细介绍及实现
栏 目:JAVA代码
本文标题:JavaEE中关于ServletConfig的小结
本文地址:http://www.codeinn.net/misctech/8468.html
阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机