web.xml的初始化參數:context-param,init-param

一、初始化參數

web.xml裏面能夠定義兩種參數:
(1)application範圍內的參數,存放在servletcontext中,在web.xml中配置以下:java

<context-param>
           <param-name>context/param</param-name>
           <param-value>avalible during application</param-value>
  </context-param>

(2)servlet範圍內的參數,只能在servlet的init()方法中取得,在web.xml中配置以下:web

<servlet>
    <servlet-name>MainServlet</servlet-name>
    <servlet-class>com.wes.controller.MainServlet</servlet-class>
    <init-param>
       <param-name>param1</param-name>
       <param-value>avalible in servlet init()</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
</servlet>

在servlet中能夠經過代碼分別取用:
 app

package com.wes.controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

public  class MainServlet  extends HttpServlet  {

    public MainServlet() {
        super();
      }

    public void init() throws ServletException {
        
         System.out.println("下面的參數是存放在servletcontext中的");
         System.out.println(getServletContext().getInitParameter("context/param"));

            System.out.println("下面的兩個參數param1是在servlet中存放的");ide

         System.out.println(this.getInitParameter("param1"));
       }

}

第一種參數在servlet裏面能夠經過getServletContext().getInitParameter("context/param")獲得
第二種參數只能在servlet的init()方法中經過this.getInitParameter("param1")this

二、初始化參數獲取

 

<context-param>
  <param-name>count</param-name>
  <param-value>1200</param-value>
  </context-param>
url

 

 

<servlet>
    <description>loginServlet</description>
    <display-name>loginServlet</display-name>
    <servlet-name>loginServlet</servlet-name>
    <servlet-class>loginServlet</servlet-class>
    <init-param>
      <description>系統初始化數</description>
      <param-name>num</param-name>
      <param-value>100</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>loginServlet</servlet-name>
    <url-pattern>/loginServlet</url-pattern>
  </servlet-mapping>spa

 

 

獲取參數的方法以及異同cdn

 

String username=request.getParameter("username");
  response.setCharacterEncoding("gb2312");
  System.out.println("getServletContext():"+getServletContext());
  ServletContext context=getServletConfig().getServletContext();
  String num=context.getInitParameter("num");// <init-param> 獲取不到值
  String count=context.getInitParameter("count");//<context-param> 獲取到值

  System.out.println("num:"+num);//null
  System.out.println("count:"+count);//1200
  System.out.println("num:"+getServletConfig().getInitParameter("num"));//100獲取到值
  System.out.println("count:"+getServletConfig().getInitParameter("count"));//null獲取不到值

  
  System.out.println("request.getSession().getServletContext():"+request.getSession().getServletContext());
  request.setAttribute("username", username);
  PrintWriter out=response.getWriter();
  out.println("獲取的用戶名是:"+username);xml

 

================================================================blog

一、URL中含有兩個問號'?? '的處理

http://a.tbcdn.cn/??s/kissy/1.2.0/kissy-min.js,p/global/1.0/global-min.js,p/fp/2012/core.js,p/fp/2012/fp/module.js,p/fp/2012/fp/util.js,p/fp/2012/fp/directpromo.js?t=2012080620120925.js

解決:先以/*作全匹配,再分配給其他servlet進行處理, url的匹配順序,參考:http://tianya23.blog.51cto.com/1081650/1001568

  
  
           
  
  
  1. @Override 
  2.   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
  3.           throws IOException, ServletException { 
  4.       HttpServletRequest httpRequest = null
  5.       if (request instanceof HttpServletRequest) { 
  6.           httpRequest = (HttpServletRequest) request; 
  7.       } else { 
  8.           throw new RuntimeException("not http request"); 
  9.       } 
  10.       String queryString = httpRequest.getQueryString(); 
  11.       if (queryString != null && queryString.startsWith("?")) { 
  12.           request.getRequestDispatcher("/taobao").forward(httpRequest, response); 
  13.       } else { 
  14.           chain.doFilter(httpRequest, response); 
  15.       } 
  16.   } 
相關文章
相關標籤/搜索