如何實現一個Servlet中的多個功能
前言:唉,打臉了,前腳剛說過要跟Servlet正式告別,後腳這不又來了,哈哈,總結出一點東西,糾結了一下還是做個分享吧,學習知識比面子重要,對吧,下回再也不約Servlet了。
需求分析
看下面程式碼,現在有一個UserServlet,裡面有增刪改查四個方法,按平常的思維,用一個Servlet實現一個功能,這沒毛病,現在問題是一個Servlet中有四個功能,那這該如何去實現呢?
package com.my.servlet; import com.my.service.UserService; import com.my.service.impl.UserServiceImpl; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/user") public class UserServlet extends HttpServlet { private UserService userService = new UserServiceImpl(); public String add(HttpServletRequest request,HttpServletResponse response){ userService.add(); request.setAttribute("mess","新增成功"); return "r:add.jsp"; } public String delete(HttpServletRequest request,HttpServletResponse response){ userService.delete(); request.setAttribute("mess","刪除成功"); return "f:add.jsp"; } public String update(HttpServletRequest request,HttpServletResponse response){ userService.update(); request.setAttribute("mess","修改成功"); return "add.jsp"; } public String query(HttpServletRequest request,HttpServletResponse response){ userService.query(); request.setAttribute("mess","查詢成功"); return "add.jsp"; } }
解決思維
解決這個問題其實也簡單,再寫一個FatherServlet繼承於HttpServlet,在這個父類Servlet中的service()方法中通過請求引數判斷要呼叫的子類UserServlet中的哪個方法並執行,可能說到這有些朋友不懂了,如何用引數去判斷大家一看下面的程式碼就明白了,我主要解釋一下大家的疑惑,子類UserServlet如何去呼叫父類FatherServlet中的service()方法,其實祕訣就在這個引數上,在訪問UserSerlvet時帶上請求引數,訪問過來後UserServlet中沒有解決引數的方法,所以他會在父類FatherServlet中尋找響應的方法,找到後執行,這是繼承的,子類繼承父類的方法沒毛病吧,所以就解決了。
解決完這個問題後我還在service()方法中加了判斷是重定向還是轉發的思維,這個就友情贈送了,不說了,來,看程式碼:
FatherServlet
package com.my.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class FatherServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String m = req.getParameter("m"); if (m==null||"".equals(m)){ throw new RuntimeException("引數不能為空"); } Class c = this.getClass(); try { Method method = c.getMethod(m,HttpServletRequest.class,HttpServletResponse.class); Object o =method.invoke(this,req,resp); String rel =String.valueOf(o); //判斷返回值的字首,預設轉發,r:重定向 f:轉發 if (rel.startsWith("r:")){ rel = rel.substring(rel.indexOf(":")+1); resp.sendRedirect(rel); }else if(rel.startsWith("f:")){ rel = rel.substring(rel.indexOf(":")+1); req.getRequestDispatcher(rel).forward(req,resp); }else { req.getRequestDispatcher(rel).forward(req,resp); } } catch (NoSuchMethodException e) { throw new RuntimeException("請求的方法不存在!!"); } catch (IllegalAccessException e) { throw new RuntimeException("請求方法執行異常!!"); } catch (InvocationTargetException e) { throw new RuntimeException("請求方法執行異常!!"); } } }
UserServlet
package com.my.servlet; import com.my.service.UserService; import com.my.service.impl.UserServiceImpl; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/user") public class UserServlet extends FatherServlet { private UserService userService = new UserServiceImpl(); public String add(HttpServletRequest request,HttpServletResponse response){ userService.add(); request.setAttribute("mess","新增成功"); return "r:add.jsp"; } public String delete(HttpServletRequest request,HttpServletResponse response){ userService.delete(); request.setAttribute("mess","刪除成功"); return "f:add.jsp"; } public String update(HttpServletRequest request,HttpServletResponse response){ userService.update(); request.setAttribute("mess","修改成功"); return "add.jsp"; } public String query(HttpServletRequest request,HttpServletResponse response){ userService.query(); request.setAttribute("mess","查詢成功"); return "add.jsp"; } }
完整演示
此篇主要是解決題目所示的需求,先在需求解決了,做戲還要做全呢,我就把我測試的程式碼都整理出來,方便新手去學習。
UserDao
package com.my.dao; public interface UserDao { public void add(); public void delete(); public void update(); public void query(); }
UserDaoImpl
package com.my.dao.impl; import com.my.dao.UserDao; public class UserDaoImpl implements UserDao { @Override public void add() { System.out.println("UserDao中的新增功能實現了"); } @Override public void delete() { System.out.println("UserDao中的刪除功能實現了"); } @Override public void update() { System.out.println("UserDao中的修改功能實現了"); } @Override public void query() { System.out.println("UserDao中的查詢功能實現了"); } }
UserService
package com.my.service; public interface UserService { public void add(); public void delete(); public void update(); public void query(); }
UserServiceImpl
package com.my.service.impl; import com.my.dao.UserDao; import com.my.dao.impl.UserDaoImpl; import com.my.service.UserService; public class UserServiceImpl implements UserService { private UserDao userDao = new UserDaoImpl(); @Override public void add() { userDao.add(); } @Override public void delete() { userDao.delete(); } @Override public void update() { userDao.update(); } @Override public void query() { userDao.query(); } }
UserServlet
package com.my.servlet; import com.my.service.UserService; import com.my.service.impl.UserServiceImpl; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/user") public class UserServlet extends FatherServlet { private UserService userService = new UserServiceImpl(); public String add(HttpServletRequest request,HttpServletResponse response){ userService.add(); request.setAttribute("mess","新增成功"); return "r:add.jsp"; } public String delete(HttpServletRequest request,HttpServletResponse response){ userService.delete(); request.setAttribute("mess","刪除成功"); return "f:add.jsp"; } public String update(HttpServletRequest request,HttpServletResponse response){ userService.update(); request.setAttribute("mess","修改成功"); return "add.jsp"; } public String query(HttpServletRequest request,HttpServletResponse response){ userService.query(); request.setAttribute("mess","查詢成功"); return "add.jsp"; } }
FatherServlet
package com.my.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class FatherServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String m = req.getParameter("m"); if (m==null||"".equals(m)){ throw new RuntimeException("引數不能為空"); } Class c = this.getClass(); try { Method method = c.getMethod(m,HttpServletRequest.class,HttpServletResponse.class); Object o =method.invoke(this,req,resp); String rel =String.valueOf(o); //判斷返回值的字首,預設轉發,r:重定向 f:轉發 if (rel.startsWith("r:")){ rel = rel.substring(rel.indexOf(":")+1); resp.sendRedirect(rel); }else if(rel.startsWith("f:")){ rel = rel.substring(rel.indexOf(":")+1); req.getRequestDispatcher(rel).forward(req,resp); }else { req.getRequestDispatcher(rel).forward(req,resp); } } catch (NoSuchMethodException e) { throw new RuntimeException("請求的方法不存在!!"); } catch (IllegalAccessException e) { throw new RuntimeException("請求方法執行異常!!"); } catch (InvocationTargetException e) { throw new RuntimeException("請求方法執行異常!!"); } } }
add.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="z" uri="http://mycompany.com" %> <html> <head> <title>測試頁面</title> </head> <body> <h1>測試</h1> ${mess} </body> </html>
測試展示
越努力越幸運
最後來點雞湯吧,好久沒說了,吃苦的年齡就不要選擇安逸,別白了少年頭,空悲切,人生贏在轉折處,改變從現在開始,分享的越多,你的價值增值越大。。。。。。。。。
*****************************************************************************************************
我的部落格園地址:https://www.cnblogs.com/zyx110/
轉載請說明出處
我不能保證我所說的都是對的,但我能保證每一篇都是用心去寫的,我始終認同“分享的越多,你的價值增值越大”,歡迎大家關注我的技術分享“Java匹馬行天下”和學習心得分享“匹馬行天下”,在分享中進步,越努力越幸運,人生贏在轉折處,改變從現在開始!
支援我的朋友們記得點波推薦哦,您的肯定就是我前進的動力。