web. What are the main configuration files in XML and what do they represent?

This is the back-end small class of the monastery. Each article is shared from

[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

[what are the main configuration files in web.xml and what do they represent?]

More content can be added

Hello, I'm the 2nd student of Shanghai Branch of it Academy. I'm an honest and kind java programmer.

Today, I would like to share with you some knowledge points that may be used in task2 task on the official website of the Academy:

web. What are the main configuration files in XML and what do they represent?

WEB. The main configurations in XML and what they represent_ Tencent video

1. Background introduction

1、 Web Characteristics (rules) of XML file:

There must be one and only one root node, which is case sensitive, labels are not nested, and must be paired.

2、 Web Functions of XML file:

web. The XML file is used to initialize configuration information, such as welcome page, servlet, servlet mapping, filter, listener, startup loading level, etc.

When your web project does not use these, you can not use the web XML file to configure your application.

3、 Web What XML can do:

On the web How many tag elements are defined in the schema file of XML, web The tag elements defined in its schema file can appear in XML, and it can have the defined functions.

And web The tags defined in the XML schema file are not fixed, and the schema file can also be changed. Generally speaking, with the development of web When upgrading the version of XML schema file, the functions defined in it will become more and more complex, that is, there will be more and more types of tag elements, but some are not commonly used. We just need to remember some commonly used ones.

2. Knowledge analysis

The following is a list of web Main tags in XML file and their meanings:

1、 Welcome page

When visiting a website, the first page you see by default is called the welcome page. Generally, the home page acts as the welcome page. In general, we will be on the web Specify the welcome page in the XML.

But the web XML is not a necessary file for the web, there is no web XML, the website can still work normally. But after the function of the website becomes more complex, the web XML is really very useful, so the dynamic web project created by default has a web. XML under the WEB-INF folder XML file.

2、 Naming and customizing URLs

Name and customize the URL for the servlet and JSP files. The custom URL depends on naming, and the naming must be in front of the custom URL.

3、 Custom initialization parameters

Customize the initialization parameters of servlet, JSP and context, and then obtain these parameter values in servlet, JSP and context.

4、 Set filter

The filter in servlet implements javax servlet. The server-side program of filter interface is mainly used to filter character coding and make some business logic judgments. It works as long as you're on the web The XML file configures the client request to be intercepted, which will help you intercept the request. At this time, you can uniformly set the coding for the request or response (request and response) to simplify the operation; At the same time, it can also make logical judgment, such as whether the user has logged in, whether he has permission to access the page, etc. It starts with the start of your web application. It is initialized only once. After that, it can intercept relevant requests. It will be destroyed only when your web application stops or redeploys.

5、 Set listener

Servlet listener, which implements javax servlet. The server-side program of servletcontextlistener interface is also started with the start of the web application. It is initialized only once and destroyed with the stop of the web application. The main function is to add some initialization contents, set some basic contents, such as some parameters or fixed objects, etc.

3. Frequently asked questions

1.web. The XML file generally includes the configuration of servlet, spring, filter and listener. So in what order are they loaded?

2. The difference between URL pattern configured as "/" and "/ *"

4. Solutions

1. The loading sequence is: listener > > Filter > > servlet > > spring

The loading order affects the call to the spring bean.

For example, if the filter needs beans, but the loading order is to load the filter first and then spring, the bean in the initialization operation in the filter is null;

But the loading order is different from them on the web The order in the XML file is independent.

2. The difference between / and / *:

@H_ 502_ 85 @ / will not match * JSP, i.e.: * JSP does not enter the dispatcherservlet class of spring.

@H_ 502_ 85@ /*

Match * JSP, when the JSP view is returned, the dispatcher servlet class of spring will be entered again, resulting in no corresponding controller, so a 404 error will be reported.

5. Coding practice

6. Expand thinking

In addition to these common tag elements, you can also go to the web What are the tag elements added to XML? What are the functions of those tag elements?

1. Set the session expiration time, where the time is in minutes. If you set a 60 minute timeout:

Session code

2. Configuration error page

Function: when 404 error occurs in the system, jump to the error handling page

wait

7. References

Document 1: http://blog.csdn.net/zuoluoboy/article/details/4213053

Document 2: http://www.cnblogs.com/hxsyl/p/3435412.html

Document 3: http://blog.csdn.net/LF_Software_Studio/article/details/8256429

Document 4: http://blog.csdn.net/lijia791541916/article/details/50381012

8. More discussion

Q: after a link is opened, what is the sequence of listeners, interceptors and filters?

A: this should be the loading sequence. The listener will be triggered when the container is started. The filter will execute before the request to the action, and the interceptor will execute after the request to the action.

Q: if we don't use the web XML, where should it be configured?

A: let's take servlet as an example, not on the web If configured in XML. There are the following solutions

Servlet 3.0 deployment description file web The top-level tag of XML has a metadata complete attribute, which specifies whether the current deployment description file is complete. If it is set to true, the container will only rely on the deployment description file and ignore all annotations during deployment (at the same time, the scanning of web-fragment.xml will be skipped, that is, pluggability support will be disabled); if this property is not configured or set to false, annotation support (and pluggability support) will be enabled.

There are annotations such as @ webservlet.

@Webservlet is used to declare a class as a servlet. The annotation will be processed by the container during deployment. The container will deploy the corresponding class as a servlet according to the specific attribute configuration. This annotation has some common attributes given in the following table (all the following attributes are optional, but vlaue or urlpatterns are usually required, and they cannot coexist. If they are specified at the same time, the value of value is usually ignored)

Thank you for watching!

That's all for today's sharing. You are welcome to like, forward, leave messages and make bricks~

Skill tree It Academy

"We believe that everyone can become an engineer. From now on, find a senior brother to introduce you, control your learning rhythm, and stop being confused on the way to learning.".

Here is the skill tree In it academy, thousands of senior brothers have found their own learning route here. Learning is transparent and growth is visible. Senior brothers have 1-to-1 free guidance. Come and study with me~

My invitation code:, or you can click this link directly:

Author: idiot, Wei Wei link: https://www.jianshu.com/p/22dbfb678fde Source: the copyright of Jianshu Jianshu belongs to the author. Please contact the author for authorization and indicate the source for any form of reprint.

It communication group 565734203 will discuss and communicate with you

Here is the skill tree · it Academy: nofollow "> https://www.jnshu.com , beginners switch to the gathering place of the Internet

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>