zoukankan      html  css  js  c++  java
  • ServletContext2

    ------------ContextServlet.java--------------节选--

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      request.setCharacterEncoding("GB2312");
      response.setContentType("text/html;charset=gb2312");
      PrintWriter out = response.getWriter();
      ServletContext context = getServletConfig().getServletContext();
      //获得工程相关信息
      out.println("获得了工程的相对路径:"+context.getRealPath("/")+"<br>");
      out.println("获取了Tomcat的信息: "+context.getServerInfo()+"<br>");
      out.println("获取了JNDI:"+context.getResource("/")+"<br>");
      out.println("获取了源程序的相对路径:"+context.getResourcePaths("/")+"<br>");
      //getInitParameter方法
      String date = context.getInitParameter("date");
      String myclass = context.getInitParameter("class");
      out.println("date is -- "+date+"<br>");
      out.println("class is -- "+myclass+"<br>");
      //集合方法
      Enumeration e = context.getInitParameterNames();
      while(e.hasMoreElements()){
       String pName=(String) e.nextElement();
       String pValue=context.getInitParameter(pName);
       out.println("<br><b>"+pName+"</b>&nbsp;");
       out.println("----- "+pValue+"<br>");
      }
     }

    -------------------web.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>test4-03</display-name>
      <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>
      <context-param>
       <param-name>date</param-name>
       <param-value>200</param-value>
      </context-param>
      <context-param>
       <param-name>class</param-name>
       <param-value>102</param-value>
      </context-param>
      <servlet>
       <description>This is the description of my J2EE component</description>
       <display-name>This is the display name of my J2EE component</display-name>
       <servlet-name>ContextServlet</servlet-name>
       <servlet-class>com.ContextServlet</servlet-class>
       <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
       <servlet-name>ContextServlet</servlet-name>
       <url-pattern>/servlet/ContextServlet</url-pattern>
      </servlet-mapping>
    </web-app>

  • 相关阅读:
    wex5 实战 框架拓展之2 事件派发与data刷新
    wex5 实战 框架拓展之1 公共data组件(Data)
    wex5 实战 HeidiSQL 导入Excel数据
    wex5 实战 手指触屏插件 hammer的集成与优劣
    wex5 实战 登陆帐号更换与用户id一致性
    wex5 实战 用户点评与提交设计技巧
    wex5 实战 省市县三级联动与地址薄同步
    wex5 实战 wex5与js的组件关系与执行顺序(父子与先后)
    wex5 实战 单页模式下的多页面数据同步
    [BZOJ]4237: 稻草人
  • 原文地址:https://www.cnblogs.com/daimazhang/p/5364994.html
Copyright © 2011-2022 走看看