一个用MVC实现登陆的程序让我迷茫!!
我想用MVC模式实现用户登陆,但程序老是报错,由于本人能力有限,所以希望大家帮我看看程序究竟哪里错了,在此先谢谢了!程序如下:登陆页面:
<%@ page contentType="text/html;charset=GB2312"%>
<html>
<head><title>登陆界面</title></head>
<body>
<center> <%//注意跟Java的括号范围也有关系%>
<h1>登陆界面</h1>
<hr>
<form action="login" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="users"></tr>
<tr>
<td>密??码:</td>
<td><input type="password" name="password"></td></tr>
<tr>
<td colspan=2 align="right"><input type="submit" value="登陆">??<input type="reset" value="清空"></td></tr>
</table>
</form>
<%
if(request.getAttribute("error")!=null)
{
%>
<%=request.getAttribute("error")%>
<%
}
%>
</center>
</body>
</html>
成功登陆页面:
<%@ page contentType="text/html;charset=GB2312"%>
<html>
<head><title>登陆成功</title></head>
<body>
<center>
<h1>登陆成功</h1>
<hr>
<%
String str=(String)session.getAttribute("name");
if(str!=null)
{
%>
<h1>欢迎<font color="red" size=12><%=session.getAttribute("name")%></font>你登陆本页</h1><br>
<%
}
else
{
response.setHeader("refresh","5;url=login.jsp");
%>
<font size=5>你还没有登陆,请先登陆!!
<br>
5秒后自动跳转到登陆页<br>
如果没有跳转,请按<a href="login.jsp">这里</a></font>
<%
}
%>
</center>
</body>
</html>
package bb.person;
public class person
{
String users=null;
String password=null;
public person()
{
}
public void setUsers(String users)
{
this.users=users;
}
public void setPassword(String password)
{
this.password=password;
}
public String getUsers()
{
return this.users;
}
public String getPassword()
{
return this.password;
}
}
package bb.loginServlet;
import javax.servlet.*;
import javax.servlet.http.*;
import bb.person.*;
import bb.Factory.*;
import bb.loginDAO.*;
import java.io.*;
public class loginServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
this.doPost(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
String path="login.jsp";
String users=request.getParameter("users");
String password=request.getParameter("password");
person p=new person();
p.setUsers(users);
p.setPassword(password);
try
{
if(Factory.getLoginDAOImpl().login(p))
{
path="login_success.jsp";
request.getSession().setAttribute("name",users);
}
else
{
request.setAttribute("error","用户名和密码不匹配!!");
}
request.getRequestDispatcher(path).forward(request,response);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
数据库数据验证类:
package bb.loginDAOImpl;
import bb.loginDAO.*;
import bb.person.*;
import bb.DataBaseConnection.*;
import java.sql.*;
import java.util.*;
public class loginDAOImpl implements loginDAO
{
String sql=null;
PreparedStatement ps=null;
ResultSet rs=null;
boolean flag=false;
DataBaseConnection conn=null;
public boolean login(person lo)
{
try
{
sql="select * from land where users=? and password=?";
conn=new DataBaseConnection();
ps=conn.getBataConnection().prepareStatement(sql);
ps.setString(1,lo.getUsers());
ps.setString(2,lo.getPassword());
rs=ps.executeQuery();
while(rs.next())
{
flag=true;
}
rs.close();
ps.close();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
conn.close();
}
return flag;
}
}
package bb.loginDAO;
import bb.person.*;
public interface loginDAO
{
public boolean login(person lo);
}
工厂类:
package bb.Factory;
import bb.loginDAOImpl.*;
public class Factory
{
public static loginDAOImpl getLoginDAOImpl()
{
return new loginDAOImpl();
}
}
数据库链接类:
package bb.DataBaseConnection;
import java.sql.*;
public class DataBaseConnection
{
private final String DBDRIVER="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private final String DBURL="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pet";
private Connection conn=null;
public DataBaseConnection()
{
}
public Connection getBataConnection()
{
try
{
Class.forName(DBDRIVER).newInstance();
conn=DriverManager.getConnection(DBURL,"sa","123456");
}
catch(Exception e)
{
}
return this.conn;
}
public void close()
{
try
{
this.conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
[[it] 本帖最后由 Javapet 于 2008-4-10 20:04 编辑 [/it]]
搜索更多相关主题的帖子:
MVC 登陆
----------------解决方案--------------------------------------------------------
问题已经解决了
----------------解决方案--------------------------------------------------------
哥们!
我虽然不知道你抱的是什么错误!但是你那个验证的方法
while(rs.next*()){
person.setUsers(rs.getString(1));
}
该是这样的,因为你设置的权限不把这个获取出来,就算程序没有错误,始终的进不去的,因为session没有得到你的用户名
----------------解决方案--------------------------------------------------------