我在寻找解决此问题的方法时遇到麻烦。
[当用户登录时(使用我的servlet),我想存储他们输入的凭据(用户名和密码),然后在普通的Java类中使用该信息。
---------这是登录Servlet代码----
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
String enteredUserName = request.getParameter("username"); //this is the same as name value from login form
String enteredPassword = request.getParameter("password");
RequestDispatcher rd = null;
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
try (Connection conn = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:xxxx/", "xxxxxx",
"xxxxxxx")) {
Statement first_stmt = null;
String first_query = "select user_name, pass_word, access_level from employees where user_name = " + "'" + enteredUserName + "';";
first_stmt = conn.createStatement();
ResultSet rs = first_stmt.executeQuery(first_query);
while (rs.next()) {
int accessLevel_db = rs.getInt("access_level");
String username_db = rs.getString("user_name");
String password_db = rs.getString("pass_word");
/*TRMSLoginDao access = new TRMSLoginDao();
//access.login(userName.toString(), password.toString());
//problem is right here accessLevel never gets updated stays at 0
if(TRMSLoginDao.login(userName, password)) {*/
if (enteredUserName.equals(username_db) && enteredPassword.equals(password_db) && accessLevel_db == 3) {
//establish session stuff
HttpSession session = request.getSession(); //create session object
session.setAttribute("username", enteredUserName); // used session to set session attribute
session.setAttribute("password", enteredPassword); // sets password attribute
//String entered_UserName = (String) session.getAttribute("username");
//establish session stuff
rd = request.getRequestDispatcher("/DistrictManagerHome.jsp");
rd.forward(request, response);
}}
我想在他们登录到网站后在此sql查询中使用他们存储的用户名
就像我想使用下面的sql语句中登录用户名的当前个人一样
-----------如在此查询中看到的----------
public void updateEmployee(Employee theEmployee) throws Exception{
Connection myConn = null;
PreparedStatement myStmt = null;
//get a connection
Class.forName("org.postgresql.Driver");
try (Connection conn = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:xxxx/", "xxxxxx",
"xxxxxxx")) {
// create SQL update Statement
String sql = "update public.employees " + "set user_name=?, pass_word=?, d_o_b=?, first_name=?, last_name=?, manager_email=?, access_level=? " + "where user_name=?;";
// Prepare Statement
myStmt = conn.prepareStatement(sql);
// set params
myStmt.setString(1, theEmployee.getUserName());
myStmt.setString(2, theEmployee.getPassWord());
myStmt.setString(3, theEmployee.getDob());
myStmt.setString(4, theEmployee.getFirstName());
myStmt.setString(5, theEmployee.getLastName());
myStmt.setString(6, theEmployee.getManagerEmail());
myStmt.setString(7, theEmployee.getAccessLevel());
myStmt.setInt(8, theEmployee.getId());
// execute statement
myStmt.executeUpdate();
}
finally {
//clean up JDBC objects
close(myConn,myStmt, null);
}
}
总之,该代码当前可以正常运行,但是,我只能在原始查询中选择*。我想根据当前用户的登录凭据进行查询。我只是在尝试通过在代码中利用用户实际输入并经过身份验证的用户名来添加更多功能,]。
我在寻找解决此问题的方法时遇到麻烦。当用户登录(使用我的servlet)时,我想存储他们输入的凭证(用户名和密码),然后使用该凭证...