现在已经尝试解决这个问题两天了,我无法解决这个问题。我创建了一个包含简单表单的jsp文件。当我提交表单时,它会将表单数据发布到/CommentController
,其中应返回输出并显示在表单下方的div
中。
AJAX调用似乎成功但它返回index.jsp的整个html而不是CommentController的输出。为什么要这样做?
我正在使用eclipse
,Jsp
和servlet
。
----------如果我导航到CommentController,它会输出看起来像index.jsp文件的大部分内容!?!?!?
以下是表格网页:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="com.servlet.explore.*, java.util.*, java.text.SimpleDateFormat, java.text.ParseException" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>p</title>
<meta name="description" content="p">
<meta name="author" content="Chris Mepham">
<BASE href="http://localhost:8080/chrismepham/read-post.jsp">
<link rel="stylesheet" href="css/main.css" type="text/css">
<link rel="stylesheet" href="css/fullPost.css" type="text/css">
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<form id="commentForm">
<input type="text" name="name" placeholder="Your name" required="required">
<textarea name="comment" placeholder="Enter your comment here (max. 500 characters)" required="required"></textarea>
<input type="submit" name="submit" id="commentSubmit" value="submit">
</form>
<div id="result"></div>
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
<script src="js/jquery.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
</body>
</html>
JQuery的:
$(document).ready(function () {
$("#commentSubmit").click(function(e){
var postData = $("#commentForm").serializeArray();
$.ajax({
type: "POST",
url : "CommentController",
data : postData,
dataType : "text",
success: function(data)
{
$("#result").text("<p>Success: </p>" + data);
},
error: function(jqXHR, textStatus, errorThrown)
{
$("#result").text("<p>ss"+errorThrown+textStatus+jqXHR+"</p>");
}
});
e.preventDefault(); //STOP default action
});
});
和CommentController.java文件:
package com.servlet.explore;
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;
import java.sql.PreparedStatement;
/**
* Servlet implementation class CommentController
*/
@WebServlet("/CommentController")
public class CommentController extends HttpServlet {
private static final long serialVersionUID = 1L;
PreparedStatement prep;
/**
* @see HttpServlet#HttpServlet()
*/
public CommentController() {
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
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text");
response.setCharacterEncoding("UTF-8");
response.getWriter().write("w0w00ww0w0");
}
}
答案 0 :(得分:0)
你犯了一个小错误。您需要替换目标HTML而不是将文本分配给那个
$("#result").html("<p>Success: </p>" + data);
而不是:
$("#result").text("<p>Success: </p>" + data);
答案 1 :(得分:0)
response.setContentType("text");
response.setCharacterEncoding("UTF-8");
response.getWriter().write("w0w00ww0w0");
用
替换它 response.getWriter().println("w0w00ww0w0");
答案 2 :(得分:0)
问题是存在urlrewriter
规则。
<rule>
<from>^/([a-zA-Z\-]+)$</from>
<to>/index.jsp?category=$1</to>
</rule>
上述规则是将POST
重定向到index.jsp页面。