Servlet basic introduction

basic concept

  Servlet, also known as Java Servlet is a web component based on java technology, running on the server side, used to generate dynamic content. Servlet is a platform-independent java class. Writing a servlet is actually a java class written according to the Servlet specification.

  Servlet operation requires a running environment and a servlet container. Here we take tomcat as an example. As a web server, tomcat has the function of processing HTML pages. In addition, it is also a servlet and jsp container.

Method to realize

  The first: to implement the servlet interface, you need to implement the 5 methods defined in the interface

package day_052102;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/*
 Here, a simple Servlet is written by implementing the Servlet interface
 */
public class ServletDemo implements Servlet
{
    public void init(ServletConfig config) throws ServletException
    {
    }
    public ServletConfig getServletConfig()
    {
        
        return null;
    }
    public String getServletInfo()
    {
        return null;
    }
    public void destroy()
    {
    }
    public void service(ServletRequest req, ServletResponse res)
            throws ServletException, IOException
    {
        PrintWriter out=res.getWriter();
        out.print("hello World!");
        out.close();
    }
}

  The second: inherit the abstract class GenericServlet, which defines a general Servlet

public abstract class GenericServlet implements Servlet, ServletConfig, java.io.Serializable
public class GenericServletDemo extends GenericServlet
{

    @Override
    public void service(ServletRequest req, ServletResponse res)
            throws ServletException, IOException
    {
        PrintWriter out=res.getWriter();
        out.println("hello World!");
        out.close();
        
    }

}

  The third type: by inheriting the abstract HttpServlet class, which inherits from the GenericServlet class

The implementation code of the service method in HttpServlet:
protected
void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method = req.getMethod(); if (method.equals(METHOD_GET)) // When the request method is GET, call the doGet method { long lastModified = getLastModified(req); if (lastModified == -1 ) { // servlet doesn't support if-modified-since, no reason // to go through further expensive logic doGet(req, resp); } else { long ifModifiedSince; try { ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE); } catch (IllegalArgumentException iae) { // Invalid date header - proceed as if none was set ifModifiedSince = -1; } if (ifModifiedSince < (lastModified / 1000 * 1000)) { // If the servlet mod time is later, call doGet() // Round down to the nearest second for a proper compare // A ifModifiedSince of -1 will always be less maybeSetLastModified(resp, lastModified); doGet(req, resp); } else { resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED); } } }

If you want to rewrite the service method in your own class, then the servlet container will hand over the request to the service method we rewrite yourself.

protected void service(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException 
{
        String method = req.getMethod();
         if (method.equals(METHOD_GET))    // When the request method is GET, call the doGet method 
        {   
             long lastModified = getLastModified(req);
             if (lastModified == -1 )
            {
                // servlet doesn't support if-modified-since, no reason
                // to go through further expensive logic
                doGet(req, resp);
            }
            else
            {
                long ifModifiedSince;
                try 
                {
                    ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
                }
                catch (IllegalArgumentException iae)
                {
                    // Invalid date header - proceed as if none was set
                    ifModifiedSince = -1;
                }
                if (ifModifiedSince < (lastModified / 1000 * 1000))
                {
                    // If the servlet mod time is later, call doGet()
                    // Round down to the nearest second for a proper compare
                    // A ifModifiedSince of -1 will always be less
                    maybeSetLastModified(resp, lastModified);
                    doGet(req, resp);
                }
                else
                {
                    resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                }
            }
        }

 

 

 

 

 

 

 

Application in web'

Precautions'

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325165807&siteId=291194637