这几周来我一直在研究我发现的东西,我决定组装一个需要登录的小型 Java Web 应用程序。 登录详细信息取自 mysql 数据库。 这一切都是通过netbeans 完成的。我已将 mysql 驱动程序添加到我的应用程序中,并且在我的 Web 服务器(glassfish)中我已连接到我的数据库。 但我不断遇到的问题是,一旦我点击登录,它就会转到我的“ReadLogin.jsp”并且不会从那里移动。用户名和密码正确,所以我不知道问题所在。
我的三个页面的代码如下
Login.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 表单放入数据库,但我无法让它执行登录功能,这将是我正在尝试开发的小工具的主要功能
最佳答案
String sqlOption="SELECT * FROM USERS where"
+" user_id='"+sUserID+"' and password'"+sPassword+"'";
psReadLogin = conn.prepareStatement(sqlOption);
psReadLogin.setString(1, sUserID);
psReadLogin.setString(2, sPassword);
您可以使用丑陋的字符串连接或使用 PreparedStatement
创建请求。但不要两者都做!它应该是这样的:
String sqlOption="SELECT * FROM USERS where user_id = ? and password = ?";
psReadLogin = conn.prepareStatement(sqlOption);
psReadLogin.setString(1, sUserID);
psReadLogin.setString(2, sPassword);
除此之外,由于您似乎是从头开始创建的,因此我强烈建议您停止使用这些 scriptlet,并开始阅读有关 beans、servlet、JSP 和 JSTL 的内容。
另请参阅:
关于mysql - jsp从Mysql数据库登录的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14244056/