servlet bottom principle

servlet bottom principle

Servlet container startup process
Tomcat7 also began to support embedded features, adding a startup class org.apache.catalina.startup.Tomcat. Creating an instance object and calling the start method makes it easy to start Tomcat. We can also add and modify Tomcat configuration parameters through this object, such as dynamically adding Context, Servlet, and so on. Let’s use this Tomcat class to manage a new Context container. Let’s select the examples Web project that comes with Tomcat7 and see how it is added to this Context container.

Listing 2. Adding a Web project to Tomcat

Tomcat tomcat = getTomcatInstance();
File appDir = new File(getBuildDirectory(), “webapps/examples”);
tomcat.addWebapp(null, “/examples”, appDir.getAbsolutePath());
tomcat.start();
ByteChunk res = getUrl(“http://localhost:” + getPort() +
“/examples/servlets/servlet/HelloWorldExample”);
assertTrue(res.toString().indexOf(“

Hello World!

”) > 0);
在这里插入图片描述
Servlet architecture
We know that Java Web applications run on the Servlet specification, so how does the Servlet itself work? Why design such an architecture.
在这里插入图片描述
As you can see from the above figure, the Servlet specification is based on these classes. The three classes that are actively associated with the Servlet are ServletConfig, ServletRequest, and ServletResponse. These three classes are passed to the servlet through the container, where ServletConfig is passed to the Servlet when the Servlet is initialized, and the latter two are passed when the Servlet is called when the request is reached. We are very clear about the meaning of ServletRequest and ServletResponse running in Servlet, but what is the value of ServletConfig and ServletContext for Servlet? A closer look at the methods declared in the ServletConfig interface reveals that these methods are used to get some configuration properties of the servlet, and these configuration properties may be used when the servlet is running. And what does ServletContext do? The servlet’s mode of operation is a typical “handshake-type interactive” mode of operation. The so-called “handshake-type interactive” means that two modules usually prepare a trading scenario in order to exchange data. This scenario follows the transaction process until the transaction is completed. The initialization of this trading scenario is customized based on the parameters specified by this transaction object. These specified parameters will usually be a configuration class. So the check-in, the trading scenario is described by the ServletContext, and the custom parameter set is described by ServletConfig. ServletRequest and ServletResponse are the specific objects to be interacted with. They are usually used as transportation tools to deliver interaction results.

How does Servlet work?
We have already seen how the servlet is loaded, how the servlet is initialized, and the architecture of the servlet. The question now is how it is called.

When a user initiates a request from the browser to the server, it usually contains the following information: http://hostname: port /contextpath/servletpath, hostname and port are used to establish a TCP connection with the server, and the following URL is used to select The child container in the server serves the user’s request. How does the server get to the correct servlet container based on this URL?

This is easy to solve in Tomcat 7.0, because this mapping work has a special class, this is org.apache.tomcat.util.http.mapper, this class holds all the children in Tomcat's Container container. Container information, when the org.apache.catalina.connector. Request class enters the Container container, the mapper will set the host and context containers to the request's mappingData property based on the hostnane and contextpath of the request. So when the Request enters the Container container, it has already determined which sub-container to access.
How the Request request reaches the final Wrapper container, we are now aware of how the request reached the correct Wrapper container, but the request to the final servlet has to complete some steps, must implement the Filter chain, and notify you on the web. The listener defined in xml.

Next, we need to execute the Servlet service method. Normally, our own defined servlet does not directly implement the javax.servlet.servlet interface, but instead inherits the simpler HttpServlet class or the GenericServlet class. We can choose Cover the appropriate methods to achieve the work we are going to do.

Servlets have indeed been able to do all of the work for us, but today's web applications rarely implement all of the interactive pages directly with servlets, but with a more efficient MVC framework. The basic principle of these MVC frameworks is to map all requests to a servlet and then implement the service method, which is the entry point to the MVC framework.

When the servlet is removed from the servlet container, it indicates that the Servlet's life cycle is over. At this point, the servlet's destroy method will be called to do some finishing work.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值