美文网首页java web 学习
表单传输链接数据库问题

表单传输链接数据库问题

作者: itachi | 来源:发表于2016-12-18 20:51 被阅读21次

    根据JDBC里面的内容在web项目中建立添加和查询的servlet

    package web;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import dao.EmpDao;
    
    import entity.Emp;
    
    public class AddEmpServlet extends HttpServlet{
    
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            //1.获取表单参数
            req.setCharacterEncoding("utf-8");
            String name = req.getParameter("name");
            String salary = req.getParameter("salary");
            String age = req.getParameter("age");
            
            //封装成emp对象
            Emp emp = new Emp();
            emp.setName(name);
            emp.setAge(Integer.parseInt(age));
            emp.setSalary(Double.parseDouble(salary));
            
            //3.保存员工
            EmpDao dao = new EmpDao();
            dao.save(emp);
            //4.响应,输出提示信息
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter out = resp.getWriter();
            out.println("<h1>新增成功</h1>");
            out.close();
        }
        
    }
    
    
    package web;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import dao.EmpDao;
    import entity.Emp;
    
    public class FindEmpServlet extends HttpServlet{
    
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            //1.查询全部员工
            EmpDao dao = new EmpDao();
            List<Emp> list = dao.findAll();
            //响应输出全部员工
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter out = resp.getWriter();
            //输出静态的html
            //包含版本声明,根元素,head,body,table,标题行
            out.println("<!doctype html>");
            out.println("<html>");
            out.println("   <head>");
            out.println("       <meta charset='utf-8'>");
            out.println("       <title>员工管理列表</title>");
            out.println("   </head>");
            out.println("   <body>");
            //当前路径:/emp/findEmp
            //目标路径:/emp/add_emp.html
            out.println("       <a href='add_emp.html'>新增</a>");
            out.println("       <table width='40%' border='1px' cellspacing='0px'>");
            out.println("           <tr>");
            out.println("               <th>编号</th>");
            out.println("               <th>姓名</th>");
            out.println("               <th>薪资</th>");
            out.println("               <th>年龄</th>");
            //out.println("             <th>操作</th>");
            out.println("           </tr>");
            //2.输出动态部分的html
            //包括:数据行,有多少个员工就有多少个数据行
            for(Emp e:list){
                out.println("           <tr>");
                out.println("               <td>"+e.getId()+"</td>");
                out.println("               <td>"+e.getName()+"</td>");
                out.println("               <td>"+e.getSalary()+"</td>");
                out.println("               <td>"+e.getAge()+"</td>");
                out.println("           </tr>");
            }
            out.println("       </table>");
            out.println("   </body>");
            out.println("</html>");
        }
        
    }
    
    

    相关文章

      网友评论

        本文标题:表单传输链接数据库问题

        本文链接:https://www.haomeiwen.com/subject/dsjzmttx.html