Initialization parameters of web. xml: context-param, init-param

Source: Internet
Author: User

1. initialization parameters

Two parameters can be defined in web. xml:
(1) parameters within the application range are stored in servletcontext and configured in web. xml as follows:

650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212595257-0.gif "/> <context-param>
650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212595257-0.gif "/> <param-name> context/param </param-name>
650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212595257-0.gif "/> <param-value> avalible during application </param-value>
650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212595257-0.gif "/> </context-param>

(2) servlet-specific parameters can only be obtained in the servlet init () method. The configuration in web. xml is as follows:

650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212595257-0.gif "/> <servlet>
650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212595257-0.gif "/> <servlet-name> MainServlet </servlet-name>
650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212595257-0.gif "/> <servlet-class> com. wes. controller. mainServlet </servlet-class>
650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212595257-0.gif "/> <init-param>
650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212595257-0.gif "/> <param-name> param1 </param-name>
650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212595257-0.gif "/> <param-value> avalible in servlet init () </param-value>
650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212595257-0.gif "/> </init-param>
650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212595257-0.gif "/> <load-on-startup> 0 </load-on-startup>
650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212595257-0.gif "/> </servlet>

In servlet, you can use the following code:
 

650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212592c1-13.gif "/> package com. wes. controller;
650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212592c1-13.gif "/>
650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212592c1-13.gif "/> import javax. servlet. ServletException;
650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212592c1-13.gif "/> import javax. servlet. http. HttpServlet;
650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212592c1-13.gif "/>
650) this. width = 650; "id =" Codehighlighter1_156_541_Open_Image "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212592147-18.gif "/> public class MainServlet extends HttpServlet {
650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212591436-19.gif "/>
650) this. width = 650; "id =" Codehighlighter1_187_212_Open_Image "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212592H3-20.gif "/> public MainServlet (){
650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212591436-19.gif "/> super ();
650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212594057-22.gif "/>}
650) this. width = 650; "id =" codehighlighter1_1__539_open_image "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212592H3-20.gif "/> public void init () throws ServletException {
650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212591436-19.gif "/>
650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212591436-19.gif "/> System. out. println ("the following parameters are stored in servletcontext ");
650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212591436-19.gif "/> System. out. println (getServletContext (). getInitParameter ("context/param "));

System. out. println ("the following two parameters param1 are stored in servlet ");

650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212591436-19.gif "/> System. out. println (this. getInitParameter ("param1 "));
650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212594057-22.gif "/>}
650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212594H0-29.gif "/>}
650) this. width = 650; "align =" top "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1212592c1-13.gif "/>

The first parameter can be used in the servlet throughGetServletContext (). getInitParameter ("context/param ")Get
The second parameter can only be passed in the init () method of servletThis. getInitParameter ("param1 ")Fetch

2. Obtain initialization parameters

 

<Context-param>
<Param-name> count </param-name>
<Param-value> 1200 </param-value>
</Context-param>

 

 

<Servlet>
<Description> loginServlet </description>
<Display-name> loginServlet </display-name>
<Servlet-name> loginServlet </servlet-name>
<Servlet-class> loginServlet </servlet-class>
<Init-param>
<Description> system initialization count </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>

 

 

Methods for obtaining parameters and Similarities and Differences

 

String username = request. getParameter ("username ");
Response. setCharacterEncoding ("gb2312 ");
System. out. println ("getServletContext ():" + getServletContext ());
ServletContext context = getServletConfig (). getServletContext ();
String num = context. getInitParameter ("num ");//<Init-param> the value cannot be obtained.
String count = context. getInitParameter ("count ");//<Context-param> the value is obtained.
System. out. println ("num:" + num); // null
System. out. println ("count:" + count); // 1200
System. out. println ("num:" + getServletConfig (). getInitParameter ("num"); // 100Obtained Value
System. out. println ("count:" + getServletConfig (). getInitParameter ("count"); // nullNo value obtained


System. out. println ("request. getSession (). getServletContext ():" + request. getSession (). getServletContext ());
Request. setAttribute ("username", username );
PrintWriter out = response. getWriter ();
Out. println ("the username obtained is:" + username );

 

========================================================== ======================================

1. the URL contains two question marks '?? 'Processing

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

Solution: First/* for full match, distributed to the rest of the servlet for processing, url matching order, refer to: 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.   } 

 

 

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.