几周来一直在玩我发现的东西,我决定将一个小的 Java Web 应用程序放在一起,您需要在其中登录。登录详细信息取自 mysql 数据库。这一切都是通过netbeans完成的。我已将 mysql 驱动程序添加到我的应用程序中,并且在我的 Web 服务器(glassfish)中,我已经连接到我的数据库。但是我一直遇到的问题是,一旦我点击登录,它就会转到我的“ReadLogin.jsp”并且不会从那里移动。用户名和密码是正确的,所以我不知道问题所在。
我的三个页面的代码如下
登录.jsp
<%
String error = request.getParameter("error");
if (error == null || error == "null") {
error = "";
}
%>
<html>
<head>
<script>
function trim(s) {
return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
}
function validate() {
if (trim(document.frmLogin.UserName.value) == "") {
alert("Login empty");
document.frmLogin.UserName.focus();
return false;
} else if(trim(document.frmLogin.Pwd.value)=="") {
alert("password empty");
document.frmLogin.Pwd.focus();
return false;
}
}
</script>
</head>
<body>
<div><%=error%></div>
<form name="frmLogin" onSubmit="return validate();" action="ReadLogin.jsp" method="post"> <%--return validate prompts script above to check if any text boxes are left empty--%>
User Name <input type="text" name="UserName" /><br />
Password <input type="password" name="Pwd" /><br />
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
下一页是“ReadLogin.jsp”
<%
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root", "nbuser");
ResultSet rsdoLogin = null;
PreparedStatement psReadLogin = null;
String sUserID = request.getParameter("UserName");
String sPassword = request.getParameter("Pwd");
String message = "User login successfully ";
try {
String sqlOption="SELECT * FROM USERS where"
+" user_id='"+sUserID+"' and password'"+sPassword+"'";
psReadLogin = conn.prepareStatement(sqlOption);
psReadLogin.setString(1, sUserID);
psReadLogin.setString(2, sPassword);
rsdoLogin=psReadLogin.executeQuery();
if(rsdoLogin.next()) {
response.sendRedirect("success.jsp?error="+message);
} else {
message="No user or password matched" ;
response.sendRedirect("login.jsp?error="+message);
}
}
catch(Exception e)
{
e.printStackTrace();
}
/// close object and connection
try{
if(psReadLogin!=null){
psReadLogin.close();
}
if(rsdoLogin!=null){
rsdoLogin.close();
}
if(conn!=null){
conn.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
%>
那么输出页面应该只是一个简单的“success.jsp”
<html>
<body>
<h1>Hello World!</h1>
</body>
</html>
我的 readlogig 文件一定有问题,因为我玩过另一个版本,我可以从 jsp 表单将数据放入数据库,但我无法让它执行登录功能,这将是主要功能到我正在尝试开发的小工具