package com.action; import java.io.IOException; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.dao.DB; import com.orm.Ttea; public class tea_servlet extends HttpServlet { @Override public void service(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException { String type=req.getParameter("type"); if(type.endsWith("teaAdd")) { teaAdd(req, res); } if(type.endsWith("teaDel")) { teaDel(req, res); } if(type.endsWith("teaMana")) { teaMana(req, res); } if(type.endsWith("teaAll")) { teaAll(req, res); } } public void teaAdd(HttpServletRequest req,HttpServletResponse res) { String bianhao=req.getParameter("bianhao"); String name=req.getParameter("name"); String sex=req.getParameter("sex"); String age=req.getParameter("age"); String loginname=req.getParameter("loginname"); String loginpw=req.getParameter("loginpw"); String del="no"; String id=null; String sql="insert into t_tea(bianhao,name,sex,age,loginname,loginpw,del) values(?,?,?,?,?,?,?)"; Object[] params={bianhao,name,sex,age,loginname,loginpw,del}; DB mydb=new DB(); mydb.doPstm(sql, params); mydb.closed(); req.setAttribute("message", "²Ù×÷³É¹¦"); req.setAttribute("path", "tea?type=teaMana"); String targetURL = "/common/success.jsp"; dispatch(targetURL, req, res); } public void teaDel(HttpServletRequest req,HttpServletResponse res) { String sql="update t_tea set del='yes' where id="+Integer.parseInt(req.getParameter("id")); Object[] params={}; DB mydb=new DB(); mydb.doPstm(sql, params); mydb.closed(); req.setAttribute("message", "²Ù×÷³É¹¦"); req.setAttribute("path", "tea?type=teaMana"); String targetURL = "/common/success.jsp"; dispatch(targetURL, req, res); } public void teaMana(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException { List teaList=new ArrayList(); String sql="select * from t_tea where del='no'"; Object[] params={}; DB mydb=new DB(); try { mydb.doPstm(sql, params); ResultSet rs=mydb.getRs(); while(rs.next()) { Ttea tea=new Ttea(); tea.setId(rs.getInt("id")); tea.setBianhao(rs.getString("bianhao")); tea.setName(rs.getString("name")); tea.setSex(rs.getString("sex")); tea.setAge(rs.getString("age")); tea.setLoginname(rs.getString("loginname")); tea.setLoginpw(rs.getString("loginpw")); tea.setDel(rs.getString("del")); teaList.add(tea); } rs.close(); } catch(Exception e) { e.printStackTrace(); } mydb.closed(); req.setAttribute("teaList", teaList); req.getRequestDispatcher("admin/tea/teaMana.jsp").forward(req, res); } public void teaAll(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException { List teaList=new ArrayList(); String sql="select * from t_tea where del='no'"; Object[] params={}; DB mydb=new DB(); try { mydb.doPstm(sql, params); ResultSet rs=mydb.getRs(); while(rs.next()) { Ttea tea=new Ttea(); tea.setId(rs.getInt("id")); tea.setBianhao(rs.getString("bianhao")); tea.setName(rs.getString("name")); tea.setSex(rs.getString("sex")); tea.setAge(rs.getString("age")); tea.setLoginname(rs.getString("loginname")); tea.setLoginpw(rs.getString("loginpw")); tea.setDel(rs.getString("del")); teaList.add(tea); } rs.close(); } catch(Exception e) { e.printStackTrace(); } mydb.closed(); req.setAttribute("teaList", teaList); req.getRequestDispatcher("qiantai/tea/teaAll.jsp").forward(req, res); } public void dispatch(String targetURI,HttpServletRequest request,HttpServletResponse response) { RequestDispatcher dispatch = getServletContext().getRequestDispatcher(targetURI); try { dispatch.forward(request, response); return; } catch (ServletException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Override public void init(ServletConfig config) throws ServletException { super.init(config); } @Override public void destroy() { } }