package com.michael.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.michael.dao.LinkDao;
import com.michael.dao.impl.LinkDaoImpl;
import com.michael.vo.Link;
public class LinkServlet
extends HttpServlet {
/** * Constructor of the object. */ public LinkServlet() {
super();
}
/** * Destruction of the servlet. <br> */ public void destroy() {
super.destroy();
// Just puts "destroy" string in log // Put your code here }
/** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String methodName = request.getParameter(
"methodName");
if (methodName !=
null && methodName.equals(
"add")) {
add(request, response);
}
else if (methodName !=
null && methodName.equals(
"query")) {
query(request, response);
}
else if (methodName !=
null && methodName.equals(
"delete")) {
delete(request, response);
}
else if (methodName !=
null && methodName.equals(
"forward")) {
forward(request, response);
}
else if (methodName !=
null && methodName.equals(
"update")) {
update(request, response);
}
else {
return;
}
/* //响应用户请求 String name = request.getParameter("name"); String url = request.getParameter("url"); //调用后台逻辑 LinkDao dao = new LinkDaoImpl(); Link l = new Link(); l.setName(name); l.setUrl(url); dao.add(l); List list = dao.list(); request.setAttribute("LinkList", list); //转发 request.getRequestDispatcher("/link.jsp").forward(request, response); */ }
public void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//响应用户请求 String name = request.getParameter(
"name");
String url = request.getParameter(
"url");
//调用后台逻辑 LinkDao dao =
new LinkDaoImpl();
Link l =
new Link();
l.setName(name);
l.setUrl(url);
dao.add(l);
query(request,response);
}
public void query(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//调用后台逻辑 LinkDao dao =
new LinkDaoImpl();
List list = dao.list();
request.setAttribute(
"LinkList", list);
//转发 request.getRequestDispatcher(
"/link.jsp").forward(request, response);
}
public void delete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//调用后台逻辑 LinkDao dao =
new LinkDaoImpl();
String[] ids = request.getParameterValues(
"ids");
dao.delete(ids);
query(request,response);
}
public void forward(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//调用后台逻辑 String id = request.getParameter(
"id");
LinkDao dao =
new LinkDaoImpl();
Link link = dao.get(Integer.parseInt(id));
request.setAttribute(
"link", link);
request.getRequestDispatcher(
"/editLink.jsp").forward(request, response);
}
public void update(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//响应用户请求 String id = request.getParameter(
"id");
String name = request.getParameter(
"name");
String url = request.getParameter(
"url");
//调用后台逻辑 LinkDao dao =
new LinkDaoImpl();
Link l =
new Link();
l.setId(Integer.parseInt(id));
l.setName(name);
l.setUrl(url);
dao.update(l);
query(request,response);
}
/** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init()
throws ServletException {
// Put your code here }
}