码迷,mamicode.com
首页 > 其他好文 > 详细

ServletConfig和ServletContext对象

时间:2014-12-31 18:17:50      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

1由于一个WEB应用程序中的所有Servlet都共享同一个对应的ServletContext对象,所以ServletContext对象被称为application对象。是当前web应用的大管家,可以获取到web应用的各个信息

package servlet;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public HelloServlet() {
        super();
        System.out.println("constructor");
    }

    public void init(ServletConfig servletconfig) throws ServletException {
        // ServletConfig常用方法
        String v = servletconfig.getInitParameter("user:");
        // String v = this.getInitParameter("user:");
        // //也行,因为HttpServlet是ServletConfig的实现类,所以可以用this调用该方法
        System.out.println("method1:" + v);
        System.out.println("init");

        // Enumeration<String> en = this.getInitParameterNames();
        Enumeration<String> en = servletconfig.getInitParameterNames();
        while (en.hasMoreElements()) {
            String names = en.nextElement();
            String values = servletconfig.getInitParameter(names);
            System.out.println(names + "-->" + values);
        }
        System.out.println("");
        // ServletContext常用方法
        ServletContext sct = servletconfig.getServletContext();
        String svalue = sct.getInitParameter("driver");
        System.out.println(svalue);
        // 获取当前文件在web服务器上的绝对路径
        String str = sct.getRealPath("/file1.txt");
        System.out.println(str);
        // 获取当前web应用名称
        String conp = sct.getContextPath();
        System.out.println(conp);
        try {
            InputStream is = sct
                    .getResourceAsStream("/WEB-INF/classes/xml.properties"); // 路径是相对当前web应用的路径
            System.out.println(is);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("doGet");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("dopost");
    }

    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
        System.out.println("destroy");
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">
    <context-param>
           <param-name>driver</param-name>
           <param-value>jdbc.odbc</param-value>
    </context-param>
    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>servlet.HelloServlet</servlet-class>
        <!--设置局部初始化参数  -->
        <init-param>
            <param-name>user:</param-name>
            <param-value>wuyj</param-value>
        </init-param>
        <init-param>
            <param-name>password:</param-name>
            <param-value>123</param-value>
        </init-param>
        <load-on-startup>3</load-on-startup>

    </servlet>

    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/servlet/HelloServlet</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

结果:

constructor
method1:wuyj
init
password:-->123
user:-->wuyj

jdbc.odbc
C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\sdf\file1.txt
/sdf
java.io.FileInputStream@1a3551c

ServletConfig和ServletContext对象

标签:

原文地址:http://www.cnblogs.com/yjtm53/p/4196199.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!