如:
/goodsservlet?method=selectGoodsListByTypeId
BaseServlet.java
package com.mylifes1110.java.controller;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
/**
* @ClassName BaseServlet
* @Description 一个基于反射实现的通用Servlet
* @Author Ziph
* @Date 2020/6/30
* @Since 1.8
* @Version 1.0
*/
public class BaseServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String methodName = req.getParameter("method");
if (methodName != null) {
Method method = null;
try {
method = this.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
} catch (NoSuchMethodException e) {
e.printStackTrace();
throw new RuntimeException("未找到Controller方法:" + methodName);
}
String invoke = null;
try {
invoke = (String) method.invoke(this, req, resp);
} catch (IllegalAccessException e) {
e.printStackTrace();
throw new RuntimeException("方法:" + methodName + " 访问异常!");
} catch (InvocationTargetException e) {
e.printStackTrace();
throw new RuntimeException("方法:" + methodName + " 执行异常!");
}
if (StringUtils.isNotEmpty(invoke)) {
if (invoke.startsWith("forward:")) {
req.getRequestDispatcher(invoke.split(":")[1]).forward(req, resp);
} else if (invoke.startsWith("redirect:")) {
resp.sendRedirect(invoke.split(":")[1]);
}
}
}
}
}
GoodsServlet.java
package com.mylifes1110.java.controller;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mylifes1110.java.bean.Goods;
import com.mylifes1110.java.bean.GoodsType;
import com.mylifes1110.java.bean.Page;
import com.mylifes1110.java.service.GoodsService;
import com.mylifes1110.java.service.impl.GoodsServiceImpl;
import com.mylifes1110.java.utils.GsonUtils;
@WebServlet(
name = "GoodsServlet",
value = "/goodsservlet"
)
public class GoodsServlet extends BaseServlet {
private GoodsService goodsService = new GoodsServiceImpl();
public String selectGoodsById(HttpServletRequest request, HttpServletResponse response)
throws SQLException, InvocationTargetException, IllegalAccessException {
String idStr = request.getParameter("id");
int id = Integer.parseInt(idStr);
Goods goods = goodsService.selectGoodsById(id);
request.setAttribute("goods", goods);
return "forward:/goodsDetail.jsp";
}
public String selectGoodsListByPage(HttpServletRequest request, HttpServletResponse response) throws SQLException {
String currentPageStr = request.getParameter("currentPage");
String typeIdStr = request.getParameter("typeId");
int currentPage = 0;
int typeId = 0;
if ((currentPageStr == null) || (currentPageStr == "")) {
currentPage = 1;
} else {
currentPage = Integer.parseInt(currentPageStr);
}
if ((typeIdStr == "") || (typeIdStr == null)) {
typeId = 1;
} else {
typeId = Integer.parseInt(typeIdStr);
}
Page goodsPage = goodsService.selectGoodsListByPage(currentPage, typeId);
request.setAttribute("pageBean", goodsPage);
return "forward:/goodsList.jsp";
}
public String selectGoodsListByTypeId(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("application/json;charset=utf8");
List goodsTypes = null;
try {
goodsTypes = goodsService.selectGoodsTypes();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
String json = GsonUtils.toJson(goodsTypes);
response.getWriter().write(json);
return null;
}
}