业务流程

开始是一个login.jsp页面。用户输入用户名和密码正确后转入main.jsp页面。登陆失败转入register.jsp页面。

 


数据库设计

/*
MySQL Data Transfer
Source Host: localhost
Source Database: user
Target Host: localhost
Target Database: user
Date: 2008-9-13 16:50:19
*/

SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for userinfo
-- ----------------------------
DROP TABLE IF EXISTS `userinfo`;
CREATE TABLE `userinfo` (
  `id` int(11) NOT NULL auto_increment,
  `userName` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;

-- ----------------------------
-- Records
-- ----------------------------
INSERT INTO `userinfo` VALUES ('1', 'anzey', 'anzey');
INSERT INTO `userinfo` VALUES ('2', 'didiao', 'didiao');

 

将以上内容保存导入到数据库中即可。


视图组件设计

视图组件包括login .jsp,register.jsp和main.jsp

login.jsp面代码如下:

<%@ page language="java" pageEncoding="GB2312"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
    <head>
        <title>JSP for loginForm form</title>
    </head>
    <body>
        <html:form action="WebRoot" method="post">
            用户名 : <html:text property="username"/><br/>
            密码 :&nbsp;&nbsp;&nbsp;&nbsp;  <html:text property="password"/><br/>
            <html:submit value="提交"/><html:cancel value="取消"/>
        </html:form>
    </body>
</html>

main.jsp页面设计

<%@ page language="java" pageEncoding="GB2312"%>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
    prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
    prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
    prefix="logic"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles"
    prefix="tiles"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-template"
    prefix="template"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-nested"
    prefix="nested"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
<head>
    <html:base />

    <title>main.jsp</title>

</head>

<body>
<%=session.getAttribute("username") %>你成功登陆,现已进入主页面!

</body>
</html:html>

 

register.jsp页面设计

<%@ page language="java" pageEncoding="GB18030"%>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-template" prefix="template" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-nested" prefix="nested" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
  <head>
    <html:base />
    <title>register.jsp</title>

  </head>
  <body>
  <h1>
<%=session.getAttribute("username") %>你未能成功登陆,<br>
现在进入注册页面!
</h1>
  </body>
</html:html>

 


构建ActionForm

loginForm.java

package struts;

import java.util.*;

import javax.servlet.http.*;
import org.apache.struts.action.*;

import Handle.LoginHandle;

public class loginAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        loginForm loginForm = (loginForm) form;
//从formbean中取出表单数据
        String username=loginForm.getUsername();
        String password=loginForm.getPassword();
        //生成一個Session對象
        HttpSession session=request.getSession(true);
        session.removeAttribute("username");
        session.setAttribute("username", username);
        //生成一个arralist对象,并把用户名和密码的值存入该对象中
        ArrayList arr=new ArrayList();
        arr.add(username);
        arr.add(password);
        String prompt;
        //调用模型组件LoginHandler,检查改用户是否已注册
        LoginHandle login =new LoginHandle();
        boolean mark=login.checkLogin(arr);
        if(mark) prompt="Success";
        else prompt="Fail";
        return mapping.findForward(prompt);
    }
}

构建action

loginAction.java

package struts;

import java.util.*;

import javax.servlet.http.*;
import org.apache.struts.action.*;

import Handle.LoginHandle;

public class loginAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        loginForm loginForm = (loginForm) form;
//从formbean中取出表单数据
        String username=loginForm.getUsername();
        String password=loginForm.getPassword();
        //生成一個Session對象
        HttpSession session=request.getSession(true);
        session.removeAttribute("username");
        session.setAttribute("username", username);
        //生成一个arralist对象,并把用户名和密码的值存入该对象中
        ArrayList arr=new ArrayList();
        arr.add(username);
        arr.add(password);
        String prompt;
        //调用模型组件LoginHandler,检查改用户是否已注册
        LoginHandle login =new LoginHandle();
        boolean mark=login.checkLogin(arr);
        if(mark) prompt="Success";
        else prompt="Fail";
        return mapping.findForward(prompt);
    }
}

 


构建控制组件

数据库连接组件

DBPool.java

package DBcon;

import java.sql.*;
import java.io.*;
import java.util.*;

public class DBPool {
    private static DBPool instance = null;

    // 取得连接
    public static synchronized Connection getConnection() {
        if (instance == null) {
            instance = new DBPool();
        }
        return instance._getConnection();
    }

    private DBPool() {
        super();
    }

    private Connection _getConnection() {

            String sDBDriver = null;
            String sConnection = null;
            String sUser = null;
            String sPassword = null;

            Properties p = new Properties();
            InputStream is = getClass().getResourceAsStream("/db.properties");
            try {
            p.load(is);}catch(Exception Ex)
            {
                System.out.print("加载properties文件失败!\n");
            }

            sDBDriver = p.getProperty("DBDriver", sDBDriver);
            sConnection = p.getProperty("Connection", sConnection);

            sUser = p.getProperty("User", "");
            sPassword = p.getProperty("Password", "");
            Properties pr = new Properties();
            pr.put("user", sUser);
            pr.put("password", sPassword);
            pr.put("characterEncoding", "GB2312");
            pr.put("useUnicode", "TRUE");
            try {
            Class.forName(sDBDriver).newInstance();
            return DriverManager.getConnection(sConnection,pr);
        } catch (Exception Ex) {
            System.out.print("加载驱动失败或者创建数据库连接失败!\n");
            return null;

        }

    }

    public static void dbClose(Connection conn, PreparedStatement ps,
            ResultSet rs) throws SQLException {
        rs.close();
        ps.close();
        conn.close();

    }

}

 

业务处理组件

package Handle;
import java.sql.*;
import java.util.*;

import DBcon.DBPool;

public class LoginHandle {
    public LoginHandle()
    {}
    Connection  conn;
    PreparedStatement ps;
    ResultSet rs;
    //检查是否已注册
    public boolean checkLogin(ArrayList arr)
    {//从数据库访问组建DBpool中取得连接
        conn=DBPool.getConnection();
        String name =(String)arr.get(0);
        String password=(String)arr.get(1);
        try
        {  
            String sql="select * from userinfo where username=? and password=?";
            ps=conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, password);
            rs=ps.executeQuery();
            if (rs.next())
            {
                DBPool.dbClose(conn,ps,rs);
                return true;
            }else{
                DBPool.dbClose(conn,ps,rs);
                return false;   
            }
        }catch(SQLException e)
        {
            return false;       
        }
    }

}

 

部署和运行

将应用程序通过myeclipse发布到Tomcat。

posted on 2008-09-13 17:11  忘记  阅读(459)  评论(0编辑  收藏  举报