【发布时间】:2019-11-25 02:41:11
【问题描述】:
在我的 login.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();
}
%>
这些是我当前的代码,但没有按我的意愿将值插入到我的数据库中。
【问题讨论】:
标签: java html jsp session-variables backend