蜗牛—JavaWeb之个人信息管理系统(一)

这次的应用也是从书上得来的。代码量相对比较多。需要MySQL数据库操作。用的struts2框架。页面嵌入java代码。

功能介绍:

                 1、进入登录页面,如果有帐号和密码,可以进入。否则,需要点击下面注册按钮。进入注册后可以进入。

                 2、登录成功后:可以看到有四个模块。然后可以点击导航拦的退出系统进行退出。

                 3、个人信息模块:可以修改自己的信息,修改自己的登录密码。

                 4、朋友模块:可以对朋友进行增删改查。

                 5、日程模块:可以对日程进行增删改查

                 6、文件模块:模拟上传文件和下载文件、删除文件。


首先,创建数据库personmessage,由于此应用代码涉及比较多。所以,一次性把所有代码写全。表方面的代码如下:

/*
Navicat MySQL Data Transfer
Source Host     : localhost:3306
Source Database : personmessage
Target Host     : localhost:3306
Target Database : personmessage
Date: 2012-03-26 09:15:40
*/

SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for date
-- ----------------------------
DROP TABLE IF EXISTS `date`;
CREATE TABLE `date` (
  `userName` varchar(30) NOT NULL,
  `date` varchar(30) NOT NULL,
  `thing` varchar(30) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;

-- ----------------------------
-- Records of date
-- ----------------------------
INSERT INTO `date` VALUES ('zzf', '2012-10-01', '少林寺旅游!');
INSERT INTO `date` VALUES ('zzf', '2013-01-01', '看升国旗!');

-- ----------------------------
-- Table structure for file
-- ----------------------------
DROP TABLE IF EXISTS `file`;
CREATE TABLE `file` (
  `userName` varchar(255) NOT NULL,
  `title` varchar(255) NOT NULL,
  `name` varchar(255) NOT NULL,
  `contentType` varchar(255) NOT NULL,
  `size` varchar(255) NOT NULL,
  `filePath` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;

-- ----------------------------
-- Records of file
-- ----------------------------
INSERT INTO `file` VALUES ('zzf', '校训', 'a.jpg', 'image/bmp', '689.51K', 'D:/Web框架技术(Struts2+Hibernate+Spring3)教程/ch04/build/web/save/a.jpg');
INSERT INTO `file` VALUES ('zzf', 'cc', 'cc.gif', 'image/bmp', '129.04K', 'D:/Web框架技术(Struts2+Hibernate+Spring3)教程/ch04/build/web/save/cc.gif');

-- ----------------------------
-- Table structure for friends
-- ----------------------------
DROP TABLE IF EXISTS `friends`;
CREATE TABLE `friends` (
  `userName` varchar(30) NOT NULL,
  `name` varchar(30) NOT NULL,
  `phone` varchar(30) NOT NULL,
  `email` varchar(30) NOT NULL,
  `workplace` varchar(30) NOT NULL,
  `place` varchar(30) NOT NULL,
  `QQ` varchar(30) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;

-- ----------------------------
-- Records of friends
-- ----------------------------
INSERT INTO `friends` VALUES ('zzf', '吕文娟', '56561234', '[email protected]', '郑州', '郑州', '66556655');
INSERT INTO `friends` VALUES ('zzf', '郑贝贝', '56566655', '[email protected]', '郑州', '郑州', '6666556');

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `userName` varchar(30) NOT NULL,
  `password` varchar(30) NOT NULL,
  `name` varchar(30) NOT NULL,
  `sex` varchar(30) NOT NULL,
  `birth` varchar(30) NOT NULL,
  `nation` varchar(30) NOT NULL,
  `edu` varchar(30) NOT NULL,
  `work` varchar(30) NOT NULL,
  `phone` varchar(30) NOT NULL,
  `place` varchar(30) NOT NULL,
  `email` varchar(30) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('zzf', 'zzf', '蜗牛', '男', '1980-12-06', '汉族', '本科', '学生', '56565566', '山东', '[email protected]');
接下来,写登录页面,一个表单和一个注册按钮。

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>


    
        
        <s:text name="个人信息管理系统"/>
    
    
        
            
为之则易,不为则难!

个人信息管理系统


长城
     
注册
如果有帐号,输入帐号后,点击登录将会调用LoginAction。下面写一个LoginAction.java,此类继承于ActionSupport类并且实现了一个ServletRequestAware接口,此接口必须实现一个setServletRequest()方法获取request对象。validate()方法中,主要意思是:利用本次请求的request、从数据库中看是否有这个登录名和密码。selectLogin()方法是根据登录名和密码从数据库中找。如果有这条记录就返回一个ResultSet的指针。默认调用execute()方法,如果登录成功,则会调用addList()方法,把此用户的四个基本模块加载。

package edu.login.Action;

import DBJavaBean.DB;
import com.opensymphony.xwork2.ActionSupport;
import java.sql.*;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

public class LoginAction extends ActionSupport implements ServletRequestAware{
    private String userName;
    private String password;
    private ResultSet rs=null;
    private String message=ERROR;
    private HttpServletRequest request;
    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;
    }
    public void setServletRequest(HttpServletRequest hsr) {
        request=hsr;
    }
    public void validate(){
        if(this.getUserName()==null||this.getUserName().length()==0){
            addFieldError("username","请输入登录名字!");
        }else{
            try{
                DB mysql=new DB();
                rs=mysql.selectMess(request, this.getUserName());
                if(!rs.next()){
                    addFieldError("username","此用户尚未注册!");
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        if(this.getPassword()==null||this.getPassword().length()==0){
            addFieldError("password","请输入登录密码!");
        }else{
            try{
                DB mysql=new DB();
                rs=mysql.selectMess(request, this.getUserName());
                if(rs.next()){
                    rs=mysql.selectLogin(request, this.getUserName(), this.getPassword());
                    if(!rs.next()){
                        addFieldError("password","登录密码错误!");
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
     public String execute() throws Exception {
        DB mysql=new DB();
        String add=mysql.addList(request, this.getUserName());
        if(add.equals("ok")){
            message="SUCCESS";
        }
        return message;
    }
//    public void message(String msg){
//        int type=JOptionPane.YES_NO_OPTION;
//        String title="信息提示";
//        JOptionPane.showMessageDialog(null,msg,title,type);
//    }
}

接下来,写关于注册功能的页面。register.jsp 一个列表。action="registerAction"

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>


    
        
        <s:text name="个人信息管理系统->注册"/>
    
    
        
            
              返回
下面是RegisterAction.java.除了一些get/set方法外。就是判断一下此用户名是否被注册过。execute()方法中,把注册的信息通过insertMess()方法插入到数据库中。

package edu.login.Action;

import DBJavaBean.DB;
import com.opensymphony.xwork2.ActionSupport;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
//import javax.swing.JOptionPane;
import org.apache.struts2.interceptor.ServletRequestAware;

public class RegisterAction extends ActionSupport implements ServletRequestAware{
    private String userName;
    private String password1;
    private String password2;
    private String name;
    private String sex;
    private String birth;
    private String nation;
    private String edu;
    private String work;
    private String phone;
    private String place;
    private String email;
    private ResultSet rs=null;
    private String message="ERROR";
    private HttpServletRequest request;
   public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword1() {
        return password1;
    }
    public void setPassword1(String password1) {
        this.password1 = password1;
    }
    public String getPassword2() {
        return password2;
    }
    public void setPassword2(String password2) {
        this.password2 = password2;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getBirth() {
        return birth;
    }
    public void setBirth(String birth) {
        this.birth = birth;
    }
    public String getNation() {
        return nation;
    }
    public void setNation(String nation) {
        this.nation = nation;
    }
    public String getEdu() {
        return edu;
    }
    public void setEdu(String edu) {
        this.edu = edu;
    }
    public String getWork() {
        return work;
    }
    public void setWork(String work) {
        this.work = work;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getPlace() {
        return place;
    }
    public void setPlace(String place) {
        this.place = place;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public void setServletRequest(HttpServletRequest hsr) {
        request=hsr;
    }

//    public void message(String msg){
//        int type=JOptionPane.YES_NO_OPTION;
//        String title="信息提示";
//        JOptionPane.showMessageDialog(null,msg,title,type);
//    }
    @Override
    public void validate(){
        if(getUserName()==null||getUserName().length()==0){
            addFieldError("userName","登录名字不允许为空!");
        }else{
            try {
                DB mysql=new DB();
                rs=mysql.selectMess(request, this.getUserName());
                if(rs.next()){
                    addFieldError("userName","此登录名字已存在!");
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        if(getPassword1()==null||getPassword1().length()==0){
            addFieldError("password1","登录密码不允许为空!");
        }
        if(getPassword2()==null||getPassword2().length()==0){
            addFieldError("password2","重复密码不允许为空!");
        }
        if(!(getPassword1().equals(getPassword2()))){
            addFieldError("password2","两次密码不一致!");
        }
        if(getName()==null||getName().length()==0){
            addFieldError("name","用户姓名不允许为空!");
        }
        if(getBirth()==null||getBirth().length()==0||getBirth().equals("yyyy-mm-dd")){
            addFieldError("birth","用户生日不允许为空!");
        }else{
            if(getBirth().length()!=10){
                addFieldError("birth","用户生日格式为'yyyy-mm-dd'!");
            }else{
                String an=this.getBirth().substring(4, 5);
                String bn=this.getBirth().substring(7, 8);
                if(!(an.equals("-"))||!(bn.equals("-"))){
                    addFieldError("birth","用户生日格式为'yyyy-mm-dd'!");
                }
            }
        }
        if(getNation()==null||getNation().length()==0){
            addFieldError("nation","用户民族不允许为空!");
        }
        if(getEdu().equals("1")){
            addFieldError("edu","请选择用户学历!");
        }
        if(getWork().equals("1")){
            addFieldError("work","请选择用户工作!");
        }
        if(getPhone()==null||getPhone().length()==0){
            addFieldError("phone","用户电话不允许为空!");
        }
        if(getPlace()==null||getPlace().length()==0){
            addFieldError("place","用户地址不允许为空!");
        }
        if(getEmail()==null||getEmail().length()==0){
            addFieldError("email","用户email不允许为空!");
        }
    }
    public String execute() throws Exception{
        DB mysql=new DB();
        String mess=mysql.insertMess(request, this.getUserName(), 
        		this.getPassword1(), this.getName(), this.getSex(), 
        		this.getBirth(), this.getNation(), this.getEdu(), 
        		this.getWork(), this.getPhone(), this.getPlace(), this.getEmail());
        if(mess.equals("ok")){
            message="SUCCESS";
        }else if(mess.equals("one")){
            message="input";
        }
        return message;
    }
}

代码量比较多,看起来应该没问题。都是一些简单易懂的东西。

先写到这吧。



你可能感兴趣的:(JavaWeb)