2014-11-05 64 views
1

我在JSP中創建了一個登錄腳本。它工作正常,但問題,因爲我使用 <%=session.getAttribute("first_name")%>它顯示Null。你能告訴我這裏有什麼問題嗎?用戶名不顯示在數據庫的網頁中?

Main.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 

    <% 
    if ((session.getAttribute("user_email") == null) || (session.getAttribute("user_email") == "")) { 
     response.sendRedirect("login.jsp"); 
%> 
<%} else 
%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Welcome to the job seeker forum.</title> 
<link rel="stylesheet" type="text/css" href="CSS_styles/forum_style.css"/> 
<link rel='stylesheet' id='open-sans-css' href='//fonts.googleapis.com/css?family=Open+Sans%3A300italic%2C400italic%2C600italic%2C300%2C400%2C600&#038;subset=latin%2Clatin-ext&#038;ver=4.1-alpha-20141011' type='text/css' media='all' /> 
<link href='http://fonts.googleapis.com/css?family=Londrina+Solid|Exo:800|Open+Sans:300italic,400,300,600|Roboto:400,100,300,500,700' rel='stylesheet' type='text/css' /> 
</head> 
<body> 
    <div id="forum"> 
     <h1><b>Welcome <%=session.getAttribute("first_name")%></b>,<br>to Job seeker forum, there are many openings that you can get through here. <a href='logout.jsp'>logout</a></h1> 
     <form id="forum_form" method="post"> 
     </form> 
    </div><br><br><br> 
    <div id="forum2"> 
     <h1 style="text-align: center;">Want to post on going walking,<br>Post your job description here.</h1> 
     <form id="forum2_form" method="post"> 
      <p> 
      <label>Job title:</label> 
      <input type="text" class="" placeholder="e.g XXX Referral program for freshers."/> 
      </p> 
      <p> 
      <label>Your Question:</label> 
      <textarea class="question_ask_style" rows="3" cols="40" placeholder="Type description here.."></textarea> 
      </p> 

      <div id="submit_btn"> 
      <input type="submit" value="Submit Question" /> 
      </div> 
     </form> 
    </div> 
</body> 
</html> 

這裏是login_process.jsp

<%@ page import="java.sql.*" %> 

<% 
    String user_email=request.getParameter("user_email"); 
    String user_password=request.getParameter("user_password"); 

    Class.forName("com.mysql.jdbc.Driver"); 
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:8888/login", "root", "1234"); 
    Statement st=con.createStatement(); 
    ResultSet rs; 
    rs=st.executeQuery("select * from members where email='"+user_email+"' and password='"+user_password+"'"); 

    if(rs.next()){ 
     session.setAttribute("user_email", user_email); 
     response.sendRedirect("main.jsp"); 
    }else{ 
     out.println("Invalid Password... Try again.. <a href='login.jsp'>login</a>"); 
    } 
%> 

這裏是我創建database table

create table `members`(
`id` int(10) unsigned NOT NULL auto_increment, 
`email` varchar(100) NOT NULL, 
`password` varchar(100) NOT NULL, 
`first_name` varchar(50) NOT NULL, 
`last_name` varchar(50) NOT NULL, 
PRIMARY KEY(`id`) 
)ENGINE= InnoDB default charset=latin1; 

這是我得到的錯誤...

屏幕截圖http://i.stack.imgur.com/AuF5M.png

+2

你沒有設置'first_name'會話屬性,所以顯然不能得到它。 – 2014-11-05 06:56:16

+0

where is session.setAttribute()for first_name? – Anptk 2014-11-05 06:58:12

+0

所以,如何獲得會話屬性,我只是被卡住了..如何編寫它因爲它從來沒有從數據庫調用.. – 2014-11-05 06:59:03

回答

0

login_process.jsp使用<%=session.setAttribute("first_name",first_name)%>然後訪問它。 例如:

rs=st.executeQuery("select * from members where email='"+user_email+"' and password='"+user_password+"'"); 

     if(rs.next()){ 
      session.setAttribute("first_name", rs.getString("first_name"));//set first_name in session 
      session.setAttribute("user_email", user_email); 

      response.sendRedirect("main.jsp"); 


    } 
1

<%=session.getAttribute("first_name")%>顯示null因爲你沒有設置在會話中first_name

您已設置唯一的user_email

if(rs.next()){ 
     String user_email=rs.getString("first_name"); 
     session.setAttribute("user_email", user_email); 
     response.sendRedirect("main.jsp"); 

你可以設置像上面

P.S

小腳本都望而卻步過去的幾十年,所以建議使用,而不是他們EL。你可以簡單地使用${sessionScope.user_email}在jsp頁面打印出來

+0

所以,如何獲得'會話屬性',我只是卡住..如何編寫它因爲它從來沒有從數據庫調用.. – 2014-11-05 06:58:44

+0

您可以將它設置爲login_process.jsp爲'<%= session.setAttribute(「first_name」 ,first_name)%>' – 2014-11-05 07:00:44

+0

但是老兄,它說'first_name'是未知變量..那麼如何初始化它? ..我真的被困住了..請幫助:( – 2014-11-05 07:02:56

0

嘗試此

if(rs.next()){ 
    session.setAttribute("user_email", user_email); 

    session.setAttribute("first_name", rs.getString(4));//Add this line to your code 


    response.sendRedirect("main.jsp"); 
} 

這裏圖4是列數爲first_name的

0
在login_process.jsp

設置

session.setAttribute("user_email", user_email); 

session.setAttribute("first_name", rs.getString("first_name")); 

,因爲您已設置屬性名稱user_email並從會話獲取first_name。