Javax.servlet Package Introduction

Source: Internet
Author: User

 

Source: Http://blog.163.com/[email protected]/blog/static/17158171200782333849784/

Developing Web Applications using Java technology, an in-depth understanding of the mechanism of the servlet will have an important role to play in the development of the application. And to learn more about the servlet mechanism you have to understand the Javax.servlet package.

In the Servlet2.3 specification, we introduce the classes in the Javax.servlet package:

The Javax.servlet package contains 7 interfaces, 3 classes, and 2 exception classes, each of which are:

Interfaces: Requestdispatcher,servlet,servletconfig,servletcontext,servletrequest,servletresponse and Singlethreadmodel

Classes: Genericservlet,servletinputstream and Servletoutputstream

Exception classes: Servletexception and Unavailableexception

The life cycle of the servlet

A servlet's life-cycle approach is defined in the servlet's interface, Init,service and destroy, respectively.

A simple servlet that demonstrates the servlet life cycle approach:

Import javax.servlet.*;
Import java.io.IOException;

public class Primitiveservlet implements Servlet {

public void init (ServletConfig config) throws servletexception {
System.out.println ("Init");
}

public void Service (ServletRequest request, servletresponse response)
Throws Servletexception, IOException {
SYSTEM.OUT.PRINTLN ("service");
}
public void Destroy () {
System.out.println ("destroy");
}

Public String Getservletinfo () {
return null;
}
Public ServletConfig Getservletconfig () {
return null;
}

}

How do I get the ServletConfig object in the servlet?

In the Servlet init method, servlet container will pass in a ServletConfig object that the developer can use to get the Servlet initialization parameters defined in the Web. xml file.

The following is an example of getting the servlet's initial parameters:

Import javax.servlet.*; Import java.util.Enumeration; Import java.io.IOException; public class Configdemoservlet implements Servlet {public  void init (servletconfig config) throws servletexception { C1/>enumeration parameters = Config.getinitparameternames ();     while (Parameters.hasmoreelements ()) {      String parameter = (string) parameters.nextelement ();       System.out.println ("Parameter name:" + Parameter);       System.out.println ("Parameter value:" +         Config.getinitparameter (Parameter));     }   }   public void Destroy () {  } public   void service (ServletRequest request, servletresponse response)     throws Servletexception, IOException {  } public   String Getservletinfo () {    return null;   }   Public ServletConfig Getservletconfig () {    return null;   }}
How do I get ServletContext objects?
ServletContext objects can be obtained through the Getservletcontext method of the ServletConfig object
Import javax.servlet.*; Import java.util.Enumeration; Import java.io.IOException;   public class Contextdemoservlet implements Servlet {ServletConfig servletconfig;   public void init (ServletConfig config) throws servletexception {servletconfig = config; public void Destroy () {} public void service (ServletRequest request, servletresponse response) throws Servlete     Xception, IOException {ServletContext ServletContext = Servletconfig.getservletcontext ();     Enumeration attributes = Servletcontext.getattributenames ();       while (Attributes.hasmoreelements ()) {String attribute = (string) attributes.nextelement ();       System.out.println ("Attribute name:" + Attribute);     System.out.println ("Attribute value:" + servletcontext.getattribute (Attribute));     } System.out.println ("Major version:" + servletcontext.getmajorversion ());     System.out.println ("Minor version:" + servletcontext.getminorversion ()); SYSTEM.OUT.PRINTLN ("Server infO: "+ servletcontext.getserverinfo ());   Public String Getservletinfo () {return null;   } public ServletConfig Getservletconfig () {return null; } }
How do I share information between servlets?
We can use ServletContext to maintain information that is shared between different servlets.
How do I troubleshoot multiple thread issues with Servlets?
If the servlet needs to read and write external resources, we need to consider thread issues, and we can use declarative interface Singlethreadmodel to avoid resource collisions between multiple thread.
However, it is important to note that if the servlet is simply reading external resources, we should not normally implement this interface. If this interface is implemented, the servlet can only serve one user request at a time, and the subsequent user request must wait in the queue.

Javax.servlet Package Introduction

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.