【问题标题】:User name is not showing in web page from database?用户名未显示在数据库的网页中?
【发布时间】:2015-01-01 07:07:54
【问题描述】:

我在 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

【问题讨论】:

  • 你没有设置first_name会话属性,所以显然无法获取。
  • first_name 的 session.setAttribute() 在哪里?
  • 那么,如何获取会话属性,我只是卡住了..如何编写它,因为它从不从数据库调用..
  • 您可以从 ResultSet rs 中获取 first_name,因为您正在从 Menmbers 表中查询 Select *,您可以从 members 表中获取 first_name 详细信息。

标签: java mysql database jsp servlets


【解决方案1】:

在 login_process.jsp 中设置

session.setAttribute("user_email", user_email);

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

因为您已经设置了属性名称 user_email 并从会话中获取了 first_name。

【讨论】:

    【解决方案2】:

    &lt;%=session.getAttribute("first_name")%&gt; 显示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");
    

    你可以像上面那样设置

    附言

    Scriptlet 几十年来一直不鼓励使用,因此建议使用 EL 代替它们。您可以简单地使用,${sessionScope.user_email}在jsp页面中打印出来

    【讨论】:

    • 那么,如何获得session attribute,我只是卡住了.. 怎么写,因为它从不从数据库调用..
    • 你可以在 login_process.jsp 上设置为&lt;%=session.setAttribute("first_name",first_name)%&gt;
    • 但是老兄,它说first_name 是未知变量.. 那么如何初始化它呢? ..我真的被卡住了..请帮助:(
    • 你在members 表中有名字吗?
    【解决方案3】:

    试试这个

    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 的列号

    【讨论】:

      【解决方案4】:

      login_process.jsp 中使用&lt;%=session.setAttribute("first_name",first_name)%&gt; 然后访问它。 例如:

          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");
      
      
         }
      

      【讨论】:

        猜你喜欢
        • 2019-02-08
        • 2015-01-29
        • 1970-01-01
        • 2020-06-14
        • 1970-01-01
        • 1970-01-01
        • 2018-11-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多