Servlet如何只定义1个service方法,其它的方法按需求设置
Servlet——接口
↑继承
GenericServlet——抽象类
↑继承
HttpServlet——抽象类:推荐使用
GenericServlet抽象类:只需要实现service方法
只用重写一个方法,其他的方法都做了空实现(仅声明了方法,方法体内没有内容)
只把service方法做了抽象(abstract)
需要其他方法时重写就行了
虽然方便,但真正开发时不使用。真正使用HttpServlet
1 // 2 // Source code recreated from a .class file by IntelliJ IDEA 3 // (powered by Fernflower decompiler) 4 // 5 6 package javax.servlet; 7 8 import java.io.IOException; 9 import java.io.Serializable; 10 import java.util.Enumeration; 11 12 public abstract class GenericServlet implements Servlet, ServletConfig, Serializable { 13 private static final long serialVersionUID = 1L; 14 private transient ServletConfig config; 15 16 public GenericServlet() { 17 } 18 19 public void destroy() { 20 } 21 22 public String getInitParameter(String name) { 23 return this.getServletConfig().getInitParameter(name); 24 } 25 26 public Enumeration<String> getInitParameterNames() { 27 return this.getServletConfig().getInitParameterNames(); 28 } 29 30 public ServletConfig getServletConfig() { 31 return this.config; 32 } 33 34 public ServletContext getServletContext() { 35 return this.getServletConfig().getServletContext(); 36 } 37 38 public String getServletInfo() { 39 return ""; 40 } 41 42 public void init(ServletConfig config) throws ServletException { 43 this.config = config; 44 this.init(); 45 } 46 47 public void init() throws ServletException { 48 } 49 50 public void log(String msg) { 51 this.getServletContext().log(this.getServletName() + ": " + msg); 52 } 53 54 public void log(String message, Throwable t) { 55 this.getServletContext().log(this.getServletName() + ": " + message, t); 56 } 57 58 public abstract void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException; 59 60 public String getServletName() { 61 return this.config.getServletName(); 62 } 63 }
package cn.itcast.web.servlet; import javax.servlet.GenericServlet; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebServlet; import java.io.IOException; /** * @author 旗木五五开 * @create 2020-02-18 20:20 */ @WebServlet("/demo2") public class ServletDemo2 extends GenericServlet { @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { System.out.println("Demo2来袭"); } }
HttpServlet抽象类:对HTTP协议的封装,简化操作
为什么会有这个类的出现?
为了屏蔽get和post请求方式处理逻辑(继承HttpServlet,复写方法doGet();dopost();)。
因为将来都是调用service方法,service做一个方法分发,是哪个方式就调用哪个方法
HttpServlet基本的定义原理
实现步骤:
-
- 定义类继承HttpServlet
- 复写doGet();、dopost();方法
protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{ // 1.判断请求方式(method对应的请求方式) String method=req.getMethod(); long lastModified; if(method.equals("GET")){//常量放前面可以防止空指针异常 lastModified=this.getLastModified(req); if(lastModified==-1L){ this.doGet(req,resp);//调用doGet方法 }else{ long ifModifiedSince; try{ ifModifiedSince=req.getDateHeader("If-Modified-Since"); }catch(IllegalArgumentException var9){ ifModifiedSince=-1L; } if(ifModifiedSince<lastModified /1000L*1000L){ this.maybeSetLastModified(resp,lastModified); this.doGet(req,resp); }else{ resp.setStatus(304); } } }else if(method.equals("HEAD")){//HEAD lastModified=this.getLastModified(req); this.maybeSetLastModified(resp,lastModified); this.doHead(req,resp); }else if(method.equals("POST")){//POST this.doPost(req,resp); }else if(method.equals("PUT")){//PUT this.doPut(req,resp); }else if(method.equals("DELETE")){//DELETE this.doDelete(req,resp); }else if(method.equals("OPTIONS")){//OPTIONS this.doOptions(req,resp); }else if(method.equals("TRACE")){//TRACE //以上对应HTTP7个请求方式。只需要关心两个常用的就行get和post this.doTrace(req,resp); }else{ String errMsg=lStrings.getString("http.method_not_implemented"); Object[]errArgs=new Object[]{method}; errMsg=MessageFormat.format(errMsg,errArgs); resp.sendError(501,errMsg); } }
练习
package cn.itcast.web.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * @author 旗木五五开 * @create 2020-02-18 23:44 */ @WebServlet("/demo3") public class ServletDemo3 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("dogat"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("dopost"); } }
通过浏览器直接请求是get方式
通过表单完成post请求
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>login</title> </head> <body> <form action="/demo3" method="post"> <input type="text" name="username"> <br> <input type="submit" value="提交"> </form> </body> </html>