简体   繁体   English

如何使用会话将输入值从一个 jsp 页面访问到另一个 jsp 页面?

[英]How to access input value from one jsp page to another jsp page using sessions?

In my login.jsp I used the following code.在我的 login.jsp 中,我使用了以下代码。 I created a session and intend to pass the username and email which the user will be inserting.我创建了一个会话并打算传递用户将插入的用户名和电子邮件。

<%
    try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/db1","root","");
        Statement st=con.createStatement();
        %>
        hai
        <%
        String username=request.getParameter("username");
        String email=request.getParameter("email");
        String password=request.getParameter("password");
        ResultSet rs=st.executeQuery("Select * from user3");
        HttpSession sess = request.getSession(); 
        sess.setAttribute("username","username");
        sess.setAttribute("email","email");

        while(rs.next())
        {
            String username=rs.getString(1);
            String email1=rs.getString(2);
            String password1=rs.getString(3);

            if((email1.equals(email)) && (password1.equals(password)))
            {
                response.sendRedirect("book.html");
            }
            else
            {
                response.sendRedirect("register.html");
            }

        }

    }
    catch(Exception e)
{

}

%> %>

In another jsp page, I need to access these values ie the username and email so that I can insert it into a database.在另一个jsp 页面中,我需要访问这些值,即用户名和电子邮件,以便我可以将其插入到数据库中。

<body>


<%
HttpSession sess = request.getSession(false); 
String username=sess.getAttribute("username").toString();
String email=sess.getAttribute("email").toString();

try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1", "root", "");
Statement st=conn.createStatement();

int i=st.executeUpdate("insert into busA(username,email)values('"+username+"','"+email+"')");
out.println("Data is successfully inserted!");
}
catch(Exception e)
{
System.out.print(e);
e.printStackTrace();
}
%>

These are my current code, but no values are being inserted into my database as I want it to.这些是我当前的代码,但没有按照我的意愿将值插入到我的数据库中。

In the first jsp you are setting the value for the session variables as strings instead of passing the username and password variables.在第一个 jsp 中,您将会话变量的值设置为字符串,而不是传递用户名和密码变量。

So instead of :所以而不是:

sess.setAttribute("username","username");
sess.setAttribute("email","email");

Try尝试

sess.setAttribute("username",username);
sess.setAttribute("email",email);

Also this issue may be relatied to not maintaning the session alive, default behavior for session in tomcat is using a cookie with the key JSESSIONID you can check with the browser dev tools if that cookie is being set and sent with the second jsp call此外,此问题可能与不保持会话处于活动状态有关,tomcat 中会话的默认行为是使用带有键 JSESSIONID 的 cookie,您可以使用浏览器开发工具检查该 cookie 是否正在设置并与第二个 jsp 调用一起发送

Inside the code you can use在您可以使用的代码中

request.getSession().getId()

To see if the session is the same between the 2 calls.查看两次调用之间的会话是否相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM