我試圖創建簡單的Web應用程序。但是我登錄到用戶界面後遇到問題。我的數據庫包括ID,用戶名,密碼和電子郵件。如何從SQL中使用java檢索會話中的數據
我已經創建了登錄表單並且它正在工作。
而在html/jsp中,我使用這個<%=session.getAttribute("name")%>
來獲得「歡迎回來USERNAME,您的電子郵件爲NULL」。
我想獲取用戶郵件。
我的代碼是在這裏:
THERE IS FIRST FILE
------------------------------------------------------------------------------------
package com.promo4free.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.promo4free.dao.LoginDao;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n = request.getParameter("username");
String p = request.getParameter("userpass");
HttpSession session = request.getSession(false);
if (session != null) session.setAttribute("name", n); // WORKING
if (LoginDao.validate(n, p)) {
RequestDispatcher rd = request.getRequestDispatcher("after_login_user_interface.jsp");
rd.forward(request, response);
} else {
out.print("<p style=\"color:red\">Sorry username or password error</p>");
RequestDispatcher rd = request.getRequestDispatcher("loginToPage.jsp");
rd.include(request, response);
}
out.close();
}
}
-----------------------------------------------------------------------------------------------------------
THERE IS SECOND FILE
-----------------------------------------------------------------------------------------------------------
package com.promo4free.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//working
public class LoginDao {
public static boolean validate(String name, String pass) {
boolean status = false;
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "ps";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url + dbName, userName, password);
pst = conn.prepareStatement("SELECT * FROM members WHERE username=? AND password=?"); //working
System.out.println("SELECT * FROM members WHERE username=? AND password=?"); //working
pst.setString(1, name);
pst.setString(2, pass);
rs = pst.executeQuery();
status = rs.next();
} catch (Exception e) {
System.out.println(e);
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pst != null) {
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return status;
}
}
-----------------------------------------------------------------------------------------------------------
THERE IS SECOND FILE web.xml
-----------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.promo4free.servlets.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
郵政相關的代碼,不要插入鏈接。 – 2015-02-08 11:31:36
感謝您的警告。現在正確 – 2015-02-08 11:48:24