我想验证数据存储在数据库中的用户和密码。我在我的decelopment中使用Netbeans IDE。我遇到的问题是它一直显示内容服务器错误的错误页面。有人可以帮我检查我的代码。我有一些像这样的代码:
loginForm.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:
<%@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>
我的数据库名称是learningApp,密码为p @ ssw0rd 并有一个名为USERNAME的表,其中包含用户(username = alice,password = alice)
答案 0 :(得分:0)
您可能希望将derbyclient.jar
和derby.jar
放在WebContent/WEB-INF/Lib
中,并在构建路径中包含相同内容。
更新
我将您的代码(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>