1.前者只依赖@Controller(“/abc”)注解进行识别,后者是依赖Controller和ReqestMapping注解进行识别。
2.前者的类对象是由Servlet接口派生,后者是无派生关系的普通类。
@Controller
@RequestMapping("/demo")
public class DemoController {
@RequestMapping("/findall")
public String handleList(Model model) {
logger.info("demo02 handleList");
List list = demoService.findAll();
model.addAttribute("results", list);
return "demo";
}
}
@Controller("/demo_servlet2")
public class Demo2Servlet extends HttpServlet {
@PostConstruct //init-method="init"
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
logger.info("Demo2Servlet init start");
logger.info("Demo2Servlet init end");
}
@RequestMapping("/findall")
public String handleList(Model model) {
logger.info("demo02 handleList");
return "demo";
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
logger.info("Demo2Servlet service start");
response.setContentType(CONTENT_TYPE);
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
String result = demoService.getById(1l).getName();
//Integer.valueOf(result); //测试异常显示页面
PrintWriter out = response.getWriter();
out.println("");
out.println("\n");
out.println("" + result + " \n");
out.println(" \n");
out.flush();
out.close();
logger.info("Demo2Servlet service end");
}
@PreDestroy //destroy-method="destroy"
@Override
public void destroy() {
// TODO Auto-generated method stub
logger.info("Demo2Servlet destroy start");
logger.info("Demo2Servlet destroy end");
}
}