Java Web Foundation (ii) (Servlet Authoring and configuration)

Source: Internet
Author: User


The following is the basic servlet knowledge, if you want to lay a good foundation, do not hurry to learn JSP, but from the servlet began to learn. Because the JSP page will eventually be translated as a servlet, mastering the servlet,jsp will also learn half, and in writing JSP pages can also be more accurate positioning errors.

Construction of development environment I will not say, Baidu a search a lot of, say my development environment: MyEclipse + Tomcat 7.0 + JDK 7

Write a basic servlet

We write a servlet in the most primitive way, first encountering building a class, then inheriting HttpServlet, and implementing some of these methods. HttpServlet is located in Servlet-api.jar, the jar package is provided by the famous web container--tomcat, let's take a look at what's in the jar package:

The following is the core class diagram:

through this class diagram we know that the Servlet interface defines the basic behavior, such as the Init (), Destroy (), which is related to the servlet lifecycle, and the service () method that is invoked when providing services And then the abstract class Genericservlet implements the Servlet interface, the ServletConfig interface, encapsulates the incoming ServletConfig instance when the container calls the Init () method, and the service () method is directly labeled abstract without any implementation.

We note that Genericservlet does not regulate any related methods of HTTP, but rather is defined by the HttpServlet that inherits it, which is easy to understand because there is not only HTTP protocol, but also other protocols such as FTP. Then httpservlet inside implemented the service () method, judge the HTTP request mode and handed over to Doget, Dopost and other methods to deal with, so we want to inherit the HttpServlet class need to rewrite Doget or Dopost method.

Registering a servlet program in Web. xml

This is the most traditional way, first we need to write a class and inherit HttpServlet, after rewriting the appropriate method, open Web. XML to register the servlet, here is an example of a registered servlet:

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "3.0" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "><servlet><description> description information for the servlet </ Description><display-name> the name displayed </display-name><servlet-name>simpleservlet</ servlet-name><servlet-class>com.web.simpleservlet</servlet-class></servlet>< servlet-mapping><servlet-name>simpleservlet</servlet-name><url-pattern>/servlet/ Simpleservlet</url-pattern></servlet-mapping></web-app>


The above registration means that the user can pass the ".. /servlet/simpleservlet.view"to request that the Servlet,web container will hand the servlet request to the name Simpleservlet (by < Servlet-name> match), the servlet corresponding class is Com.web.SimpleServlet. The specific process is as follows:

One thing to note here is that when the application starts, all servlet instances are not created by default. When the servlet was first requested, He will be translated by the Web container into a servlet and compiled into a. class file, stored in the specified directory (which is determined by the Web container), and the corresponding instance is created, which is called the servlet initialization, and if the servlet is requested later, the translation and compilation operations are not triggered. If you want to compile and translate when the application starts, you need to specify that the value of <load-on-startup> is greater than 0 (less than or equal to 0 is initialized without program startup) in the <servlet> tag in Web. The size of their values determines the order in which they are initialized.

Registering a servlet program with @webservlet

in Servlet 3.0, you can use annotations (Annotation) to tell the Web container which servlets need to be registered, such as the following example:

This example is equivalent to configuring this in Web. xml:

<servlet><servlet-name>helloservlet</servlet-name><servlet-class>com.web.helloservlet </servlet-class></servlet><servlet-mapping><servlet-name>helloservlet</servlet-name ><url-pattern>/hello.view</url-pattern></servlet-mapping>

We see that using callouts is much simpler than configuring a servlet with Web. XML, @WebSevlet can also be configured with Name, Urlpatterns (default), Loadonstartup, etc. Can be configured in Web. XML, and can be configured using @webservlet.

Although it is cumbersome to use the Web. XML, the configuration in Web. XML overrides the configuration of the @webservlet annotation, so we can use @webservlet as the default value and use the Web. Xml method as a later change setting value.

servlet Advanced Configuration and deployment, put it to the right time later talk about it!


Java Web Foundation (ii) (Servlet Authoring and configuration)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.