Java servlet代码没有被调用?

时间:2016-05-10 11:47:33

标签: javascript java jquery html ajax

added the directory structure

我创建了一个简单的java servlet,在文本框中添加两个数字,并在单击提交按钮时返回结果。调用javaservlets来获取结果。我正在使用eclipse进行编辑。

index.jsp code
    <%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Ajax Example in JSP And Servlet</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
 $('#btn').click(function() {
  var number = $('#number').val();
  var number1=$('#number1').val();
  var datastr='number='+number+'&number1='+ number1;
  $.ajax({
   type:"post",
   url:"CalcAjax",
   data:datastr,
   success:function(msg)
   {
        $("p").append(response.d);
   }
  });
 });
});
</script>
</head>
<body>
<form id="form" method="post">
 Enter number1:
 <input id="number" type="text" name="number" />
 Enter number2:
 <input id="number1" type="text" name="number1" />
 <input id="btn" type="button" value="Calculate" name="btn"/>
</form>
<p></p>
</body>
</html>


CalcAjax.java code

    package com.ajax.servelets;

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

/**
 * Servlet implementation class CalcAjax
 */

public class CalcAjax extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request,response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        int number=0, number1=0;
        number=Integer.parseInt(request.getParameter("number"));
        number1=Integer.parseInt(request.getParameter("number1"));
        PrintWriter out=response.getWriter();
        out.println(number+number1);    
    }
}


web.xml code

    <web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http:/java.sun.com/dtd/web-app_2_3.dtd">
  <servlet>
    <servlet-name>CalcAjax</servlet-name>
    <servlet-class>com.ajax.servelets.CalcAjax</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>CalcAjax</servlet-name>
    <url-pattern>/CalcAjax</url-pattern>
  </servlet-mapping>
</web-app>

2 个答案:

答案 0 :(得分:2)

在仔细重新访问整个代码之后,代码中出现了一个小错误。

代码中的所有内容都非常正常,只是显示部分错误地显示结果

所以这个:

$("p").append(response.d);

需要替换为

$("p").html("");
$("p").append(msg);

ajax call的回复正在进入msg

希望这可以解决您的问题。

答案 1 :(得分:0)

试试这个,

$.post("CalcAjax",
{
    "number": "number",
    "number1": "number1"
},
function(data, status){
    $("p").append("Data: " + data + " Status: " + status);
});

代替$ .ajax()方法,而在之前的servlet中,

PrintWriter out=response.getWriter();

添加

response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");

希望这会有用