java servletlis_一个基于反射实现的通用Servlet

如:

/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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值