Servlet learning, servlet
1. servlet Introduction
Servlet (server applet) is a technology provided by sun for developing dynamic web resources.
Sun provides servlet interfaces in its APIs. To develop web resources, you must:
1. Write a java class to implement the servlet Interface
2. Deploy the developed java class to the web server
Ii. servlet running process
The servlet is called by the web server. After the web server receives the servlet access request from the client:
1. The web server first checks whether the servlet instance object has been loaded and created.
Yes ---- perform Step 1
No-Execute Step 1
2. Load and create an instance object for this servlet
3. Call the init () method of the servlet Instance Object
4. Create an HttpServletRequest object to encapsulate the http request message and an HttpServletResponse object to encapsulate the http response message, call the service () method of the servlet, and pass the request and response as parameters.
5. Before the web application is stopped or restarted, the servlet engine will uninstall the Servlet and uninstall the Servlet's destory () method.
Iii. Servlet interface implementation class
Servlet interface Sun defines two default implementation classes: GenericServlet (empty implementation) and HttpServlet (servlet capable of processing htpp requests)
During development, we only need to inherit the HttpServlet class, and then process requests and data in the service () method.
4. Access developed Servlet Resources
Because the client (browser) accesses resources on the web server through the URL address, Servlet programs must map the Servlet to a URL address if they want to be accessed.
· Configure in web. xml
<Servlet> an element is used to register a Servlet. It contains two main child elements.
<Servlet-name> Registration name
<Servlet-class> complete class Name
<Load-on-startup> load the Servlet when creating a web application
<Init-param> Configure attributes
<Servlet>
<Servlet-name> Servlet </servlet-name>
<Servlet-class> cctv. Session. Servlet </servlet-class>
</Servlet>
The <servlet-mapping> element is used to map an external access path of a registered Servlet.
<Servlet-name> Registration name
<Url-pattern> external access path
<Servlet-mapping>
<Servlet-name> Servlet </servlet-name>
<Url-pattern>/servlet </url-pattern>
</Servlet-mapping>
5. Servlet features
1. for multiple Servlet requests from the client, the server will only create one Servlet instance object. That is to say, once the Servlet instance object is created, it will reside in the memory and serve other subsequent requests, the servlet instance object will not be destroyed until the web Container exits.
2. In the Servlet life cycle, the init () method is called only once. For the service () method, each request will call this method and the instance Request Response object
6. Thread Security (waiting for updates)
Implement the SingleThreadModel Interface
VII. ServletConfig object
Config means configuration. You can imagine how this object is used to configure the file. How does it configure the file?
Use <init-param> to configure some initialization parameters for servlet in the web. xml file.
Store as key-value pairs
<Init-param>
<Param-name> url </param-name>
<Param-value> jdbc: mysql: // localhost: 3306 </param-value>
</Init-param>
Use the getInitParameter (name) method of the object to obtain the value of name, or use the getInitParameterNames () method to obtain the enumerator and then iterate to obtain all names and values.
8. ServletContext object
Context is the meaning of Context. We can imagine that this object is used to represent this web application.