| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1307 人关注过本帖
标题:一个用MVC实现登陆的程序让我迷茫!!
只看楼主 加入收藏
Javapet
Rank: 1
等 级:禁止访问
帖 子:80
专家分:0
注 册:2008-1-5
结帖率:50%
收藏
 问题点数:0 回复次数:2 
一个用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 *;
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 登陆 
2008-04-10 15:04
Javapet
Rank: 1
等 级:禁止访问
帖 子:80
专家分:0
注 册:2008-1-5
收藏
得分:0 
问题已经解决了
2008-04-10 21:08
zhangyan297
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-4-2
收藏
得分:0 
哥们!
我虽然不知道你抱的是什么错误!但是
你那个验证的方法
while(rs.next*()){
  person.setUsers(rs.getString(1));
}
该是这样的,因为你设置的权限不把这个获取出来,就算程序没有错误,始终的进不去的,因为session没有得到你的用户名
2008-04-14 23:41
快速回复:一个用MVC实现登陆的程序让我迷茫!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.048049 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved