0
我有一個項目進行登錄,其中的密碼是硬編碼和用戶名是從數據庫。我有一個簡單的index.jsp在哪裏輸入用戶名和密碼,info.jsp在哪裏可以訪問,如果憑證是正確的,error.jsp,當憑證是錯誤的,登錄servlet。從數據庫驗證硬編碼的密碼和用戶名
這是我登錄的servlet:
package webAccess;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Login")
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("","", "");
Statement st = con.createStatement();
ResultSet rs;
rs = st.executeQuery("select * from user where USERID=?");
if(username.equals(rs.getString("USERID")) && password.equals("password")){
response.sendRedirect("info.jsp");
}
else {
response.sendRedirect("index.jsp");
}
}
catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
中的index.jsp,它只是加載一個空格我輸入憑據後。
我改變我的代碼如下:
package webAccess;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/Login")
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String USERID = request.getParameter("username");
String PWD = request.getParameter("password");
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("","", "");
PreparedStatement ps =con.prepareStatement
("select USERID from user where USERID=?");
ps.setString(1, USERID);
ResultSet rs=ps.executeQuery();
rs.next();
if(USERID.equals(rs.getString("USERID")) && PWD.equals("password")){
response.sendRedirect("info.jsp");
}
else {
response.sendRedirect("error.jsp");
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}
如果用戶名和密碼是正確的,將重定向到info.jsp,如果用戶名是正確的,密碼是不是會重定向到error.jsp文件。但是,如果用戶名無效並且密碼正確且不正確,則會加載空白區域。
如果憑據錯誤,您將重定向到index.jsp而不是error.jsp。 –
如果發生異常,頁面會被重定向到哪裏?另外,您不應該在密碼中以明文形式存儲密碼 –
硬編碼密碼?什麼可能會出錯? :-) – paxdiablo