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

ServletConfig、ServletContext属性遍历

时间:2015-09-23 08:59:05      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

可以进行属性遍历;

package com.stono.servlet;

import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.RequestDispatcher;
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 TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public TestServlet() {
        System.out.println("com.stono.servlet.TestServlet constructor called");
    }

    @Override
    public void init() throws ServletException {
        System.out.println("com.stono.servlet.TestServlet.init() called");
    }

    @Override
    public void destroy() {
        System.out.println("com.stono.servlet.TestServlet.destroy() called");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
            IOException {
        ServletContext context = req.getServletContext();
        Enumeration<String> attributeNames = context.getAttributeNames();
        int i = 0;
        while (attributeNames.hasMoreElements()) {
            String nextElement = attributeNames.nextElement();
            System.out.print(++i + ":" + nextElement + ":");
            Object attribute = context.getAttribute(nextElement);
            System.out.println(attribute);
        }
        ServletConfig config2 = getServletConfig();
        String name = config2.getServletName();
        System.out.println(name);
        Enumeration<String> names = config2.getInitParameterNames();
        i = 0;
        while (names.hasMoreElements()) {
            String nextElement = names.nextElement();
            System.out.print(++i + ":" + nextElement + ":");
            String initParameter = config2.getInitParameter(nextElement);
            System.out.println(initParameter);
        }
        RequestDispatcher dispatcher = req.getRequestDispatcher("test2.do");
        dispatcher.forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
            IOException {
    }
}

xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>TestBootWeb</display-name>
    <servlet>
        <servlet-name>TestServlet</servlet-name>
        <servlet-class>com.stono.servlet.TestServlet</servlet-class>
        <init-param>
            <param-name>name</param-name>
            <param-value>123</param-value>
        </init-param>
    </servlet>
    <servlet>
        <servlet-name>TestServlet2</servlet-name>
        <servlet-class>com.stono.servlet.TestServlet2</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>TestServlet</servlet-name>
        <url-pattern>/test.do</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>TestServlet2</servlet-name>
        <url-pattern>/test2.do</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

 

ServletConfig、ServletContext属性遍历

标签:

原文地址:http://www.cnblogs.com/stono/p/4831174.html

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