Home  >  Article  >  Java  >  How to configure servlet

How to configure servlet

(*-*)浩
(*-*)浩Original
2019-05-16 16:36:529724browse


Java Servlet is a Web component based on Java technology. It runs on the server side and is managed by the Servlet container. It is used to generate dynamic content. Servlet is a platform-independent Java class. Writing a Servlet is actually writing a Java class according to the Servlet specification.

Servlet operation requires a running environment, that is, a Servlet container. Here we use Tomacat. As a Web server, Tomcat has the function of processing HTML pages. In addition, it is also a Servlet and JSP container.

How to configure servlet

#Implement a helloWord example.

Recommended course: Java Tutorial.

Method 1: To write a Servlet class by implementing the Servlet interface, you need to implement the 5 methods defined in the interface. The code is as follows:

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;
/*
 这里通过实现Servlet接口来编写一个简单的Servlet
 */
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();
    }
}

Then proceed to web.xml Configuration, the code is as follows:

<servlet>
     <servlet-name>ServletDemo</servlet-name>
     <servlet-class>day_052102.ServletDemo</servlet-class>
 </servlet>
 <servlet-mapping>
     <servlet-name>ServletDemo</servlet-name>
     <url-pattern>/ServletDemo</url-pattern>
 </servlet-mapping>

Click the run button to start the tomacat server, and then enter http://localhost:8080/day_052102/ServletDemo in the browser to implement an example of displaying hello World on the page. The results are as follows.


Method 2: Inherit the abstract class GenericServlet, which defines a general Servlet.

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

The code example to implement hellWord is as follows:

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();        
    }

}

Method three: By inheriting the abstract HttpServlet class, this class inherits from the GenericServlet class.

PS: There are no abstract methods in the HttpServlet class. This class overrides the service method. It will call the doGet or doGet method according to the request. Part of the source code is as follows:

protected void service(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException 
{
        String method = req.getMethod();
        if (method.equals(METHOD_GET))   //当请求方式为GET时,调用doGet方法
        {   
            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 override the service method in the class you write, then the Servlet container will The request is handled by the service method you overridden.

public class HttpServletDemo extends HttpServlet
{
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
    {
        PrintWriter out=resp.getWriter();
        out.println("hello!");
        out.close();
    }
}


The above is the detailed content of How to configure servlet. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn