Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用会话从一个jsp页面访问另一个jsp页面的输入值?_Java_Html_Jsp_Session Variables_Backend - Fatal编程技术网

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

Java 如何使用会话从一个jsp页面访问另一个jsp页面的输入值?,java,html,jsp,session-variables,backend,Java,Html,Jsp,Session Variables,Backend,在我的login.jsp中,我使用了以下代码。我创建了一个会话,并打算传递用户将插入的用户名和电子邮件。 海 在另一个jsp页面中,我需要访问这些值,即用户名和电子邮件,以便将其插入数据库 <body> <% HttpSession sess = request.getSession(false); String username=sess.getAttribute("username").toString(); String email=sess.getAttr

在我的login.jsp中,我使用了以下代码。我创建了一个会话,并打算传递用户将插入的用户名和电子邮件。

在另一个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();
}
%>



这些是我当前的代码,但是没有像我希望的那样将任何值插入到我的数据库中。

在第一个jsp中,您将会话变量的值设置为字符串,而不是传递用户名和密码变量

因此,不是:

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

此外,这个问题可能与不在活动状态下维护会话有关,tomcat中会话的默认行为是使用带有键JSESSIONID的cookie。如果cookie是在第二次jsp调用中设置和发送的,您可以使用浏览器开发工具检查该cookie

在代码内部,您可以使用

request.getSession().getId()
查看两次调用之间的会话是否相同

sess.setAttribute("username",username);
sess.setAttribute("email",email);
request.getSession().getId()