jsp新手求教, 求帮忙
且看这个小程序,问题在最后
//登录判断用户存在方法:
public User login() throws
SQLException{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
User user = null;
conn=DBConnection.getConnection();
String sql = "select * from [user] where name = ? and password = ?";
ps = conn.prepareStatement(sql);
ps.setString(1,name);
ps.setString(2,password);
rs=ps.executeQuery();
while(rs.next()){
user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setEmail(rs.getString("email"));
user.setAge(rs.getInt("age"));
user.setBirthday(rs.getDate("birthday"));
}
conn.close();
ps.close();
return user;
}
以下是登录处理界面,点submit跳转过来
<body>
<jsp:useBean id = "user" class = "jspjavabean.User"/>
<jsp:setProperty name = "user" property = "*" />
<%
user = user.login();
if(user!=null){
session.setAttribute("user", user);
%>
<jsp:forward page = "user.jsp"/>
<%
}
else{
%>
<jsp:forward page = "loginFail.jsp"/>
<%
}
%>
</body>
问题是:为什么user = user.login();这句不能接收信息? 每次都是null?
------解决方案--------------------哦,还有这段:
if(user!=null){
session.setAttribute("user", user);
改为:
if(user.login()!=null){
session.setAttribute("user", user.login);
楼主的命名规则。。。多多改进吧
------解决方案--------------------把我上面提到的问题改掉,然后
if (user.login()!= null)
{
session.setAttribute("newUser", user.login());
这个地方改掉就OK了。