Servlet没有将html代码返回给ajax

时间:2017-05-04 14:30:51

标签: ajax jsp servlets

我有一个jsp,其中ajax调用是为了从servlet获取一些数据,它将返回html表作为响应:

       function searchStudent() {
            var lname = document.getElementsByName("lname");
            var fname = document.getElementsByName("fname");
            var email = document.getElementsByName("email");
            var submit = document.getElementById("search");
            xmlhttp.onreadystatechange=useResponse;
            xmlhttp.open("GET", "SearchUser?
            submit="+submit+"&fname="+fname+"&lname="+lname+"&email"+email, 
             true);
            xmlhttp.send(null);
            }

         function useResponse() {
           alert(xmlhttp.status+" "+xmlhttp.readyState);
           if (xmlhttp.readyState==4 && xmlhttp.status==200) {
           alert("hi"+xmlHttp.responseText);
           document.getElementById("demo").innerHTML=xmlhttp.responseText;
           alert("hi"+xmlHttp.responseText);
         } 
       }

按钮单击时调用函数'searchStudent()'。

下面是servlet的doGet()方法:

     protected void doGet(HttpServletRequest request, HttpServletResponse 
    response) throws ServletException, IOException {
        String fname="";
        String lname="";
        String email="";
        String button="";
        button = request.getParameter("submit");
         db = new DbOperation();
        HttpSession session = request.getSession();
        session.removeAttribute("msg");

         if(button!=null && button.equalsIgnoreCase("Search")){
        fname=request.getParameter("fname")==null?"":request.getParameter("fname");
        lname=request.getParameter("lname")==null?"":request.getParameter("lname");
        email=request.getParameter("email")==null?"":request.getParameter("email");
        Admin admin =(Admin) session.getAttribute("Admin");
        int adminId=0;
        if(admin!=null)
            adminId=admin.getId();
        ArrayList<Student> student = new ArrayList<Student>();
        student= db.searchStudent(fname,lname,email,adminId);

        PrintWriter pw = response.getWriter();
        response.setContentType("text/html");
        response.setHeader("Cache-Control", "no-cache");  

        if(student!=null && student.size()>0){
            session.setAttribute("student", student);
            StringBuilder std= new StringBuilder();
            std.append("<table border='1'><th>Id</th><th>First_name</th><th>Last_Name</th><th>Email</th><th>State</th><th>City</th>");
            for(Student st :student ){
                std.append("<tr><td>"+st.getId()+"</td><td>"+st.getFname()+"</td><td>"+st.getLname()+"</td><td>"+st.getEmail()+"</td><td>"+st.getState()+"</td><td>"+st.getCity()+"</td></tr>");
            }
            std.append("<table>");

            //pw.write(std.toString());
            pw.write(std.toString());
            //pw.write("<h3>here</h3>");
        }else{
            pw.write("<h3>Student doesn't exists</h3>");
            /*RequestDispatcher rd = request.getRequestDispatcher("AdminServlet?msg1=Student doesn't exists");
            rd.forward(request, response);*/
        }



    }

}

servlet返回的html直接显示在控制台中,而不是从进行ajax调用的jsp进入。  警报( “HI” + xmlHttp.responseText);也没有得到填充

0 个答案:

没有答案