jsp+javabean实现简单的用户登陆

1:简单的登陆界面的绘制

<div class="login">
        <form action="doLogin.jsp" method="post">
            <tr>
                <td>username</td>
                <td><input type="text" name="username" value="">
                </td>
            </tr>
            <br>
            <tr>
                <td>password</td>
                <td><input type="password" name="password" value="">
                </td>
            </tr>
            <br>
            <tr>
                <td colspan=2 align="center"><input type="submit" value="login">
                </td>
            </tr>
        </form>
    </div>

2:建立javabean用户类

package com.entity;
//登陆的用户类
public class Users {

    private String username;
    private String password;
    public Users(){
        
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
    
}

3:建立处理业务逻辑的javabean

package com.dao;
import com.entity.Users;
//用户的业务逻辑类
public class UserDAO {
    //用户登陆方法
    public boolean UserLogin(Users u)
    {
      if("admin".equals(u.getPassword())&&"admin".equals(u.getPassword()))
      {
          return true;
      }
      else
      {
          return false;
      }
    }
}

4:对输入登陆账号密码的管理

<%@ page language="java" import="java.util.*"
    contentType="text/html; charset=utf-8"%>
<%
    request.setCharacterEncoding("utf-8");
%>
<jsp:useBean id="loginUser" class="com.entity.Users" scope="page"></jsp:useBean>
<jsp:useBean id="UserDAO" class="com.dao.UserDAO" scope="page"></jsp:useBean>
<jsp:setProperty property="*" name="loginUser" />
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";

    if (UserDAO.UserLogin(loginUser)) {
        session.setAttribute("loginusername", loginUser.getUsername());
        request.getRequestDispatcher("login_success.jsp").forward(
                request, response);
        //请求重定向,成功后跳到的是dologin.jsp页面,客户端行为,相当于两次请求
    } else {
        response.sendRedirect("login_failure.jsp");
        //请求转发,成功后跳转到login_failure.jsp页面,服务端行为,相当于一次请求
    }
%>

 

posted @ 2015-01-29 15:23  BigShui  阅读(845)  评论(0编辑  收藏  举报