Java ajax将值从jsp传递给servlet

时间:2014-05-06 04:21:57

标签: java jquery ajax jsp servlets

我试图通过ajax将jsp等基本值传递给servlet。我尝试了一切,但只传递了null。即使console.log(val)也不会向浏览器控制台打印任何内容。

我的理解是:网页的表单值在onsubmit上调用js文件。 js有一个调用servlet并传递表单数据的ajax。 servlet通过request.getParameter(val)

从ajax中获取数据

这是我的代码:

main.jsp中

<%@ 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>Insert title here</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" type="text/javascript">
<script src="js/main.js" type="text/javascript"></script>
</head>
<body>

<form method="post" action="Main" id="firstform">
    <h1>Enter name:</h1>
    <input type="text" name="id" id="id" />
    <input type="submit" name="submit"/>
</form>

</body>
</html>

main.js

var form = $('#firstform');
console.log("gi");
form.submit(function()
{
    $.ajax({
        url: 'Main',
        data: form.serialize(),
        type: 'post',
        success: function(data){ 
            console.log(data);
        }
            });

    //return false;

});

Main.java

package servlets;

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

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 Main
 */
@WebServlet("/Main")
public class Main extends HttpServlet {
    private static final long serialVersionUID = 1L;

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

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        int ids;
        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();
        String val = request.getParameter("id");
        System.out.print(val);
        if(val != null){
            ids = Integer.parseInt(val);
            out.print(ids); //
        }

    }

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

}

**问题:
1)从jsp传递给servlet的值
2)console.log不会在浏览器控制台上打印任何内容

1)有效,但2)仍然没有。**

3 个答案:

答案 0 :(得分:4)

main.js类型中的

type: 'post',并且您已在get方法中编写代码 做type:'get'

答案 1 :(得分:2)

输入字段中没有name属性。当你在做什么

String val = request.getParameter("id"); 

然后在servlet中,它将搜索具有name="id"的输入字段,但在您的表单中没有任何内容,因此它将返回null;

为输入字段命名,如

<input type="text" id="id" name="id"/>

同样 sanjay 已经说过你的ajax有类型帖子,所以也将它改为get

答案 2 :(得分:0)

仅针对 console.log(数据)问题,可能是 $ .ajax()函数与响应类型混淆,试试这个:

  • <强>的Ajax

    $.ajax({
        url: 'Main',
        data: form.serialize(),
        type: 'post',
        dataType:'text/plain',
        success: function(data){ 
            console.log(data);
        }
    });
    
  • <强>的Servlet

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