您好,我正在編寫一個登錄註冊頁面。 Page正在用數據庫檢查用戶名密碼,當它正確時,用戶可以訪問新的jsp頁面。並打印出「歡迎+用戶名」。我怎樣才能做到這一點 ?我在互聯網上搜索,我在代碼下面搜索。但它不起作用。當我運行它返回「歡迎null」。我想這的success.jsp在jsp中獲取會話的屬性
的success.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body background="http://www.teamarking.com/barcode/bar_background.jpg">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<ul>
<li><%
out.println("<a href='music.jsp'>Music</a>");
%>
</li>
<li><%
out.println("<a href='sport.jsp'>Sport</a>");
%></li>
<li> <%
out.println("<a href='stage.jsp'>Stage</a>");
%></li>
<li> <%
out.println("<a href='family.jsp'>Family</a>");
%></li>
<li> <%
out.println("<a href='education.jsp'>Education</a>");
%></li>
</ul>
<div class="landscape">
<%
String username = (String) request.getSession().getAttribute("username");
session.setAttribute("username", username);
out.println("welcome" + username);
out.println(
"<a href='logout.jsp'>Log out</a>");
out.println(
"<a href='myreservations.jsp'>My Reservations</a>");
%>
</div>
</body>
</html>
的login.jsp
<%@ page import ="java.sql.*" %>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/users", "users", "123");
Statement st = con.createStatement();
ResultSet rs;
rs = st.executeQuery("select * from users where username='" + username + "' and password='" + password + "'");
if (rs.next()) {
session.setAttribute("username", username);
response.sendRedirect("success.jsp");
} else {
out.println("Invalid password <a href='index.jsp'>try again</a>");
}
st.close();
%>
請問您可以在代碼中將「用戶名」放入會話屬性中嗎? –
我更新了我的代碼 – tripley