表单传输链接数据库问题

根据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("

新增成功

"); 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 list = dao.findAll();
        //响应输出全部员工
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        //输出静态的html
        //包含版本声明,根元素,head,body,table,标题行
        out.println("");
        out.println("");
        out.println("   ");
        out.println("       ");
        out.println("       员工管理列表");
        out.println("   ");
        out.println("   ");
        //当前路径:/emp/findEmp
        //目标路径:/emp/add_emp.html
        out.println("       新增");
        out.println("       ");
        out.println("           ");
        out.println("               ");
        out.println("               ");
        out.println("               ");
        out.println("               ");
        //out.println("             ");
        out.println("           ");
        //2.输出动态部分的html
        //包括:数据行,有多少个员工就有多少个数据行
        for(Emp e:list){
            out.println("           ");
            out.println("               ");
            out.println("               ");
            out.println("               ");
            out.println("               ");
            out.println("           ");
        }
        out.println("       
编号姓名薪资年龄操作
"+e.getId()+""+e.getName()+""+e.getSalary()+""+e.getAge()+"
"); out.println(" "); out.println(""); } }

你可能感兴趣的:(表单传输链接数据库问题)