JavaWeb learning --Servlet related interfaces and classes

JavaWeb learning --Servlet related interfaces and classes

Abstract: This paper studied the Servlet interface and related classes.

Servlet interfaces and classes

Three ways

There are three ways to achieve Servlet:

Javax.servlet.Servlet implement the interface.

Javax.servlet.GenericServlet inherited class.

Javax.servlet.http.HttpServlet inherited class.

Implement the interface Servlet

Servlet interface is the most basic interface, if you want to use Servlet, we must implement this interface, or other inheritance have achieved this type of interface.

Create a class and implements the Servlet interface:

 1 public class TestServlet implements Servlet {
 2     @Override
 3     public ServletConfig getServletConfig() {
 4         return null;
 5     }
 6 
 7     @Override
 8     public String getServletInfo() {
 9         return null;
10     }
11 
12     @Override
13     public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
14         System.out.println ( "service () method is executed ......" );
 15      }
 16  
. 17      @Override
 18 is      public  void the init (the ServletConfig config) throws ServletException {
 . 19          System.out.println ( "the init () method is executed ... ... " );
 20 is      }
 21 is  
22 is      @Override
 23 is      public  void the destroy () {
 24          System.out.println (" the destroy () method is executed ...... " );
 25      }
 26 }

Inheritance of GenericServlet

GenericServlet class and override some of the ways to achieve Servlet interface, so that programmers in the development of the service only need to focus on implementation of the method () just fine.

Create a class and inherit GenericServlet categories:

1 public class TestServlet extends GenericServlet {
2     @Override
3     public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
4         System.out.println("service()方法被执行……");
5     }
6 }

Inherited HttpServlet class 

HttpServlet class inherits GenericServlet class is special support for HTTP request, due to the development of the project generally follows the HTTP protocol, so often used is the HttpServlet class.

In the HttpServlet service (HttpServletRequest, HttpServletResponse) method will be to judge the current request is a GET or POST, if a GET request, it will be to call this class doGet () method, if it is POST requests to call doPost () method, which described in subclasses to override doGet () or doPost () method can be.

Create a class and inherit HttpServlet class:

 1 public class TestServlet extends HttpServlet {
 2     @Override
 3     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 4         super.doGet(req, resp);
 5     }
 6     
 7     @Override
 8     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 9         super.doPost(req, resp);
10     }
11 }

Other interfaces and classes

ServletConfig Interface

Representative current configuration information the Servlet, ServletContext object encapsulates parameters and configuration information.

Can be obtained by Servlet inside getServletConfig () Method:

1 public ServletConfig getServletConfig();

Common methods:

1  public String getServletName (); // Gets the friendly name of the current Servlet. 
2  public ServletContext GetServletContext (); // Get ServletContext object. 
. 3  public String the getInitParameter (String name); // Get parameter initialization, initialization parameters can </ servlet> tag in <init-param> </ init -param> tag provided <servlet> web.xml profile. 
. 4  public the Enumeration <String> the getInitParameterNames (); // get all initialization parameters, initialization parameter also needs </ servlet> tag in <init-param> in <servlet> web.xml configuration file </ init-param> tag Lane set.

ServletContext interface

It represents the current Web applications, each Web application server creates a corresponding ServletContext object, to be shared by all clients. Automatically created when the Web application starts, when the Web application is closed and restarted when the server is shut down will cause ServletContext destroyed.

() Method which is obtained by ServletConfig getServletContext:

1 public ServletContext getServletContext();

Common methods:

1  public String getServletContextName (); // Gets the name of the current project. 
2  public String getServerInfo (); // returns the Servlet container name and version number. 
. 3  public ServletContext the getContext (String uripath); // Get ServletContext object through the path. 
4  public String getContextPath (); // get the relative directory on the server for the current project. For example: / HelloWorld. 
5  public String getRealPath (String path); // get the true path of the specified file, from the application root directory begins. For example: TestServlet get is drive: \ folder \ workspace \ project name \ WebContent \ TestServlet. 
6  public the Set <String> getResourcePaths (String path); //Get the relative path specified folder name and file name, start from the application root directory, you must start with / specified path. For example: / content is obtained under the application root directory. 
. 7  public the URL the getResource (String path) throws a MalformedURLException; // get the address specify the relative path from the root directory of the application begins. For example: / get is jndi: / domain name / project name /. 
. 8  public the InputStream the getResourceAsStream (String path); // the specified file to read into the stream, from the application start at the root. For example: index.html index.html can be acquired under the application root directory and into streams. 
9  public the RequestDispatcher getRequestDispatcher (String path); // create a jump to the transponder specified path, from the application root directory begin, you must start with / specified path. 
10  public the RequestDispatcher getNamedDispatcher (String name); // create a jump to the designated forwarder Servlet name, do not need or can not start with /. 
11  publicThe getInitParameter String (String name); // Get the specified parameter initialization, initialization parameters necessary <context-param> web.xml configuration file </ context-param> tag set. 
12 is  public the Enumeration <String> the getInitParameterNames (); // get all the specified initialization parameters, initialization parameters need in <context-param> web.xml configuration file </ context-param> tag set. 
13 is  public  Boolean setInitParameter (String name, String value); // set the initialization parameter, the same effect as in the <context-param> web.xml configuration file as </ context-param> tag set. 
14  public Object getAttribute (String name); // get the object properties by property name. 
15  public Enumeration <String> getAttributeNames (); // get all the property name. 
16  public  voidsetAttribute (String name, Object Object); // set the attribute names and objects. 
17  public  void removeAttribute (String name); // delete the object properties by property name.

RequestDispatcher Interface

RequestDispatcher instance object is created by the Servlet engine, a resource package to be called for other resources, and by a method which will forward the request to the client's package of resources.

RequestDispatcher interface defines two methods: forward () method and include () method. forward () and include () method of receiving two parameters, the current must be passed to the Servlet service () method of ServletRequest and ServletResponse objects or packaging them and the ServletRequestWrapper ServletResponseWrapper objects.

getRequestDispatcher by ServletContext object () method and the getNamedDispatcher () method to get:

1 public RequestDispatcher getRequestDispatcher(String path);
2 public RequestDispatcher getNamedDispatcher(String name);

Forwards the request:

1  // This method for transmitting a request from a Servlet server to another Servlet, Jsp Html page or file, sent by the current to another Servlet Servlet, which must be called before the response is submitted to the client.
2  // in the current request to the Servlet can set properties, respond back to the header information is not provided to be ignored, but the response-information setting may be ignored. 
. 3  public  void Forward (the ServletRequest Request, the ServletResponse Response) throws ServletException, IOException;

The request contains:

1  // This method comprises a Servlet, Jsp Html page or file in the response sent by the client to the current Servlet must be provided in the current encoding format the Servlet.
2  // current Servlet and resources can be set to be included in response to the body, according to the order output to the client. 
. 3  public  void the include (the ServletRequest Request, the ServletResponse Response) throws ServletException, IOException;

Forward the request and the request contains the comparison:

Most requests are forwarded applications in Servlet, the forwarding target mostly Jsp page.

Most of the request is included in the application Jsp page, complete with multiple pages.

Guess you like

Origin www.cnblogs.com/shamao/p/12061505.html