您的位置:首页 > 编程语言 > Java开发

javaweb servlet开发之servlet抽取(抽取基类减少servlet类数量)

2017-06-21 19:43 330 查看
平时如果客户端每要一笔数据,对应一个servlet去执行客户端请求,但这样做代码过于臃肿

会有一堆servlet类,xml文件也得配置超级多的内容。

此时我们可以采取抽取多个servlet为方法,

比如有多个和Product相关的servlet: productList, productItem, productCategory等

我们可以将这些Servlet变成方法集中到ProductServlet类中,此类中没有doGet()和doPost()方法

但是继承BaseServlet类,BaseServlet含有service方法,此方法采用反射的方式调用ProductServlet中的对应方法

这样product相关的客户端请求都可以放在ProductServlet类中

jsp或Html页面请求方式:   /productServlet?method=xxx

BaseServlet抽取方法:

package com.shop.web.servlet;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* 透过反射自动调用向相应方法,base类
* 子类找不到相应的service()方法会向父类寻找,找到baseServlet.service()
* 透过调用此方法
*/
public class BaseServlet extends HttpServlet {

@Override
public void service(HttpServletRequest req,HttpServletResponse resp){
//this代表调用此方法的对象,实际对象是ProductServlet类对象,
try {
Class clazz = this.getClass();
String methodName = req.getParameter("method");
Method method = clazz.getMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
method.invoke(this, req,resp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
ProudctServlet类对应的方法:

package com.shop.web.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.shop.Utils.JedisUtils;
import com.shop.domain.Category;
import com.shop.domain.PageBean;
import com.shop.domain.Product;
import com.shop.service.ProductService;
import com.shop.service.UserService;

import redis.clients.jedis.Jedis;

public class ProductServlet extends BaseServlet {

//Header中的商品分类列表servlet
public void categoryList(HttpServletRequest request,HttpServletResponse response) throws IOException{
ProductService productService = new ProductService();
//首先确认Redis缓存中是否含有categoryListJson
Jedis jedis = JedisUtils.getJedis();
String categoryListJson = jedis.get("categoryListJson");
if(categoryListJson==null){
System.out.println("redis缓存中没有数据,开始从数据库捞取");
//System.out.println("category ajax");
List<Category> categoryList = productService.findAllCategory();
Gson gson = new Gson();
categoryListJson = gson.toJson(categoryList);
jedis.set("categoryListJson", categoryListJson);
}

response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(categoryListJson);
}
//首页index servlet
public void index(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
ProductService productService = new ProductService();
List<Product> hotProductList = productService.hotProductList();
List<Product> newProductList = productService.newProductList();
request.setAttribute("hotProductList", hotProductList);
request.setAttribute("newProductList", newProductList);
request.getRequestDispatcher("/index.jsp").forward(request, response);
}

//product_info商品详细信息servlet
public void productInfo(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
String pid = request.getParameter("pid");
String cid = request.getParameter("cid");
String currentPage = request.getParameter("currentPage");

ProductService productService = new ProductService();
Product product = productService.findProductByPid(pid);
//	System.out.println(pid);
request.setAttribute("product", product);
request.setAttribute("cid", cid);
request.setAttribute("currentPage", currentPage);
//建立一个Cookie存到客户端,有新增浏览记录时再修改回来
//获取客户端Cookie,进行解析
Cookie[] cookies = request.getCookies();
String pids = pid;
if(cookies!=null){
for(Cookie cookie:cookies){
if("pids".equals(cookie.getName())){
pids=cookie.getValue();
String[] split = pids.split("-");
List<String> pidList = Arrays.asList(split);
LinkedList<String> linkedList = new LinkedList<String>(pidList);
if(linkedList.contains(pid)){
linkedList.remove(pid);
}
linkedList.addFirst(pid);
StringBuffer sb = new StringBuffer();
for(int i=0; i<linkedList.size()&& i<7; i++){
sb.append(linkedList.get(i));
sb.append("-");
}
pids = sb.substring(0, sb.length()-1);
}
}
}
Cookie pid_cookie = new Cookie("pids",pids);
response.addCookie(pid_cookie);
request.getRequestDispatcher("product_info.jsp").forward(request, response);
}
//分类商品详细信息,使用分页显示
public void productList(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
//获取cid
String cid = request.getParameter("cid");
ProductService productService = new ProductService();
String strPage = request.getParameter("currentPage");
int currentPage= 1;
if(strPage!=null){
currentPage = Integer.valueOf(strPage);
}
//考虑分页显示pageBean

int currentCount = 12;

PageBean<Product> pageBean = productService.pageBean(cid,currentPage,currentCount);

request.setAttribute("pageBean", pageBean);
request.setAttribute("cid", cid);

/*
* 获取cookie,显示历史浏览记录
*/
Cookie[] cookies = request.getCookies();
String historyPid = null;
List<Product> productList = new ArrayList<Product>();
if(cookies!=null){
for(Cookie cookie:cookies){
if("pids".equals(cookie.getName())){
historyPid = cookie.getValue();
}
}
if(historyPid !=null){
String[] historyPids = historyPid.split("-");
//List<Product> productList = new ArrayList<Product>();
for(String pid : historyPids){
productList.add(productService.findProductByPid(pid));
}
}

}

request.setAttribute("productList", productList);
request.getRequestDispatcher("/product_list.jsp").forward(request, response);

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  servlet java web jsp