基于java的收藏排行功能之二:Servlet调用service层,向前端返回数据
接上一篇:5 创建FavoriteServlet的父类BaseServletpackage cn.itcast.travel.web.servlet1.base;import com.fasterxml.jackson.databind.ObjectMapper;import javax.servlet.ServletException;import javax.servlet...
·
接上一篇:
5 创建FavoriteServlet的父类BaseServlet
package cn.itcast.travel.web.servlet1.base;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class BaseServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
StringBuffer url = req.getRequestURL();
System.out.println(url);
String methodName = url.substring(url.lastIndexOf("/") + 1);
System.out.println("methodName:"+methodName);
try {
Method method = this.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
method.invoke(this,req,resp);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public String writeValueAstring(HttpServletResponse response,Object obj) throws IOException{
ObjectMapper mapper=new ObjectMapper();
return mapper.writeValueAsString(obj);
}
public void writeValue(HttpServletResponse response,Object obj) throws IOException{
ObjectMapper mapper=new ObjectMapper();
response.setContentType("application/json;charset=utf-8");
mapper.writeValue(response.getOutputStream(),obj);
}
}
6 创建FavoriteServlet调用service层,向前端返回数据
package cn.itcast.travel.web.servlet1;
import cn.itcast.travel.domain.PageBean;
import cn.itcast.travel.domain.Route;
import cn.itcast.travel.domain.User;
import cn.itcast.travel.service.FavoriteService;
import cn.itcast.travel.service.PageBeanService;
import cn.itcast.travel.service.impl.FavoriteServiceImp;
import cn.itcast.travel.service.impl.PageBeanServiceImp;
import cn.itcast.travel.web.servlet1.base.BaseServlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/favorite/*")
public class FavoriteServlet extends BaseServlet {
FavoriteService favoriteService=new FavoriteServiceImp();
public void favoriteOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String cid="";
String currentPage = request.getParameter("currentPage");
String cname = request.getParameter("cname");
String highPrice = request.getParameter("highPrice");
String lowPrice = request.getParameter("lowPrice");
String pageSize="8";
PageBean pageBean=favoriteService.findRouteOrderPageBean(cid,currentPage,cname,highPrice,lowPrice,pageSize);
writeValue(response,pageBean);
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)