Javaweb Core Technology Servlet

Source: Internet
Author: User

First, the servlet introduction 1. What is a servlet

The Servlet runs the Java applet on the server side, and Sun provides a set of specifications (interfaces) to handle the client requests and respond to the dynamic resources of the browser. But the essence of the servlet is Java code, which dynamically outputs content to the client through the Java API

Servlet specification: contains three technical points

1) servlet Technology

2) Filter Technology---Filters

3) Listener Technology---Listener

2. Servlet Quick Start

Implementation steps:

1) Create a class to implement the Servlet interface

2) Overwrite Methods---service methods that have not been implemented

3) configuration of the servlet in Web. xml

However, in actual development, we do not directly implement the Servlet interface, because there are too many methods to overwrite, we generally create class inheritance HttpServlet

Implementation steps:

1) Create class inheritance HttpServlet class

2) cover Doget and Dopost

3) configuration of the servlet in Web. xml

Paint describes the entire access process:

Ii. servlet API (Life cycle) (1) methods in the Servlet interface

1) init (servletconfig config)

When to execute: When a Servlet object is created

ServletConfig: Represents the configuration information for the Servlet object

2) Service (ServletRequest request,servletresponse response)

When to execute: Every request will be executed

ServletRequest: Information on behalf of the request that the ServletRequest internal encapsulation is HTTP request

Servletresponse: Represents the response that is supposed to encapsulate the information of the response

3) Destroy ()

When to execute: The servlet executes when it is destroyed

(2) Methods of the HttpServlet class

1) init ()

2) doget (httpservletrequest request,httpservletresponse response)

3) DoPost (httpservletrequest request,httpservletresponse response)

4) Destroy ()

(3) The life cycle of the servlet (interview questions)

1) When the servlet is created

Create this object the first time the servlet is accessed by default

2) When the servlet is destroyed

The server shuts down the servlet and destroys it.

3) The method that must be executed for each visit

Service (ServletRequest req, servletresponse res) method

Question: 10 accesses to the Xxxservlet, init (), Destory (), service (), Doget (), DoPost () How many times are there execution forces? How many request objects are created? Response Create several?

Third, the configuration of the servlet 1. Basic Configuration

Where Url-pattern is configured:

1) The exact Match access resource is identical to the configured resource to access the

2) directory matching format:/virtual directory. /* * represents any

3) Extension match format: *. extension name


Note: The second and third kind do not mix /aaa/bbb/*.abcd (wrong)

2. The server initiates the instantiation of the servlet configuration

When the servlet was created: the default is created on first access

Why is the default?

When the configuration of the servlet is added, a configuration <load-on-startup>servlet object is created when the server starts

3. Default servlet

The Url-pattern can be configured with a/, which means that the servlet is the default servlet

What is the default servlet?

When you access a resource address where all servlets do not match, the default servlet is responsible for handling

In fact, all the resources in the Web application are responsive to the servlet, including static resources

4. Welcome page

Four, ServletContext object 1. What is a ServletContext object

The ServletContext represents an environment (context) object for a Web application, and the ServletContext object's internal encapsulation is information about the Web application, and ServletContext object A Web application has only one

Question: How many servlet objects does a Web application have? ----multiple

ServletContext the life cycle of an object?

Create: The Web App is loaded (the server starts or publishes the web app (assuming that the server is up))

Destroy: The Web App is uninstalled (the server shuts down to remove the web app)

2. How to get ServletContext objects

1) ServletContext ServletContext = Config.getservletcontext ();

2) ServletContext ServletContext =this.getservletcontext ();

3. The role of ServletContext (1) Get the global initialization parameters of the Web application

Configuring initialization parameters in Web. xml

Get parameters from the context object

(2) Get the absolute path to any resource in the Web App ( important important)

Method: String Path = Context.getrealpath (relative to the Web app's relative address);

(3) ServletContext is a domain object ( important important)

What is a domain object? What is a domain?

The area where the data is stored is the domain object

ServletContext domain Object scope: The entire web should (all Web resources can freely access the data in the ServletContext domain, the data can be shared)

Common methods for domain objects:

Setatrribute (String name,object obj);

GetAttribute (String name);

RemoveAttribute (String name);

Javaweb Core Technology Servlet

Related Article

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.