Java Web-servlet (4) Three ways to develop Servlets, configuration servlet details, servlet lifecycle (1)

Source: Internet
Author: User
Tags java web

First, implement the Servlet interface

Understanding The life cycle of a servlet by Implementing a servlet interface

(1) Create a Web app in a canonical form (i.e. Create a Web app in the WebApps directory )

Create a Web app

(2) create a . Java(the Java file to implement the Servlet interface) under the classes directory

Create a folder Myfirstservlet.java

The code is as follows:

Package com.focus;

Import javax.servlet.*;

Import javax.servlet.http.*;

Import java.io.*;

public class Myfirstservlet implements Servlet

{

The servlet engine calls the init method exactly before it is placed into the service after the servlet is instantiated . (Initialize the servlet, load the servlet into memory)

the function will only be called once

public void init (ServletConfig config) throws servletexception{

}

the servlet engine calls this method to allow the servlet to respond to requests. This method cannot be called until the Servlet is initialized successfully. (Business logic code will be written in this function.)

Theservice is not called when it is uninstalledand is not initialized, and the rest can be called repeatedly

public void Service (ServletRequest request, servletresponse response)

Throws Servletexception, ioexception{

System.out.println ("Hello world!");

Response.getwriter (). println ("Hello world!" +new java.util.Date ());

}

uninstalling a servlet

public void Destroy () {

}

get configuration information for the servlet

Public ServletConfig Getservletconfig () {

return null;

}

Plain Text servlet Information

Public String Getservletinfo () {

return null;

}

}

An error occurred after compilation and the imported package could not be found.

PS: When using dos compile, the Java has a package that is required to enter the command line format as

Javac-d. Name.java

(3) configuration CLASSPATH is specified to Servlet-api.jar in the lib directory of Tomcat (which has Servlet-api.jar the one that was introduced above javax.servlet.*; javax.servlet.http.*; ).

When configuring CLASSPATH , specify to a specific . Jar

Can not set *.jar , can only write each, so the general project will configure a startup script, specifically calculate all the jar Package, and then import the boot environment ...

Restart cmd.exe, and then recompile. Can be

(4) Configuring and deploying Servlets

<servlet>

<servlet-name>MyFirstServlet</servlet-name>

<display-name>MyFirstServlet</display-name>

<servlet-class>com.focus.MyFirstServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>MyFirstServlet</servlet-name>

<url-pattern>/My</url-pattern>

</servlet-mapping>

(5) Final effect:



Add: When Tomcat starts , a server.xml is started first , and when the servlet mapping cannot find the corresponding,tomcat start directly on the error.

Second, Configuring and Deploying Servlets

Because the client accesses the resources in the Web server through the URL address , the Servlet program, if it wants to be accessed by the outside world, must The servlet program maps to a URL address.

<servlet>

<servlet-name> register name, customize The name of a servlet

<servlet-name>MyFirstServlet</servlet-name>

<display-name>MyFirstServlet</display-name>

</servlet-class> Specifies in which package the servlet class is put in package / package /.../ Class (Package name + class name)

<servlet-class>com.focus.MyFirstServlet</servlet-class>

</servlet>

<servlet-mapping>servlet Mapping (for one already registered)

<servlet-mapping>

<servlet-name> Customize The name of a servlet , but this name means that when the browser enters /my this web Resource, the name is mapped to the corresponding name, and the servlet 's class is located

<servlet-name>MyFirstServlet</servlet-name>

<url-pattern> Web Resource name entered by the browser

<url-pattern>/My</url-pattern>

</servlet-mapping>

A well-registered servlet can be mapped multiple times

such as:<servlet>

<servlet-name>1</servlet-name>

<display-name>1</display-name>

<servlet-class>com.focus.MyFirstServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>1</servlet-name>

<url-pattern>/My</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>1</servlet-name>

<url-pattern>/Servlet/My</url-pattern>

</servlet-mapping>

<servlet-name>1</servlet-name>

<url-pattern>/Servlet/My.html</url-pattern>

</servlet-mapping>

That is, the input Web resource is either /my or /servlet/my or /servlet/my.html can be accessed to Com.focus.MyFirstServlet This class

When mapping a servlet , it can be multiple layers, and the suffix named html is not necessarily an HTML file.

When you make a Servlet map, you can use the wildcard character

There are two types of formats:

First format *. extensions such as:*.html *.ABC

The second formatends with "/" at thesame time as "/*" such as:/abc/* /*

Example:1. <url-pattern>/*</url-pattern>

2. <url-pattern>/abc</url-pattern>

3. <url-pattern>/abc/*</url-pattern>

4. <url-pattern>*.gogo</url-pattern>

Match 2 when the servlet name of the URL of The browser is entered /abc

Match 1 when the servlet name of the URL of The browser is entered /hellobaby

Match 3 when the servlet name of the URL of The browser is entered /abc/hellobaby

Match 1 when the servlet name of the URL of The browser is entered /hello.gogo

Matching Criteria:

(1) high matching selection map that one.

(2)*. the extension has the lowest priority

Load-on-startup element Tag

<servlet>

<servlet-name>1</servlet-name>

<display-name>1</display-name>

<servlet-class>com.focus.MyFirstServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

1) The Load-on-startup element flags whether the container loads the servlet at startup ( instantiates and invokes its init () method ) .

2) its value must be an integer indicating The order in which the servlet should be loaded

2) When the value is 0 or greater than 0 , indicates that the container loads and initializes the servlet when the application starts ;

3) When the value is less than 0 or unspecified, the container will not load until the servlet is selected.

4) The lower the value of a positive number, the higher the precedence of the servlet , and the more loaded it will be when the app starts.

5) at the same time, the container will be loaded in its own order of choice.

So,<load-on-startup>x</load-on-startup>, the value of 1in x , 2,3,4,5 represents the priority, not the start delay time.

add: How to better manage the Web app in Tomcat , which is entering Tomcat manager

Reload: Reload the Web App , meaning reload .

Stop: stops the Web app

Benefit: Handle a Web application individually, without operating the entire server

Java Web-servlet (4) Three ways to develop Servlets, configuration servlet details, servlet lifecycle (1)

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.