无法从servlet获取传递的数据 [英] unable to get the passed data from servlet

查看:78
本文介绍了无法从servlet获取传递的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个与常规jsp-servlet交互正常运行的程序.但是,当我使用ajasx提交相同内容时,它不能正常工作.下面是我的代码.

I'm writing a program that is working fine with the regular jsp-servlet interaction. But when I submit the same with ajasx, it is not working fine. Below is my code.

JSP-Servlet(不含Ajax)

index.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>
</head>
<body>
    <form name="f1" id="f1" method="post" action="Controller">
        <input type="text" name="name1" id="name1" /> <input type="submit" />
    </form>
</body>
</html>

控制器

protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String nsme = request.getParameter("name1");
        request.setAttribute("name", nsme);
        request.getRequestDispatcher("index1.jsp").forward(request, response);
    }

index1.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>
</head>
<body>
    <input type="text" value="${name}" />
</body>
</html>

使用Ajax

With Ajax

index.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 type="text/javascript"
    src="http://code.jquery.com/jquery-1.10.0.min.js"></script>
</head>
<body>
    <form name="f1" id="f1">
        <input type="text" name="name1" id="name1" /> <input type="submit"
            name="x" id="x" />
    </form>
    <script type="text/javascript" src="SampleJS.js"></script>
</body>
</html>

SampleJS.js

$('#x').click(function() {
    $.ajax({
        type : 'POST',
        url : 'Controller',
        success : function(data) {
            console.log(data)
        },
    });
});

控制器

protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String nsme = request.getParameter("name1");
        request.setAttribute("name", nsme);
        request.getRequestDispatcher("index1.jsp").forward(request, response);
    }

index1.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>
</head>
<body>
    <input type="text" value="${name}" />
</body>
</html>

在这里,当我使用第一种方法时,名称将正确打印.当我将其与AJAX结合使用时,令我惊讶的是,文本框中未打印任何内容.

Here when I use the first method, The name is printed correctly. When I use it with AJAX, to my surprise nothing gets printed in the textbox.

基本上,我们使用request.getRequestDispatcher("GetCaseData.jsp").forward(request, response);转到另一页.在我的情况下,它有什么替代方案? 请让我知道我要去哪里错了,我该如何解决.

Basically, We use request.getRequestDispatcher("GetCaseData.jsp").forward(request, response); to go to another page. What would be the alternative of it in my case? please let me know where am I going wrong and how can I fix this.

推荐答案

在Ajax情况下,您不发送任何表单数据.使用$.serialize序列化表单数据并将其发送到服务器:

In the Ajax case you don't send any form data. Use $.serialize to serialize the form data and send it to the server:

$('#x').click(function() {
    var formData = $("#f1").serialize();
    $.ajax({
        type : 'POST',
        url  : 'Controller',
        data : formData,
        success : function(data) {
            console.log(data)
        },
    });
});

这篇关于无法从servlet获取传递的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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