我尝试了很多东西,但似乎没有用。可能是我做了很多错事,但我没有更多的想法可以测试可能有用。
顺便说一下,我正在使用Tomcat处理netBeans。
JSP代码:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Dynatree - Example</title>
<script src="jquery/jquery.js" type="text/javascript"></script>
<script src="jquery/jquery-ui.custom.js" type="text/javascript"></script>
<script src="jquery/jquery.cookie.js" type="text/javascript"></script>
<link href="src/skin/ui.dynatree.css" rel="stylesheet" type="text/css">
<script src="src/jquery.dynatree.js" type="text/javascript"></script>
<script type="text/javascript">
function myFunction(){
alert("Hello! I am an alert box!");
};
</script>
</head>
<body class="example">
<h1>Example: Form</h1>
<form action="Test" method="post" name="form" >
Username: <input type="text" name="userName" />
<br>
<textarea name="comment"></textarea>
<br>
<input type="submit" id="id2" value="Send data">
<br>
<input type="button" onclick="myFunction();" value="Show alert box">
</form>
<script type="text/javascript">
$("#id2").click(function() {
var json=[1,2,3,4];
$.ajax({
url:"Test",
type:"POST",
dataType:'json',
data: {json:json},
success:function(data){
alert("Hello! I am an alert box!");
}
});
});
</script>
</body>
</html>
我的servlet“测试”代码:
@WebServlet(urlPatterns = {"/Test"})
public class Test extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
String descr = request.getParameter("comment");
String[] myJsonData = request.getParameterValues("json[]");
out.print("\nDescription:");
out.print(descr);
out.print("\nPrint:");
out.print(myJsonData);
// response.sendRedirect("pasoServlet.jsp");
}
} /* end doPost() */
}
我的主要问题是,当我尝试打印“myJsonData”时,打印为null,我认为它不应该,但我不知道它为什么会这样做。
Description:This is the description
Print:null
答案 0 :(得分:1)
尝试更改
String[] myJsonData = request.getParameterValues("json[]");
到
String[] myJsonData = request.getParameterValues("json");