1、DispatcherServlet
package 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.getReque服务器托管网stURI(); if (Arrays.stream(staticResourceSuffixes).anyMatch(uri::endsWith)) { RequestDispatcher defaultDispatcher = req.getServletContext().getNamedDispatcher("default"); defaultDispatcher.forward(req,resp); } } }
2、web.xml
The default servlet for all web applications, that s服务器托管网erves 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文件中定义)。
default org.apache.catalina.servlets.DefaultServlet debug 0 listings false 1
2、FruitServlet
package 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 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()); } }
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net