java如何使用会话从一个jsp页面访问另一个jsp页面的输入值?
在我的登录中。我使用了以下代码。我创建了一个会话,并打算传递用户将插入的用户名和电子邮件。
<%
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)
{
}
%>
在另一个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();
}
%>
这些是我当前的代码,但没有像我希望的那样将任何值插入到我的数据库中
# 1 楼答案
在第一个jsp中,您将会话变量的值设置为字符串,而不是传递用户名和密码变量
因此,与其说:
试试看
此外,这个问题可能与不在活动状态下维护会话有关,tomcat中会话的默认行为是使用带有键JSESSIONID的cookie。如果cookie是在第二次jsp调用中设置和发送的,您可以使用浏览器开发工具进行检查
在你可以使用的代码里面
查看两次呼叫之间的会话是否相同