I. Introduction of the Servlet
1. What is a servlet:
A set of specifications (interfaces)provided by Sun to handle the dynamic resources of client requests and responses to the browser . The essence of the servlet, however, 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. How to create a servlet
implementation steps:1, create a class to implement Servlet interface 2, overwrite the methods that have not been implemented---focus on implementing Service Method 3, configuring the servlet in Web. xml
However, in the actual development , we generally do not implement the Servlet interface directly, because there are too many methods to implement, it is generally the creation of class inheritance HttpServlet.
implementation steps:1, create class inherits HttpServlet Class 2, overwrite doget and dopost 3, configure servlet in Web. xml
Code Demo:
Packagecom.oracle.demo01;Importjava.io.IOException;ImportJavax.servlet.Servlet;ImportJavax.servlet.ServletConfig;Importjavax.servlet.ServletException;Importjavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse;ImportJavax.servlet.http.HttpServletResponse; Public classMyservletImplementsservlet{@Override Public voiddestroy () {//TODO auto-generated Method Stub} @Override PublicServletConfig Getservletconfig () {//TODO auto-generated Method Stub return NULL; } @Override PublicString Getservletinfo () {//TODO auto-generated Method Stub return NULL; } @Override Public voidInit (ServletConfig arg0)throwsservletexception {//TODO auto-generated Method Stub} @Override//requests: Request response: Response Public voidService (ServletRequest arg0, Servletresponse arg1)throwsservletexception, IOException {httpservletresponse res=(HttpServletResponse) arg1; Res.setcharacterencoding ("UTF-8"); Res.getwriter (). Write ("Hello"); }}
Servlet templates:
Package${enclosing_package};Importjava.io.IOException;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public class${primary_type_name}extendsHttpServlet { Public voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {response.getwriter (). Write ("Hello Dandan ..."); } Public voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {doget (request, response); }}
Servlet Template Creation steps:
Add a servlet template
1.windows→perferences→java→editor→templates→new ...
Enter in the 2.Name column: Servlet,description is the cue bar, you can enter it casually.
3. Copy the template Ctrl + V in the pattern bar
4. Click Ok→ok to complete the template additions
5. Create a new servlet file
6.alt+a Delete all the original code
7. Enter the servlet and press ALT + "/" to point out all the content and complete the add
Ii. APIs (methods) in the servlet
(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) The life cycle of the servlet (emphasis)
1) Creation of Servlets
The object is created the first time the servlet is accessed when it is started by default (server)
2) When the servlet is destroyed
When the server shuts down, it destroys the
3) The method that must be executed on each visit
Service (ServletRequest req, servletresponse res) method
Small Exercise: 10 visits to Xxxservlet, Init (), Destory (), service (), Doget (), DoPost () How many times have they been executed? How many request objects are created? Response Create several?
Are once, once, 10 times, 10 times, 10 times, 10 times, 10, 10.
(3) method of HttpServlet class:
1) init ()
2) doget (httpservletrequest request,httpservletresponse response)
3) DoPost (httpservletrequest request,httpservletresponse response)
4) Destroy ()
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 character
3) Extension match format: *. extension name
Note: the second and third kind do not mix /aaa/bbb/*.abcd (wrong)
2. Server initiates instantiation of servlet creation
When the servlet is 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 is started.
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 of the resources in the Web application are responsive to Servlets, including static resources .
4, welcome page (in the Address bar access path only input to the server layer, no specific JSP and servlet access to the page)
If all deleted, also will jump to the Index.jap page, because the large Web. xml file is configured.
If not deleted, you will find the first eligible Welcome page from the top ,
Iv. ServletContext Objects (emphasis)
1. What is a ServletContext object?
ServletContext represents an environment context variable for a Web application , and ServletContext internally encapsulates the information for that Web application. ServletContext object A Web application has only one, and through this large ServletContext external application, represents all the servlet objects.
Summary: A web App has only one ServletContext object and multiple Servlet objects.
ServletContext the life cycle of an object:
Create: created when the Web app is loaded. (The server starts or publishes the web app (assuming that the server is up)) (the server is add, and then it is created after you click Start)
Destroy:The Web App is uninstalled. (server off, remove the Web App) (server stop or remove)
2. How to Get ServletContext object
2.1 ServletContext ServletContext = Config.getservletcontext ();
2.2 ServletContext ServletContext = This.getservletcontext ();
3, the role of ServletContext
3.1 Getting the global initialization parameters in the Web App
3.2 Getting the absolute path to any resource in the Web App (important)
method:String Path = Context.getrealpath (relative to the Web app's relative address);
Write a relative, get an absolute path
3.3 ServletContext used as a domain object (important)
What is a domain object? What is a domain?
the area where the data is stored is the domain object
ServletContext domain objects: The entire Web application (all Web resources are free to access data in the ServletContext domain, data can be shared)
Common methods for domain objects:
Setatrribute (String name,object obj); formatting data into a field, in key-value pairs
GetAttribute (String name); gets the value stored by key from the domain
RemoveAttribute (String name); Delete the saved data
Java Phase III Learning (11, Servlet basics, methods in Servlets, configuration of Servlets, ServletContext objects)