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
- @Override
- public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
- throws IOException, ServletException {
- HttpServletRequest httpRequest = null;
- if (request instanceof HttpServletRequest) {
- httpRequest = (HttpServletRequest) request;
- } else {
- throw new RuntimeException("not http request");
- }
- String queryString = httpRequest.getQueryString();
- if (queryString != null && queryString.startsWith("?")) {
- request.getRequestDispatcher("/taobao").forward(httpRequest, response);
- } else {
- chain.doFilter(httpRequest, response);
- }
- }