你好我正在写一个登录注册页面。页面用数据库检查用户名密码,如果正确,用户可以访问新的jsp页面。它打印出“welcome + username”。我怎样才能做到这一点 ?我在互联网上搜索,我在代码下面。但它不起作用。当我运行它时返回“welcome null”。我希望这成功.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();
%>
答案 0 :(得分:0)
在您阅读属性之前&#34;用户名&#34;你必须设置属性。因此,您必须使用函数setAttribute()。
print(1.__add__(2))
检查或验证用户名和密码后,您必须使用此代码。您能否显示其余代码(验证发生的部分)?