我正在使用jsp将表单发送到servlet。在服务器端,处理完成,然后我想向用户发出警报,通知他是否成功。问题是我没有警报但也没有例外。当帖子结束时,我看到(虽然我不想)浏览器网址上的表单值如get ....这是代码
<form id="register" class="form-signin" >
<h2 class="form-signin-heading">Register here</h2>
First Name:<input type="text" name="firstName" class="input-block-level" placeholder="user name" required><br>
Last Name:<input type="text" name="lastName" class="input-block-level" placeholder="user name" required><br>
User Name:<input type="text" name="user" class="input-block-level" placeholder="user name" required><br>
Password:<input type="password" name="password" class="input-block-level" placeholder="Password" required><br>
Retype Password:<input type="password" name="retypedPassword" class="input-block-level" placeholder="Password" required><br>
Email:<input type="email" name="email" class="input-block-level" placeholder="email required"><br>
Retype Email:<input type="email" name="retypedEmail" class="input-block-level" placeholder="email required"><br>
<input type="submit" value="submit" id="submitButton" class="btn btn-primary">
</form>
<script type="text/javascript">
$(document).ready(function(){
$("#submitButton").on("click", function(){
$.ajax({
type: "POST",
url: "RegisterServlet", //process to mail
data: $("#register").serialize(),
success: function(msg){
alert(msg);
},
error: function(){
alert("failure editing account. User name already taken");
}
});
});
});
</script>
在服务器端
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String username = request.getParameter("user");
String password = request.getParameter("password");
String retypedPassword = request.getParameter("retypedPassword");
String email =request.getParameter("email");
String retypedEmail = request.getParameter("retypedEmail");
String hash = UUID.randomUUID().toString();
if(firstName == null || firstName.equals("")){
errorMsg = "First name can't be null or empty.";
}
if(lastName == null || lastName.equals("")){
errorMsg = "Last name can't be null or empty.";
}
if(username == null || username.equals("")){
errorMsg = "User name can't be null or empty.";
}
if(password == null || password.equals("")){
errorMsg = "Password can't be null or empty.";
}
if(retypedPassword == null || retypedPassword.equals("")){
errorMsg = "Retype password can't be null or empty.";
}
if(email == null || email.equals("")){
errorMsg = "Email can't be null or empty.";
}
if(retypedEmail == null || retypedEmail.equals("")){
errorMsg = "Retyped email can't be null or empty.";
}
if(password.equals(retypedPassword)==false){
errorMsg = "Passwords do not match.";
}
if(retypedEmail.equals(email)==false){
errorMsg = " Emails are not the same.";
}
if(errorMsg != null){
response.setContentType("text/plain"); // Set content type of the response so that jQuery knows what it can expect.
response.setCharacterEncoding("UTF-8");
response.getWriter().write(errorMsg);
}
但即使我尝试,我也什么也得不到。这段代码有什么问题?为什么提交后我会在网址上看到参数?
答案 0 :(得分:1)
为什么提交后我会在网址上看到参数?
您可以在URL中看到这些参数,因为您的表单未使用ajax(jquery)提交,您必须在表单上使用submit
处理程序,而不是单击按钮上的处理程序。
当您在submit
按钮上绑定点击事件时,在到达jquery之前提交您的表单。
使用此e.preventDefault();
来阻止默认提交表单。
而不是使用submit
按钮点击处理程序:
$("#submitButton").on("click", function(){
//your code
}
使用form
提交处理程序:
$('#register').on("submit", function(e) {
e.preventDefault();//preventing default submission
//your code
}
答案 1 :(得分:0)
看起来您的表单是通过''控件提交来代替您的javascript。表单的默认方法是GET(请参阅here以获得一个很好的解释),这就是您在地址栏中看到参数的原因。
将输入类型从submit
更改为button
,或将<input>
替换为<button>
,您可以更加严格地控制正在发生的事情。
答案 2 :(得分:0)
问题在于您的注册表格已提交且您的ajax调用失败。
我看到(虽然我不想)浏览器上表单的值 网址就像一个获取
由于表单已提交且未指定方法,因此默认情况下get会导致通过url发送参数。
请改为尝试:
$(document).ready(function(){
$("#register").on("submit", function(){
/* to do here */
return false;// restricts form submit
}
};