文章目录
- 前言
- 一、前后对比✨
- 二、具体实现🎇
- 三、效果展示🎏
前言
先说项目背景,本项目是本人在校期间老师布置的作业(就一个CRUD),课程是后端应用程序设计,其实就是servlet和jsp那一套,要求使用jsp+servlet完成一个天气查询系统,如果直接使用HttpServlet的话,业务代码中会有大量的if-else,所以本人将servlet进行了再封装,并使用自定义注解接收前端的参数,主要是利用反射去完成的。
一、前后对比✨
前后对比
封装前
重写doGet和doPost方法,在其中使用action这个参数来完成业务实现的区分。
package com.servlet;import com.entity.WeatherInfo;
import com.service.ManageService;
import com.service.impl.ManageServiceImpl;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;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;/*** @Author YZK* @Date 2023/11/9*/
@WebServlet(value = "/weather/manage",name = "mangeServlet")
public class MangeServlet extends HttpServlet {ManageService manageService = new ManageServiceImpl();@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("utf-8");resp.setContentType("text/html;charset=UTF-8");String action = req.getParameter("action");//跳转至添加数据界面if (action.equals("toAdd")) {req.getRequestDispatcher("/addInfo.jsp").forward(req, resp);//修改数据并回显当前行数据} else if (action.equals("toEdit")) {String id = req.getParameter("id");try {WeatherInfo weatherInfo = manageService.queryWeatherInfoById(id);req.getSession().setAttribute("weatherInfo", weatherInfo);req.getRequestDispatcher("/editInfo.jsp").forward(req, resp);} catch (Exception e) {throw new RuntimeException(e);}}}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//todo 封装更新、添加操作req.setCharacterEncoding("utf-8");resp.setContentType("text/html;charset=UTF-8");String action = req.getParameter("action");//通过id删除数据switch (action) {case "delete": {String id = req.getParameter("id");try {manageService.deleteById(id);} catch (Exception e) {throw new RuntimeException(e);}break;}//添加数据case "add": {//todo 增加数据校验Map<String, String> map = this.encapsulationData(req);try {manageService.insertWeatherInfo(map.get("province"), map.get("city"), map.get("weather"), map.get("temperature"), map.get("windDirection"), map.get("windPower"), map.get("humidity"), new Date(),Float.toString(Float.parseFloat(map.get("temperature"))), Float.toString(Float.parseFloat(map.get("humidity"))));} catch (Exception e) {throw new RuntimeException(e);}break;}//更新数据case "update": {String id = req.getParameter("id");Map<String, String> map = this.encapsulationData(req);try {manageService.updateWeatherInfo(id, map.get("province"), map.get("city"), map.get("weather"), map.get("temperature"), map.get("windDirection"), map.get("windPower"), map.get("humidity"), new Date(),Float.toString(Float.parseFloat(map.get("temperature"))), Float.toString(Float.parseFloat(map.get("humidity"))));} catch (Exception e) {throw new RuntimeException(e);}break;}}}public Map<String, String> encapsulationData(HttpServletRequest req) {Map<String, String> map = new HashMap<>();map.put("province", req.getParameter("province"));map.put("city", req.getParameter("city"));map.put("weather", req.getParameter("weather"));map.put("temperature", req.getParameter("temperature"));map.put("windDirection", req.getParameter("windDirection"));map.put("windPower", req.getParameter("windPower"));map.put("humidity", req.getParameter("humidity"));return map;}
}
封装后
package com.servlet;import cn.hutool.http.HttpUtil;
import com.annotation.Action;
import com.entity.WeatherInfo;
import com.service.IndexService;
import com.service.impl.IndexServiceImpl;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;/*** @Author YZK* @Date 2023/11/9*/
@WebServlet(value = "/weather/index", name = "indexServlet")
public class IndexServlet extends BaseServlet {IndexService indexService = new IndexServiceImpl();@Action(actionName = "queryWeather")public void queryWeather(HttpServletRequest req, HttpServletResponse resp) throws Exception {List<WeatherInfo> weatherInfoList = indexService.queryWeather();req.getSession().setAttribute("weatherInfoList", weatherInfoList);req.getRequestDispatcher("/weatherInfo.jsp").forward(req, resp);}@Action(actionName = "init")public void init(HttpServletRequest req, HttpServletResponse resp) {List<WeatherInfo> weatherInfoList = null;try {weatherInfoList = indexService.queryWeather();req.getSession().setAttribute("weatherInfoList", weatherInfoList);req.getRequestDispatcher("/init.jsp").forward(req, resp);} catch (Exception e) {throw new RuntimeException(e);}}@Action(actionName = "toLogin")public void toLogin(HttpServletRequest req, HttpServletResponse resp) throws Exception {req.getRequestDispatcher("/login.jsp").forward(req, resp);}@Action(actionName = "login")public void login(HttpServletRequest req, HttpServletResponse resp) throws Exception {String username = req.getParameter("username");String password = req.getParameter("password");if (username.equals("admin") && password.equals("123")) {List<WeatherInfo> weatherInfoList = indexService.queryWeather();req.getSession().setAttribute("weatherInfoList", weatherInfoList);req.getRequestDispatcher("/weatherInfo.jsp").forward(req, resp);} else {resp.sendRedirect("/login.jsp");}}
}
将每一个业务行为都抽成了一个方法,前端发起一个带有action的参数,后端使用同样的@Action(actionName="")进行接收,如果相同,则执行该方法。
二、具体实现🎇
在HttpServlet中,service()方法是用来处理客户端请求的主要方法。它接收一个HttpServletRequest对象和一个HttpServletResponse对象作为参数,并根据请求的类型(GET、POST、PUT等)调用对应的doGet()、doPost()、doPut()等方法来处理请求。如果没有覆盖service()方法,它会自动调用doGet()或doPost()方法,具体取决于客户端请求的类型。开发人员可以覆盖service()方法来自定义处理请求的逻辑。
BaseServlet
package com.servlet;import com.annotation.Action;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.Method;/*** @Author YZK* @Date 2023/12/2* @Desc*/
public class BaseServlet extends HttpServlet {@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("utf-8");resp.setContentType("text/html;charset=UTF-8");String servletName = req.getHttpServletMapping().getServletName();String className = req.getServletContext().getServletRegistration(servletName).getClassName();//通过全类名获取servlet实例来执行其中的方法try {Class<?> clazz = Class.forName(className);Method[] methods = clazz.getMethods();for (Method m : methods) {if (m.isAnnotationPresent(Action.class)) {String s = m.getAnnotation(Action.class).actionName();m.setAccessible(true);if (req.getParameter("action").equals(s)) {m.invoke(this, req, resp);}}}} catch (Exception e) {e.printStackTrace();throw new RuntimeException(e);}}
}
代码步骤解释:
- 在用户发起请求时,从请求中获取当前请求的servlet的名字,
- 通过req.getServletContext().getServletRegistration(servletName)获取当前请求的servlet的全类名
- 通过全类名获取Class对象,再通过getMethods()方法获取该类所有的方法
- 遍历所有方法,如果带有@Action注解,再判断传入的action参数是否与注解中的actionName参数相同
- 相同的话就执行该方法(前提是一定要传入req和resp两个参数)
完成以上所有便对servlet进行了简单的封装,这个封装还是很不完善,比如没有判断action为空和同名的情况。
三、效果展示🎏
调用相应接口,返回正常页面