servlet里的添加语句

tech2024-10-25  22

public class AddServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //post请求 req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); //获取传递过来的值 //根据jsp里的name 点击sbumid传输过来的值 String name = req.getParameter("name"); double price= Double.parseDouble(req.getParameter("price")); String desc = req.getParameter("desc"); goods g = new goods(name, price, desc); //调用数据库的方法 goodsDao gd = new goodsDao(); //调用dao方法里的东西 int state = gd.addServlet(g); PrintWriter pw = resp.getWriter(); if(state!=0){ pw.print("<script>"); pw.print("alert('添加成功');"); //location.href跳转 //一定要通过servlet来访问index.jsp的内容 pw.print("location.href='QueryServlet'"); pw.print("</script>"); }else{ pw.print("<script>"); pw.print("alert('添加失败!');"); //一定要通过servlet来访问index.jsp的内容 pw.print("location.href='add.jsp'"); pw.print("</script>"); } } }
最新回复(0)