About Servlet
A servlet running a Java applet on the server is a set of specifications (interfaces)provided by Sun to handle client requests and dynamic resources that respond to the browser . The essence of a 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
Servlet Quick Start Steps
1) Create a class to implement the Servlet interface
2) Overwrite methods that have not been implemented---focus on implementing service methods
3) Servler configuration on Web. xml
Package Com.oracle.demo01;import Java.io.ioexception;import Javax.servlet.servlet;import Javax.servlet.servletconfig;import Javax.servlet.servletcontext;import Javax.servlet.servletexception;import Javax.servlet.servletrequest;import Javax.servlet.servletresponse;public class Myservlet implements servlet {@ Overridepublic void Destroy () {System.out.println ("Dstory is Running");} @Overridepublic void init (ServletConfig arg0) throws Servletexception {System.out.println ("Init is Running");// Gets the servlet's namestring servletname = Arg0.getservletname (); System.out.println (servletname);//Gets the initialization parameter string value = Arg0.getinitparameter ("url"); System.out.println (value);//Get ServletContext object ServletContext sc = Arg0.getservletcontext (); System.out.println (SC);} @Overridepublic void Service (ServletRequest arg0, Servletresponse arg1) throws Servletexception, IOException { SYSTEM.OUT.PRINTLN ("service is Running");} @Overridepublic ServletConfig Getservletconfig () {//TODO auto-generated method Stubreturn null;} @Overridepublic StrinG Getservletinfo () {//TODO auto-generated method Stubreturn null;}}
However, in the actual development , we generally do not directly implement the Servlet interface , because there are too many methods to rewrite, we generally create class inheritance HttpServlet
Implementation steps:1) Create class inheritance HttpServlet class
2) overriding the Doget and Dopost methods
3) configuration of the servlet in Web. XML
The entire access process for the servlet
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 message that the response is supposed to encapsulate as an HTTP response
3)destroy ()
When to execute:the servlet executes when it is destroyed
(2)methods of 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
Default (when server starts) the object is created the first time the servlet is accessed
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 request,servletresponse response) method
the Xxxservlet made 10 visits, how many times did Init (), Destory (), service (), Doget (), DoPost () execute altogether? How many request objects are created? Response Create several?
A: Init (), Destroy (), doget (), DoPost () Execute 1 times, service () executes 10 request and response objects create 10
Servlet configuration
Basic Configuration
How to configure Url-pattern :
1) The exact match access resource is exactly the same as the configured resource to be accessed
2) directory matching format:/virtual Directory ... */* represents arbitrary, that is, as long as the contents of the virtual directory on the full write, the following content can be arbitrary
3) Extension matching format: *. extension as long as the following extension is correct, the preceding content can be arbitrary
Note: The second and third types do not mix/AAA/BBB/*.ABCD
The server initiates the instantiation of the servlet configuration
When the servlet was created: default is created at the first access time
Default? When the servlet is configured, a configuration <load-on-startup> servlet object is created when the server is started
Default servlet
The Url-pattern can be configured with a / , which means that the servlet is the default servlet
When you access a resource address where all servlets do not match, the default servlet takes care of the fact that all the resources in the Web application respond to the servlet, including static resources.
Welcome interface
ServletContext Object
ServletContext represents an environment (context) object for a Web application, and the ServletContext object encapsulates information about the Web application,ServletContext object, and only one Web application. You can have more than one Servlet object in a web App
ServletContext the life cycle of an object
Create: The Web App is loaded (the server starts or publishes the Web app (provided the server is up))
Destroy: The Web App is uninstalled (the server shuts down to remove the web app)
Get ServletContext Object
1) ServletContext ServletContext =config.getservletcontext ();
public void init (ServletConfig config) throws servletexception {
Get ServletContext Object
ServletContext Context=config.getservletcontext ();
}
2)ServletContext servletcontext =this.getservletcontext ();(commonly used)
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
String path =context.getrealpath (relative to the Web app's relative address);
Create a file a.txt under WebContent, create a b.txt under Web-inf, create a c.txt under SRC, and when the relative path is passed in the file name, the Getrealpath () method called with the context object returns a full path. That is, the absolute path
(3) ServletContext is a domain object
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);
Getatrribute (String name);
RemoveAttribute (String name);
Java Learning Javaweb Core servlet