如何在运行JavaScript验证代码后调用servlet



我正在尝试首先运行一个字符长度检查,如果用户通过了检查,则需要调用servlet,servlet将返回该用户登录的信息。

<html>
<head><title>Login</title></head>
<link rel="stylesheet" href="css/style.css">
<body>
<div class="Welcome-division">
<div class="header-top"> <a href="/"><img class="logo" src="images/logo.png" alt="Hostel logo"></a>
<H1>Welcome to Arihant's hostel</H1>
</div>
<navbar class="horizontal-navbar">
<a href="Home.jsp">Home</a>
<a class="active" href="Login.jsp">Login</a>
<a href="Room details.jsp">Room Details</a>
<a href="Contact Us.jsp">Contact Us</a>
</navbar>
</div>
<div class="center-body">
<navbar class="Menu-option">
<a href="Home.jsp">Home</a>
<a class="active" href="Login.jsp">Login</a>
<a href="Room details.jsp">Room Details</a>
<a href="Contact Us.jsp">Contact Us</a>
</navbar>
<div class="Room-information">

<form class="loginForm" method=post action=LoginServlet> 
<div>Room No: <input type="text" name="Roomno" id="Rno"><br></div><br>
<div>Password : <input type="password" name="password" id="pass"></div><br>
<input id="formSubmit" type="submit" value="SUBMIT">  
</form> 
</div>
</div>
<div class="Copyright-Information">
&#169; Arihant graphics
</div>
<script>
const formSubmit = document.getElementById("formSubmit");
formSubmit.addEventListener("click", (e) => {
const Rno = document.getElementById("Rno").value;
const Password = document.getElementById("pass").value;
if (Rno == null || Rno == "" || Password.length < 7) {
alert("Please enter a Room No or your password is incorrect");
setTimeout(function () {
window.location.reload();
}, 5000);
}

e.preventDefault();
})
</script>

</body>
</html>

在运行上面的jsp代码之后,它将正确地检查密码长度和Rno检查,但在下面给出的代码之后不会运行servlet。

如果可能的话,我想在doPost上做这件事。

并添加";else document.locationhref='登录服务器'"在javascript部分返回错误状态405-不允许的方法

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

PrintWriter out = response.getWriter();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","root");
int roomNo = Integer.parseInt(request.getParameter("Roomno"));
String password = request.getParameter("password");
PreparedStatement ps = con.prepareStatement("select * from login where roomNo=? and password=?");
ps.setInt(1, roomNo);
ps.setString(2, password);
ResultSet rs =  ps.executeQuery();

if(rs.next()) {
int userRoom = rs.getInt(1);
String userName = rs.getString(3);
out.println("RoomNo:"+userRoom+" is allocated to "+ userName);
con.close();
}
else
out.println("Failed to log in");

} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

在JSP页面中,您使用的HTML<form>具有action属性,用于将URL值绑定到servlet。servlet使用通过web.xml创建的<servlet-mapping>配置公开自己,如果您在应用程序中至少使用servlet 3.0规范,则使用annotarions公开自己。

要与servlet通信,您应该从浏览器发出HTTP请求。有很多方法可以从页面发送HTTP请求,但您使用的是HTML<form>标记,当您按下提交按钮时,请求将转到<form>标记的action属性中包含的URL,method属性中使用的HTTP方法。

如果在表单的提交按钮上绑定了特定的事件处理程序,那么在代码中应该允许函数继续处理事件队列。但是您的处理程序是这样编写的,即它不执行事件,但也重新加载页面。通过这种方式,从浏览器发出一个新的HTTP请求,并使用servlet未实现的HTTPGET方法。错误405被发送到浏览器,因为servlet中没有doGet()方法。

该问题的解决方案是删除重新加载页面的代码,并仅在密码不正确的情况下使用preventDefault()