Java的AJAX从JSP传递值与Servlet [英] Java ajax passing values from jsp to servlet

查看:129
本文介绍了Java的AJAX从JSP传递值与Servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过AJAX从JSP到servlet传递的基本价值观,如ID。我什么都试过,但只有空被传递。即使的console.log(VAL)不打印任何浏览器控制台。

I am trying to pass basic values such as id from jsp to the servlet through ajax. I tried everything but only null is being passed. Even console.log(val) does not print anything to browser console.

我的理解是:的网页有其onsubmit的调用JS文件的形式值。 JS具有AJAX它调用servlet和传递形式的数据。从阿贾克斯该servlet争夺数据由的request.getParameter(VAL)

My understanding is: Web page has form values which onsubmit calls js file. js has ajax which calls the servlet and passes the data of the form. The servlet grabs data from ajax by request.getParameter(val)

下面是我的code:

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犯规打印在浏览器控制台什么

**Problems:
1)values passed from jsp to servlet
2)console.log doesnt print anything on browser console

1)作品,但2)还是犯规。**

1) works but 2) still doesnt.**

推荐答案

在main.js类型是键入:后,你已经写了code在获得方法 做键入:获得'

in main.js type is type: 'post' and you have written code in get method do type:'get'

这篇关于Java的AJAX从JSP传递值与Servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆