无法获取ajax调用中从服务器传递的响应的整数值?

时间:2013-06-16 05:56:37

标签: java ajax servlets

<%@ 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 to get the xmlhttp object
    function getHttpObject(){
        var xmlhttp = null;
        if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}
        else if (window.ActiveXObject){xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
        else {alert("Your browser does not support XMLHTTP!");}
        return xmlhttp;
    }    

    function callMultiply(){
         xmlhttp = getHttpObject();
        xmlhttp.onreadystatechange=function(){
            if(xmlhttp.readyState==4){

            f1.result.value  = (xmlhttp.responseText);




            }
        }
        var a = parseInt(document.getElementById("a").value);
        var b = parseInt(document.getElementById("b").value);

       // var resul= (parseInt(a)*parseInt(b));
      // alert(parseInt(a)*parseInt(b));
       xmlhttp.open("POST","Multi?a=" + a + "&b=" + b, true);
       xmlhttp.send();

    }
    </script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form name="f1">
A= <input name="a" id="a" value=""><br />

B= <input name="b" id="b" value=""><br/>
Result= <input  id="result"  onclick="callMultiply();" >
</form>

</body>
</html>

On输入两个值A和B,该值将传递给servlet,返回两个数字的乘积。但是我无法将响应转换为Integer值。我得到了产品的相应ASCII caharcter。 servlet程序如下。

package com;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Multi
 */
@WebServlet("/Multi")
public class Multi extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Multi() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/xml");


       int a=0,b=0;
        if(request.getParameter("a") != null && request.getParameter("b") != null)
        {
        a = Integer.parseInt(request.getParameter("a")); 
        b = Integer.parseInt(request.getParameter("b"));

        }
        int result = (a*b);

        response.getWriter().write(result);
    }
    }

1 个答案:

答案 0 :(得分:1)

使用PrintWriter#print(int i)方法。

  

打印整数

     

<强>参数:

     

i - 要打印的int

PrintWriter#write(int c)方法说

  

写一个字符。

     

<强>参数:

     

c - 指定要写入的字符。

因此,您正在使用write()方法查看相应的ASCII字符。