Call servlet from Ajax [duplicate]

2019-08-22 04:28发布

问题:

This question already has an answer here:

  • How to use Servlets and Ajax? 7 answers

This is my jsp.. and i use javascript-function and ajax-call to call servlet (ReadprojectInfo).

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
function displayProject()
{
    var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }

    xmlhttp.open("GET","ReadProjectsInfo",true);
    xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xmlhttp.onreadystatechange= function ()
    {
        if (xmlhttp.readyState==4)
        {
        if (xmlhttp.status == 200)
        {
        var time = xmlhttp.responseText;
        //alert(time);
        document.getElementById("center").innerHTML=xmlhttp.responseText;
        }
        }
    }
    xmlhttp.send(); 

//document.getElementById("center").innerHTML=Date();
}

</script>


<link rel="stylesheet" type="text/css" href="css/start.css" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Learning CSS</title>
</head>
    <body>
    <div id ="headerLink" class="HeaderLink" >
        <button id="adminLink" class="AdminLink" href='DNE.jsp'>Home</button></div>
        <button id="projectButton" class="ProjectButton"  onclick="displayProject()" >Projects</button>
    </div>

        <div id="center" class ="Center"><p>Click Project</p></div>
    </body>
</html>

Servlet code is as follows--

package excelExchange;

import java.io.IOException;
import java.io.PrintWriter;

import javaFiles.Dog;
import javaFiles.Person;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ReadProjectsInfo {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
    doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException{
        //String var1= request.getParameter("var");

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.write("Hello from servlet !!");
        out.println("Hello");

    }

}

My web.xml look like ---

    <servlet>
    <servlet-name>projectInfo</servlet-name>
    <servlet-class>excelExchange.ReadProjectsInfo</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>projectInfo</servlet-name>
    <url-pattern>/ReadProjectsInfo</url-pattern>
  </servlet-mapping>

Nothing is happenig on clickiing on " Project " button !! Please help me out in this .

回答1:

try commenting out the: xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); and the: if (xmlhttp.status == 200) { ... bits and see if it works



回答2:

You have to extend the HttpServlet in your class, like:

public class ReadProjectsInfo extend HttpServlet{
      ...
}

I think you could remove the line:

xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

It must work...



回答3:

give the complete url of the servlet class as below and try once. It worked for me. xmlhttp.open("GET","http://localhost:8080/excelExchange/ReadProjectsInfo",true);