通用型的Servlet(簡化Servlet的個數)
阿新 • • 發佈:2019-01-26
每次 一個 tel @param 類對象 自己 head oid inf
我們在編寫Web項目的是候,要產生動態頁面於是就有請求,而每一個請求就對應了一個Servlet,這個時候,如果頁面如果數量較多,那麽我們所編寫的Servlet也是很多的,於是就出現了通用的Servlet.
這麽舉個例子吧:
一個學生表, 恩--要有CRUD操作的話,至少要5個Servlet對吧,(Update操作需要兩個,一個查找並顯示,另一個負責修改), 這麽算,操作一張表就有了5個,那平時如果我們編寫的表愈多,那麽.....
呵呵,死不是不敢想象,於是就出現了通用的,怎麽說呢,就是一個小操作,
1.將所有的功能,都集成到一個類中,也就是將原先編寫在Servlet的doget或者doPost方法的內容,單獨寫到一個獨立的方法,這樣方法多少無所謂,不過可得要記住方法名,這在後面編寫方法時有用.
2.在頁面上填寫Servlet路徑的時候,添加一個參數 method="你編寫的方法名",
恩~完了,接下來看代碼
再提一下,我們都知道,請求到來時,tomcat會調用HttpServlet 的 service()方法然後由service()方法負責判斷轉交給那個方法像doGet()等,我們平時就覆寫doGet()等,在這裏我們要對HttpServlet()的service()方法進行拓展一下.
1 package com.item.servlet; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.http.HttpServlet;5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 import java.io.IOException; 8 import java.lang.reflect.Method; 9 10 //從此類來代替HttpServlet, 每次只需要繼承BaseServlet即可 11 public class BaseServlet extends HttpServlet { 12 @Override 13 protected voidservice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 14 15 //獲取url提交參數 -- 方法名 16 //這裏具體的方法在子類實現 17 String md = req.getParameter("method") ; 18 System.out.println(md) ; 19 String path = null ; //轉發的url 20 21 try { 22 //獲取本類對象的 字節碼對象 23 Class aClass = this.getClass() ; 24 25 //獲取類對象的叫 md 的方法 26 //第一個參數為方法名,後面的參數是該方法的形參列表,
這裏插入一個鏈接,可以去看一些其他人的解釋 https://blog.csdn.net/handsome_fan/article/details/54846959
27 Method method = aClass.getMethod(md, HttpServletRequest.class, HttpServletResponse.class) ; 28 29 //通過Method對象調用其md方法 30 if(null != method) { 31 // @param obj the object the underlying method is invoked from 32 path = (String) method.invoke(this, req, resp) ; 33 } 34 //如果需要轉發,那麽方法返回一個非null字符串表示路徑 35 if(null != path) { 36 req.getRequestDispatcher(path).forward(req, resp) ; 37 } 38 } catch (Exception e) { 39 e.printStackTrace() ; 40 } 41 } 42 }
恩,有了這個之後,其他的就很好做了
首頁
<%-- Created by IntelliJ IDEA. User: jff Date: 19-1-26 Time: 下午7:22 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>練習</title> <%--//Jquery庫--%> <script src="js/jquery-1.4.2.min.js"></script> <script> function doMesg() { $.post("/mytest/DemoServlet", {"method": "update"}, function(data) { alert(data) ; }, "json") ; } </script> </head> <body> <h1>添加請求</h1> <form action="/mytest/DemoServlet" method="POST"> <input type="hidden" name="method" value="add"> 信息:<input type="text"> <input type="submit" value="添加" /> </form> <h1>刪除</h1> <a href="/mytest/DemoServlet?method=remove">刪除</a> <h1>修改</h1> <button onclick="doMesg()" >修改</button> </button> </body> </html>
長這個樣子
------------------
我們只需要繼承自BaseServlet,編寫自己所需的方法,即可
1 package com.item.servlet; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.annotation.WebServlet; 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 import java.io.IOException; 8 9 //簡便方式 10 @WebServlet( 11 urlPatterns = {"/DemoServlet"} 12 ) 13 public class DemoServlet extends BaseServlet { 14 //添加 15 public String add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { 16 System.out.println("添加"); 17 return "" ; //這裏要返回轉交的url 18 } 19 public String remove(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { 20 System.out.println("刪除"); 21 return "" ; 22 } 23 public String update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { 24 System.out.println("更新") ; 25 return null ; 26 } 27 }
//運行實例嘛,可行,
-------------------------------------------------------------------------------------------------------------------------------
一起學習,一起進步.
通用型的Servlet(簡化Servlet的個數)