标签:ast override RoCE argument java技术 date 运行 gets 接口
基本概念
Servlet又称为Java Servlet是一个基于java技术的web组件,运行在服务器端,用于生成动态的内容。Servlet是平台独立的java类,编写一个Servlet实际上就是按照Servlet规范编写的java类。
Servlet运行需要一个运行环境,及需要一个Servlet容器,这里我们以tomcat为例,tomcat作为一个web服务器,具有处理HTML页面的功能,另外它还是一个Servlet和jsp容器。
实现方式
第一种:实现servlet接口,需要实现接口中定义的5个方法
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(); } }
第二种:继承抽象类GenericServlet,该类定义了一个通用的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(); } }
第三种:通过继承抽象的HttpServlet类,此类继承于GenericServlet类
HttpServlet中service方法的实现代码:
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); } } }
如果想在自己的类中重写service方法,那么servlet容器就会把请求交给我们自己重写的service方法处理
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); } } }
在web中的应用
首先java类的实现代码和“实现方式”中的代码类似,不再累述。
除此之外,还需要在web.xml中配置响应的servlet标签,分为两部分:
<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>
注意:servlet必须放在servlet-mapping之前
匹配原则
1.精确匹配
2.最长路径匹配
3.扩展匹配
注意事项
1.一个servlet可以对应多个servlet-mapping
2.servlet标签必须写在对应的servlet-mapping之前(这个不太确定)
标签:ast override RoCE argument java技术 date 运行 gets 接口
原文地址:https://www.cnblogs.com/htyj/p/8981613.html