错误信息一直在,不自动刷新是什么问题,第一次正常运行,可以提示错误,第二次再失去焦点时候没有自动刷新,服务器报错500
前端代码
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>欢迎登录</title>
</head>
<body>
<form action="http://localhost:8080/2023-04-04_02_JDBC/UsernameServlet" method="post">
<input type="text" id="username" name="username" /><br />
<span id="nameshow"></span><br />
<input type="password" id="password" name="password" /><br />
<span id="pwdshow"></span><br />
<input type="submit" value="注册" id="zhuce"/>
<input type="submit" value="登录" id="denglu"/>
</form>
<script src="js/jq.js"></script>
<script>
$(function(){
$('#username').blur(function(){
$.ajax({
url:'http://localhost:8080/2023-04-04_02_JDBC/UsernameServlet',
data:{
username:$('#username').val(),
},
type:'post',
success(result) {
if(result==0){
$('#nameshow').html('用户名不存在').css('color','red');
}
}
});
});
});
</script>
</body>
</html>
servlet代码
package com.qf.servlet;
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 javax.servlet.http.HttpSession;
import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;
/**
* Servlet implementation class CheckNameServlet
*/
@WebServlet("/UsernameServlet")
public class UsernameServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public UsernameServlet() {
super();
}
private UserServiceImpl userServiceImpl = new UserServiceImpl();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String us = request.getParameter("username");
String pa = request.getParameter("password");
User u = userServiceImpl.selectByUsername(us);
if(u == null) {
response.getWriter().print(0);
}else {
// 判断密码是否正确
if (pa.equals(u.getPassword())) {
HttpSession session = request.getSession();
session.setAttribute("user", u);
response.sendRedirect("03-index.jsp");
} else {
response.getWriter().print("<script>alert('密码错误,请重新登陆');window.location.href='http://localhost:8080/2023-04-04_02_JDBC/01-denglu.jsp'</script>");
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
500 的内部服务器错误,控制台报错发出来看看
doGet方法中 39行看下是啥代码
根据您提供的代码,似乎在用户名输入框失去焦点时,通过 AJAX 发送了一个请求到 UsernameServlet
,然后根据返回结果在 <span id="nameshow"></span>
中显示错误消息。但是第二次失去焦点时,错误消息并没有自动刷新,而是仍然显示上一次的错误消息。
这可能是因为您没有在 AJAX 请求中设置 cache
为 false
。默认情况下,AJAX 请求会缓存响应,因此当您第二次失去焦点时,浏览器可能会从缓存中获取响应,而不是向服务器发送新的请求。由于服务器端没有处理缓存,因此会抛出 500 错误。
为了解决这个问题,您可以在 AJAX 请求中设置 cache
为 false
,如下所示:
$.ajax({
url:'http://localhost:8080/2023-04-04_02_JDBC/UsernameServlet',
data:{
username:$('#username').val(),
},
type:'post',
cache:false, // 添加这行代码
success(result) {
if(result==0){
$('#nameshow').html('用户名不存在').css('color','red');
}
}
});
通过设置 cache
为 false
,每次失去焦点时都会向服务器发送新的请求,从而保证显示的错误消息是最新的。