database 使用数据库用户进行jsp登录认证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24548362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jsp login authentication with database user
提问by raymond_wmt
i would like to authenticate user and password which the data store in database. I'm using Netbeans IDE in my decelopment. The problem I faced is it keep show an error page with content internal server error. Can someone one help me to check my code. I have some code like this:
我想验证数据存储在数据库中的用户和密码。我在我的开发中使用 Netbeans IDE。我面临的问题是它一直显示一个带有内容内部服务器错误的错误页面。有人可以帮我检查我的代码。我有一些这样的代码:
LoginForm.jsp:
登录表单.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Form</title>
</head>
<body bgcolor="#D8CEF6">
<h1>Login Page</h1>
<fieldset>
<center>
<legend><h2>Sign in Details</h2> </legend>
<form action="LoginCheck.jsp" method="post">
<br/>Username:<input type="text" name="username"/>
<br/>Password:<input type="password" name="password"/>
<br/>
<tr>
<td>usertype</td>
<td>
<select name="usertype">
<option value="student">student</option>
<option value="faculty">faculty</option>
</select>
</td>
</tr>
<tr>
<td>
<br/><input type="submit" value="Submit" name="bt"/>
</td>
</tr>
</form>
</center>
</fieldset>
</body>
</html>
LoginCheck.jsp:
登录检查.jsp:
<%@page import ="java.sql.*" %>
<%@page import ="java.io.IOException" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.io.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Check</title>
</head>
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
String userType = request.getParameter("usertype");
String dbURL = "jdbc:derby://localhost:1527/learningApp";
String dbuser = "learningApp";
String dbpassword = "p@ssw0rd";
Connection theConnection = null;
PreparedStatement theStatement = theConnection.prepareStatement("select * from USERNAME where username=? and password=?");
try{
Class.forName("org.apache.derby.jdbc.ClientDriver");
}catch(Exception ex){
System.out.println("Class Not Found");
}
try{
theConnection = DriverManager.getConnection(dbURL, dbuser, dbpassword);
}catch(Exception ex){
System.out.println("Connection Error");
}
try{
theStatement.setString(1,request.getParameter("username"));
theStatement.setString(2,request.getParameter("password"));
ResultSet theResult = theStatement.executeQuery();
if(theResult.next()){
response.sendRedirect("Home.jsp");
}
}catch(Exception ex){
System.out.println("Statement Error");
}
%>
</body>
</html>
My database name is learningApp with password p@ssw0rd and have a table named USERNAME with a user(username = alice, password = alice)
我的数据库名称是 learningApp,密码为 p@ssw0rd 并且有一个名为 USERNAME 的表,其中包含一个用户(用户名 = alice,密码 = alice)
采纳答案by Conquest
You might want to place the derbyclient.jarand derby.jarin the WebContent/WEB-INF/Liband also include the same in the build path.
您可能希望将derbyclient.jar和放在和derby.jar中,WebContent/WEB-INF/Lib并且在构建路径中也包含相同的内容。
Update:
更新:
I changed your code (LoginCheck.jsp) to this , (I'm using Oracle DB). Seems to be working fine for me.
我将您的代码 (LoginCheck.jsp) 更改为此,(我使用的是 Oracle DB)。似乎对我来说工作正常。
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
String userType = request.getParameter("usertype");
String driver = "oracle.jdbc.driver.OracleDriver";
String dbURL = "jdbc:oracle:thin:@10.113.130.22:1521:ORCL";
String dbuser = "user";
String dbpassword = "password";
Connection theConnection = null;
PreparedStatement theStatement = null;
try{
Class.forName(driver);
theConnection=DriverManager.getConnection(dbURL,dbuser,dbpassword);
theStatement = theConnection.prepareStatement("select * from USERNAME where username=? and password=?");
theStatement.setString(1,request.getParameter("username"));
theStatement.setString(2,request.getParameter("password"));
ResultSet theResult = theStatement.executeQuery();
if(theResult.next())
System.out.println("Success");
else
System.out.println("Failed");
}catch(Exception e){
System.out.println("Exception occured! "+e.getMessage()+" "+e.getStackTrace());
}
%>
</body>

