Java project java project web.xml Document details document details

java project web.xml Document details


  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




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



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

  <description>load test System Information</description>
  <display-name>test  Server</display-name>
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		


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();
        		chain.doFilter(request, response);
        		request.getRequestDispatcher("/login.jsp").forward(httpRequest, httpResponse); 


   <! -- 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. >

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

web.xml Additional configuration sections included in


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


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




    < 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 -- >

Tags: xml Session Java JSP

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