首页 > 代码库 > servletConfig的应用
servletConfig的应用
在Servlet的配置文件中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数。
当servlet配置了初始化参数后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,将ServletConfig对象传递给servlet。进而,程序员通过ServletConfig对象就可以得到当前servlet的初始化参数信息。
阅读ServletConfig API,并举例说明该对象的作用:
获得字符集编码
获得数据库连接信息
获得配置文件 ,查看struts案例的web.xml文件
view plaincopy to clipboardprint?
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="3.0"
- 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_3_0.xsd">
- <display-name></display-name>
- <servlet>
- <servlet-name>DemoServlet</servlet-name>
- <servlet-class>cn.csdn.web.servlet.DemoServlet</servlet-class>
- <init-param>
- <param-name>version</param-name>
- <param-value>2.0</param-value>
- </init-param>
- <init-param>
- <param-name>characterEncoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- <init-param>
- <param-name>driver</param-name>
- <param-value>com.mysql.jdbc.driver</param-value>
- </init-param>
- <init-param>
- <param-name>url</param-name>
- <param-value>jdbc:mysql://localhost:3306/3g</param-value>
- </init-param>
- <init-param>
- <param-name>user</param-name>
- <param-value>root</param-value>
- </init-param>
- <init-param>
- <param-name>password</param-name>
- <param-value>0904</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>DemoServlet</servlet-name>
- <url-pattern>/demo/*</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>DemoServlet</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- </welcome-file-list>
- </web-app>
view plaincopy to clipboardprint?
- package cn.csdn.web.servlet;
- import java.io.IOException;
- import java.io.PrintWriter;
- 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 DemoServlet extends HttpServlet {
- /**
- * @author 宋鹤贤
- */
- private static final long serialVersionUID = 1L;
- /* private ServletConfig config; */
- @Override
- public void destroy() {
- // TODO Auto-generated method stub
- super.destroy();
- System.out.println("销毁");
- }
- public DemoServlet() {
- super();
- // TODO Auto-generated constructor stub
- System.out.println("创建对象");
- }
- @Override
- public void init() throws ServletException {
- // TODO Auto-generated method stub
- super.init();
- System.out.println("初始化");
- }
- @Override
- public void init(ServletConfig config) throws ServletException {
- super.init(config);
- System.out.println("--------------" + config);
- /* this.config=config; */
- }
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- this.doPost(request, response);
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- /* 设置编码 */
- request.setCharacterEncoding("UTF-8");
- String version = this.getServletConfig().getInitParameter("version");
- String characterEncoding = this.getServletConfig().getInitParameter(
- "characterEncoding");
- Enumeration<String> enumer = this.getServletConfig()
- .getInitParameterNames();
- // 遍历
- while (enumer.hasMoreElements()) {
- String name = enumer.nextElement();
- String value =http://www.mamicode.com/ this.getServletConfig().getInitParameter(name);
- System.out.println(name + "===" + value);
- }
- /* 获取一下PrintWriter对象 */
- PrintWriter out = response.getWriter();
- out.print("versin=" + version + "\tcharacterEncoding="
- + characterEncoding);
- }
- }
代码如上:我们发布到tomcat中,然后开启tomcat服务器,在浏览器中输入http://localhost:8080/20111018/xxx.do,界面返回结果如下:
servletConfig的应用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。