我试图通过自定义jQuery确认框将值传递给servlet。
我搜索了网页,发现我们可以使用$post
将数据传递给servlet,但不知何故数据没有传递给servlet。
我的要求是我需要向用户提供两个选项open
或save
pdf文档。我使用带有这些选项的自定义jQuery框,但数据没有传递给servlet。
以下是我的代码:
<%@ 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>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.2/themes/redmond/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.11.2.js"></script>
<script src="https://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<!-- User Defined Js file -->
<script type="text/javascript">
$(document).ready(function(){
$('#Export').click(function(event) {
event.preventDefault();
var currentForm = $(this).closest('form');
var dynamicDialog = $('<div id="conformBox">'+
'<span style="float:left; margin:0 7px 20px 0;">'+
'</span>Open or save the document</div>');
dynamicDialog.dialog({
title : "Open/Save Dialog",
closeOnEscape: true,
modal : true,
buttons :
[{
text : "Export",
click : function() {
$(this).dialog("close");
$.POST({
type: "post",
url: "button",
data: $('#Export').attr("name"),
success: function(data) {
$('#results').show();
$('#results').html(data);
}
});
// currentForm.submit(); // if I use this then control going to servlet but data is not passed without nothing was happening
}
},
{
text : "Open",
click : function() {
$(this).dialog("close");
currentForm.submit();
}
}]
});
return false;
});
});
</script>
</head>
<body>
<form id="god" action="button">
<button type="button" id="Export">Export</button>
</form>
<div id="results"></div>
</body>
</html>
Servlet代码:
package com.testcase.testing;
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 button
*/
@WebServlet("/button")
public class button extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public button() {
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
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
response.getWriter().append(request.getParameter("data"));
System.out.println(request.getParameter("data"));
}
}
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Testcase</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- <servlet>
<servlet-name>button</servlet-name>
<servlet-class>com.testcase.testing.button</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>button</servlet-name>
<url-pattern>/Buttonpage.jsp</url-pattern>
</servlet-mapping> -->
</web-app>
无法追踪我做错了什么。
答案 0 :(得分:1)
这是使用$ .post
发送数据的方法var param = "value";
$.post('query.php', { param: param }, function(data) {
//do what you want with returned data
})