0
<%@ 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>

在输入两个值 A 和 B 时,值被传递给 servlet,从那里返回两个数字的乘积。但我无法将响应转换为整数值。我得到了产品的相应 ASCII 字符。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);
    }
    }
4

1 回答 1

2

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

打印一个整数

参数:

i - 要打印的 int

PrintWriter#write(int c)方法说

写入单个字符。

参数:

c - int 指定要写入的字符。

write()因此,您在 using方法中看到了相应的 ASCII 字符。

于 2013-06-16T06:03:44.147 回答