从Ajax调用servlet

时间:2012-03-20 14:32:35

标签: ajax servlets javascript-events web.xml

这是我的jsp ..我使用javascript-function和ajax-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>

<%@ 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代码如下 -

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");

    }

}

我的web.xml看起来像---

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");

    }

}

单击“项目”按钮时没有任何事情发生! 请帮帮我。

3 个答案:

答案 0 :(得分:0)

尝试评论: xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 和: if (xmlhttp.status == 200) { ... 比特,看它是否有效

答案 1 :(得分:0)

您必须在类中扩展HttpServlet,例如:

public class ReadProjectsInfo extend HttpServlet{
      ...
}

我认为你可以删除该行:

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

它必须有效......

答案 2 :(得分:0)

给出servlet类的完整url,如下所示。它对我有用。 xmlhttp.open("GET","http://localhost:8080/excelExchange/ReadProjectsInfo",true);