Java project java project web.xml Document details document details

java project web.xml Document details

summary

  1. When a web project starts, the web container will read it web.xml file
  2. Then create a servlet context that all parts of the web project will share
  3. The container will be converted to a key value pair and handed over to the servletContext

web.xml The main configuration sections included in:

  • ServletContext
  • context-param
  • listener
  • filter
  • servlet

Load order of main configuration sections

Configure the loading order between sections and their web.xml The order in the file is irrelevant. That is, the filter will not be loaded first because it is written in front of the listener.
The final conclusion is: servlet context - > context param - > listener - > filter - > Servlet

Load order in main configuration section

  1. For a certain type of configuration section, the loading order is that web.xml The sequence in the file is loaded first and then
  2. Take the filter (servlet) as an example, web.xml Of course, multiple filters can be defined in. A configuration section related to the filter is filter mapping. It must be noted here that for the filter and filter mapping configuration sections with the same filter name, filter mapping must appear after the filter, otherwise when the filter mapping is parsed, it corresponds to Filter name is not defined yet. When the web container starts to initialize each filter, it is initialized according to the order in which the filter configuration section appears. When the request resource matches multiple filter mapping, the filter intercepting resource calls the doFilter() method according to the order in which the filter mapping configuration section appears.

Main configuration section details

ServletContext

context-param

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext*.xml</param-value>
 </context-param>

For the parameters set here, you can use the following methods in the servlet

getServletContext().getInitParameter("contextConfigLocation") 

listener

The listener listener is mainly implemented javax.servlet.ServletContextListener The server-side program of the interface, like the filter, starts with the start of the web application. It only needs to be initialized once and can be monitored later. Generally, it is mainly used to listen for property change information events in ServletContext, HttpSession and HttpServletSession

<listener>
  <description>load test System Information</description>
  <display-name>test  Server</display-name>
  <listener-class>com.test.servlet.ServerContext</listener-class>
</listener>
public class ServerContext implements ServletContextListener{
	
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub	
		ServletContext servletcontext = servletcontextevent.getServletContext();	
	}
	public void contextInitialized(ServletContextEvent arg0) {
		// TODO Auto-generated method stub		
	}
}
	

filter

<filter>
  <filter-name>login</filter-name>
  <filter-class>com.test.servlet.LoginFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>login</filter-name>
  <url-pattern>*.xul</url-pattern>
</filter-mapping>
public class LoginFilter implements Filter {
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest)request;
        HttpServletResponse httpResponse = (HttpServletResponse)response;
        String url=httpRequest.getRequestURL().toString();
        String uri = httpRequest.getRequestURI();
        if(true){
        		chain.doFilter(request, response);
        	}else{
        		request.getRequestDispatcher("/login.jsp").forward(httpRequest, httpResponse); 
        	}
	}
}

servlet

<servlet>
   <servlet-name>ShoppingServlet</servlet-name>
   <servlet-class>com.myTest.ShoppingServlet</servlet-class>
   <load-on-startup>0</load-on-startup>
   <! -- load on startup: Specifies the order in which the servlet is loaded when the web application is started. Its value must be an integer. If its value is a negative integer or the element does not exist, the container will load the servlet when it is called. If the value is a positive integer or zero, the servlet will be loaded and initialized when the container is configured. The container must ensure that the smaller value is loaded first. If the values are equal, the container can automatically choose who loads first. >
 </servlet>
 <servlet-mapping>
    <servlet-name>ShoppingServlet</servlet-name>
    <url-pattern>/shop/ShoppingServlet</url-pattern>
 </servlet-mapping>

public class StopcarcaseListSlt extends HttpServlet {
@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
		
}

web.xml Additional configuration sections included in

session-config

<session-config>
  <! -- used to define the validity period of all session s of this web site, in minutes -- >
  <session-timeout>9999</session-timeout>  
</session-config>

mime-mapping

<mime-mapping>  
  <extension>htm</extension><!--Name of extension-->
  <mime-type>text/html</mime-type><!--MIME format-->
</mime-mapping>

welcom-file-list

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
</welcom-file-list>

error-page

<error-page>
    < error code > 404 < / error code > <! -- specify error code -- >
    <exception-type> JAVA.lang.Exception < / exception type > <! -- specify a JAVA exception type -- >
    < location > / error404. JSP < / location > <! -- specify the relevant resource path in the web site -- >
</error-page>

Tags: xml Session Java JSP

Posted on Thu, 18 Jun 2020 04:51:09 -0400 by johncox