我是如何使用Ajax调用从servlet到我的jsp页面获取ArrayList数据的

时间:2015-12-18 09:39:19

标签: servlets

   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
  {
    // TODO Auto-generated method stub

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    // out.print("<html>");
    // out.print("<center>");
    //out.println("<body>");
    //out.println("<table border=3>");        
    //   String f,l,d,p,e,g;

    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/texon","root","");
        Statement st = con.createStatement();
        String query = "select * from emp_info";
        ResultSet rs = st.executeQuery(query);
         HttpSession session=request.getSession(false);
         String unm1= session.getAttribute("uname").toString();
         ArrayList<Employeelist> datalist = new ArrayList<Employeelist>();
       //  Employeelist emp=new Employeelist();
       //  int i=0;
         while(rs.next())
        {
              Employeelist emp=new Employeelist();

        /*  out.println("<tr><td>"+rs.getString(2)+"</td>");
            out.println("<td>"+rs.getString(3)+"</td>");
            out.println("<td>"+rs.getString(4)+"</td>");
            out.println("<td>"+rs.getString(5)+"</td>");
            out.println("<td>"+rs.getString(6)+"</td>");
            out.println("<td>"+rs.getString(7)+"</td></tr>");
        }
        out.println("</table>");
        out.print("</html>");
        out.print("</center>");
        out.println("</body>");*/
            // i=rs.getInt(1);
            emp.setEmp_f(rs.getString(2));
            emp.setEmp_l(rs.getString(3));
            emp.setEmp_d(rs.getString(4));
            emp.setEmp_p(rs.getString(5));
            emp.setEmp_e(rs.getString(6));
            emp.setEmp_g(rs.getString(7));
            //System.out.print(i+"/"+emp.getEmp_e());
            datalist.add(emp);
            /*datalist.add(rs.getString(3));
            datalist.add(rs.getString(4));
            datalist.add(rs.getString(5));
            datalist.add(rs.getString(6));
            datalist.add(rs.getString(7));*/


            //response.sendRedirect("editrecord.jsp");

    }
        //HttpSession session=request.getSession(false);
        session.setAttribute("data", datalist);

        request.getRequestDispatcher("employeeinfo.jsp").forward(
                request, response);
       /* Iterator<Employeelist> iterator = (Iterator) datalist.iterator();
        while (iterator.hasNext()) {    
            out.println("<td>"+iterator.next()+"</td>");
        }
        System.out.print("jdjknksdc");*/


    }catch(Exception e1) {
        System.out.print(e1);
    }
    /*finally{
     try 
     {
        session.setAttribute("resultset", rs);
        RequestDispatcher rd= request.getRequestDispatcher("employeeinfo.jsp");
        rd.forward(request, response);
        //response.sendRedirect("editrecord.jsp");
    } 
    catch (Exception e) 
    {
        System.out.println("This is finally block editrecord.jsp");
        e.printStackTrace();
    }
 }*/

}

0 个答案:

没有答案