1、DispatcherServlet
javapackage com.csdn.mymvc.core; import jakarta.servlet.RequestDispatcher; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Arrays; @WebServlet("/*") public class DispatcherServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String[] staticResourceSuffixes = {".html", ".jsp", ".jpg", ".png", ".gif", ".css", ".js", ".ico"}; String uri = req.getRequestURI(); if (Arrays.stream(staticResourceSuffixes).anyMatch(uri::endsWith)) { RequestDispatcher defaultDispatcher = req.getServletContext().getNamedDispatcher("default"); defaultDispatcher.forward(req,resp); } } }
2、web.xml
XMLThe default servlet for all web applications, that serves static resources. It processes all requests that are not mapped to other servlets with servlet mappings (defined either here or in your own web.xml file). 默认servlet是所有Web应用程序的默认servlet,用于提供静态资源。 它处理所有未映射到其他servlet的请求, 这些servlet映射(在此处或您自己的web.xml文件中定义)。
XML<servlet> <servlet-name>default</servlet-name> <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>0</param-value> </init-param> <init-param> <param-name>listings</param-name> <param-value>false</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
2、FruitServlet
javapackage com.csdn.fruit.servlet; import com.csdn.fruit.dto.PageInfo; import com.csdn.fruit.dto.PageQueryParam; import com.csdn.fruit.dto.Result; import com.csdn.fruit.pojo.Fruit; import com.csdn.fruit.service.FruitService; import com.csdn.fruit.service.impl.FruitServiceImpl; import com.csdn.fruit.util.RequestUtil; import com.csdn.fruit.util.ResponseUtil; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/fruit/*") public class FruitServlet extends HttpServlet { private FruitService fruitService = new FruitServiceImpl(); @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // /fruit/index String uri = req.getRequestURI(); uri = uri.substring(1); String[] split = uri.split("/"); String methodName = split[1]; switch (methodName) { case "index" ->index(req,resp); case "add"-> add(req, resp); case "del"->del(req,resp); case "edit"->edit(req,resp); case "getFname"-> getFname(req, resp); case "update" -> update(req, resp); } } protected void index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Integer pageNo = 1; String pageNoStr = req.getParameter("pageNo"); if (pageNoStr != null && !"".equals(pageNoStr)) { pageNo = Integer.parseInt(pageNoStr); } String keyword = ""; String keywordStr = req.getParameter("keyword"); if (keywordStr != null) { keyword = keywordStr; } PageQueryParam pageQueryParam = new PageQueryParam(pageNo, 5, keyword); PageInfo<Fruit> pageInfo = fruitService.getFruitPageInfo(pageQueryParam); Result result = Result.OK(pageInfo); ResponseUtil.print(resp, result); } protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Fruit fruit = (Fruit) RequestUtil.readObject(req, Fruit.class); fruitService.addFruit(fruit); ResponseUtil.print(resp, Result.OK()); } protected void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Integer fid = Integer.parseInt(req.getParameter("fid")); fruitService.delFruit(fid); ResponseUtil.print(resp, Result.OK()); } protected void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Integer fid = Integer.parseInt(req.getParameter("fid")); Fruit fruit = fruitService.getFruitById(fid); ResponseUtil.print(resp, Result.OK(fruit)); } public void getFname(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String fname = req.getParameter("fname"); Fruit fruit = fruitService.getFruitByFname(fname); ResponseUtil.print(resp, fruit == null ? Result.OK() : Result.Fail()); } protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Fruit fruit = (Fruit) RequestUtil.readObject(req, Fruit.class); fruitService.updateFruit(fruit); ResponseUtil.print(resp, Result.OK()); } }
项目实战:设置静态资源放行
丁总学Java2023-11-06 16:38
相关推荐
MrZhangBaby6 分钟前
SQL-leetcode—1158. 市场分析 I一只淡水鱼6620 分钟前
【spring原理】Bean的作用域与生命周期五味香26 分钟前
Java学习,查找List最大最小值jerry-8940 分钟前
Centos类型服务器等保测评整/etc/pam.d/system-authJerry Lau41 分钟前
大模型-本地化部署调用--基于ollama+openWebUI+springBoot小白的一叶扁舟1 小时前
Kafka 入门与应用实战:吞吐量优化与与 RabbitMQ、RocketMQ 的对比幼儿园老大*1 小时前
【系统架构】如何设计一个秒杀系统?言之。1 小时前
【Java】面试中遇到的两个排序计算机-秋大田1 小时前
基于SSM的家庭记账本小程序设计与实现(LW+源码+讲解)南宫生1 小时前
力扣动态规划-7【算法学习day.101】