jsp+jstl+servlet+javaBean+Mysql用户登录注册页面

一、首页面login.jsp用于完成用户的登录

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'login.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
    

用户登陆页面

用户名 密码

二、登录进去的主界面显示

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'index.jsp' starting page
	
	
	    
	
	
	
  
  
  	
  	
  		
  		
  	
  

三、首页面顶部显示页面

	<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    My JSP 'top.jsp' starting page
  
  
    

欢迎用户[${user.nickname}]登陆成功

您的权限为【${user.power }】 注销

四、页面左边显示代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.bjsxt.com/sxttag" prefix="sxt" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'left.jsp' starting page
  
  
  
    

菜单栏

用户模块

  1. 显示用户信息
  2. 新增用户信息

商品模块

  1. 显示商品信息
  2. 新增商品信息

评价模块

  1. 显示评价信息

五、显示模块信息的页面 放到单独的文件夹user里面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.bjsxt.user.vo.User" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<%
List userList = (List) request.getAttribute("userList");
%>



  
    
    
    My JSP 'index.jsp' starting page
   
  
   	

用户模块首页

序号 用户名 昵称 性别 创建时间 操作
${vars.count } ${each.uname } ${each.nickname } ${each.gender == '1'?"男":"女" } 修改 删除

六、添加用户的页面代码,同样放到user文件夹内

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'add.jsp' starting page
 
  
  
  
    

用户新增页面

用户名
密码
昵称
邮箱
年龄
性别

七、用户对象User,用于封装用户信息

package com.bjsxt.user.vo;

import java.util.Date;

public class User {
	private Integer id;
	private String uname;
	private String pwd;
	private String nickname;
	private String gender;
	private String email;
	private Integer age;
	private Integer power;
	private Date createTime;
	private Date updateTime;
	
	/**
	 * 构造器
	 */
	public User() {

	}

	public User(String uname, String pwd, String nickname, String gender, String email, Integer age) {
		this.uname = uname;
		this.pwd = pwd;
		this.nickname = nickname;
		this.gender = gender;
		this.email = email;
		this.age = age;
	}
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getNickname() {
		return nickname;
	}
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public Date getUpdateTime() {
		return updateTime;
	}
	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}
	
	public Integer getPower() {
		return power;
	}

	public void setPower(Integer power) {
		this.power = power;
	}
}

八、Dao层,用于数据的交换,用于验证用户的登录以及用户的注册

package com.bjsxt.user.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bjsxt.user.vo.User;
import com.bjsxt.util.DBUtil;

//用户数据的访问层
public class UserDao {
	
	/**
	 * 验证用户名和密码是否匹配
	 * @param uname
	 * @param pwd
	 * @return
	 */
	public User doLogin(String uname, String pwd) {
		User user = null;
		//声明链接
		Connection connection = null;
		PreparedStatement pstmt = null;
		ResultSet resultSet = null;
		
		//sql。若果账号密码匹配,*代表的用户所有信息,返回到resultset结果集中去
		String sql = "SELECT * FROM USER WHERE UNAME = ? AND PWD = ?";
		
		try {
			//获取链接
			connection = DBUtil.getConnection();
			pstmt = DBUtil.getPstmt(connection, sql);
			pstmt.setString(1, uname);//页面穿过来的uname
			pstmt.setString(2, pwd);//页面穿过来的pwd
			
			//执行sql
			resultSet = pstmt.executeQuery();
			//遍历结果集合
			while(resultSet.next()){
				user = new User();
				//将结果集里的信息,重新封装到user对象中去
				resultset2bean(resultSet,user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//关闭链接
			DBUtil.closeAll(connection, pstmt, resultSet);
		}
		return user;//返回封装好的user对象
	}
	/**
	 * 将结果集合里面的数据转存之对象
	 * @param resultSet
	 * @param user
	 */
	private void resultset2bean(ResultSet resultSet, User user) {
		try {
			//将resultset结果集里的信息一一分类,重新封装到user对象中去
			user.setId(resultSet.getInt("id"));
			user.setUname(resultSet.getString("uname"));
			user.setPwd(resultSet.getString("pwd"));
			user.setNickname(resultSet.getString("nickname"));
			user.setGender(resultSet.getString("gender"));
			user.setEmail(resultSet.getString("email"));
			user.setAge(resultSet.getInt("age"));
			user.setPower(resultSet.getInt("power"));
			user.setCreateTime(resultSet.getTimestamp("createTime"));
			user.setUpdateTime(resultSet.getTimestamp("updateTime"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	public void doAdd(User user) {
		//声明链接
		Connection connection = null;
		PreparedStatement pstmt = null;
		//sql语句
		String sql = "INSERT INTO USER VALUES(NULL,?,?,?,?,?,?,333,NOW(),NOW())";
		try {
			//获取连接
			connection = DBUtil.getConnection();
			pstmt = DBUtil.getPstmt(connection, sql);
			//设置绑定变量
			pstmt.setString(1, user.getUname());
			pstmt.setString(2, user.getPwd());
			pstmt.setString(3, user.getNickname());
			pstmt.setString(4, user.getGender());
			pstmt.setString(5, user.getEmail());
			pstmt.setInt(6, user.getAge());
			//执行sql
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//关闭连接
			DBUtil.closeAll(connection, pstmt, null);
		}
		
	}
	public List doQueryALL() {
		//将所有的信息封装到List容器中
		List userList = new ArrayList();
		//声明连接
		Connection connection = null;
		PreparedStatement pstmt = null;
		ResultSet resultSet = null;
		//SQL语句
		String sql = "SELECT * FROM USER";
		
		try {
			connection = DBUtil.getConnection();
			pstmt = DBUtil.getPstmt(connection, sql);
			//执行SQL语句
			resultSet = pstmt.executeQuery();
			//遍历结果集,利用next()方法,让游标往下
			while(resultSet.next()){
				User user = new User();
				//将取得的数据resultSet放入user对象中
				resultset2bean(resultSet, user);
				//再将单个user对象放入泛型为User的userList容器中去
				userList.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(connection, pstmt, resultSet);
		}
		return userList;
	}
	/**
	 * 根据id删除用户信息
	 * 和添加用户相似
	 * @param id
	 */
	public void doDelete(String id) {
		//声明链接
		Connection connection = null;
		PreparedStatement pstmt = null;
		//sql语句
		String sql = "DELETE FROM USER WHERE ID = ?";
		try {
			//获取连接
			connection = DBUtil.getConnection();
			pstmt = DBUtil.getPstmt(connection, sql);
			//设置绑定变量,页面传递进来的id类型为String
			pstmt.setInt(1, Integer.parseInt(id));
			//执行sql
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//关闭连接
			DBUtil.closeAll(connection, pstmt, null);
		}
		
	}

}

九、逻辑业务处理层
package com.bjsxt.user.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bjsxt.user.dao.UserDao;
import com.bjsxt.user.vo.User;

public class UserController extends HttpServlet{
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置编码格式
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		//获取操作类型
		String oper = request.getParameter("oper");
		//获取请求方式,得到的方式小写
		String method = request.getMethod().toLowerCase();
		//选择相应的方法
		if(oper != null && oper.equals("login") && "post".equals(method) ){
			//验证登陆
			doUserLogin(request,response);
		}else if(oper != null && oper.equals("logout") && "get".equals(method)){
			//注销用户
			doUserLogout(request,response);
		}else if(oper != null && oper.equals("add") && "post".equals(method)){
			//添加用户
			doUserAdd(request,response);
		}else if (oper != null && oper.equals("showAll") && "get".equals(method)) {
			doUserShowAll(request, response);
		}else if (oper != null && oper.equals("delete") && "get".equals(method)) {
			doUserDelete(request, response);
		}else{
			System.out.println("找不到指定的方法:["+method+"]["+oper+"]");
		}
	}
	
	/**
	 * 根据id删除指定的用户信息
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void doUserDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取页面传递过来的:被删除用户的id 这里的di为sting类型
		String id = request.getParameter("id");
		//根据id删除用户信息
		new UserDao().doDelete(id);
		//刷新页面
		resetList(request, response);
	}
	//显示所有用户信息
	private void doUserShowAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		//查询所有用户信息
//		List userList = new UserDao().doQueryALL();
//		//将数据荣servlet传至jsp页面.先设置传递的参数
//		request.setAttribute("userList", userList);
//		//跳转到jsp页面去显示用户信息
//		//利用的是请求转发。request
//		//使用请求转发request中存放的变量不会失效
//		request.getRequestDispatcher("user/index.jsp").forward(request, response);
//		return;
		//直接调用:刷新List并跳转到显示页面(用户模块的首页)
		resetList(request, response);
	}

	/**
	 * 处理新增用户业务
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void doUserAdd(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		//获取注册信息
		String uname = request.getParameter("uname");
		String pwd = request.getParameter("pwd");
		String nickname = request.getParameter("nickname");
		String email = request.getParameter("email");
		String gender = request.getParameter("gender");
		String age = request.getParameter("age");
		//封装对象
		User user = new User(uname, pwd, nickname, gender, email, Integer.parseInt(age));
		//存入数据库
		new UserDao().doAdd(user);
		//跳转到指定页面
		resetList(request, response);
	}


	/**
	 * 处理用户注销业务
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws IOException 
	 */
	private void doUserLogout(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//注销用户当前登陆信息
		//让session立刻失效
		request.getSession().invalidate();
		//重定向至登陆页面
		response.sendRedirect("login.jsp?type=zx");
	}
	
	
	/**
	 * 处理登陆业务
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws IOException 
	 */
	private void doUserLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//获取用户名密码  parameter用来处理页面传递的值
		String uname = request.getParameter("uname");
		String pwd = request.getParameter("pwd");
		//验证是否匹配
		User user = new UserDao().doLogin(uname,pwd);//doLogin返回的是user对象
		//根据结果返回指定页面
		if(user != null){
			//将当前登陆用户信息存放至session
			request.getSession().setAttribute("user", user);
			//重定向至网站后台首页
			response.sendRedirect("index.jsp");
		}else{
			//跳转至登陆页面
			response.sendRedirect("login.jsp?="+Math.random());
		}
	}
	
	//刷新List并跳转到显示页面(用户模块的首页)
	private void resetList(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		//查询所有用户信息
		List userList = new UserDao().doQueryALL();
		//将数据从servlet传至jsp
		request.setAttribute("userList", userList);
		//跳转至jsp页面去显示用户信息
		request.getRequestDispatcher("user/index.jsp").forward(request, response);
	}
	
	
	
}

十、JDBC。用于连接mysql数据库

package com.bjsxt.util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * 数据库JDBC公共类
 * @author Administrator
 *
 */
public class DBUtil {
	//获取数据库配置信息
	private static Properties properties = new Properties();
	//定义JDBC需要的参数
	private static String datatype = null;
	private static String driver = null;
	private static String url = null;
	private static String user = null;
	private static String password = null;

	static {
		//可以保证只加载一次,而且调用的时候肯定已经加载完成
		try {
			//加载配置文件
			properties.load(DBUtil.class.getClassLoader().getResourceAsStream("DataBaseUtil.properties"));
			//获取配置文件里的配置信息
			datatype = properties.getProperty("datatype");
			driver = properties.getProperty(datatype + "Driver");
			url = properties.getProperty(datatype + "Url");
			user = properties.getProperty(datatype + "User");
			password = properties.getProperty(datatype + "Password");
			//加载驱动
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取连接
	 * @return
	 */
	public static Connection getConnection() {
		Connection connection = null;
		try {
			connection = DriverManager.getConnection(url, user, password);
		} catch (SQLException e) {
			System.out.println("DataBaseUtil.getConnection()" + url + ":" + user + ":" + password);
			e.printStackTrace();
		}
		return connection;
	}

	/**
	 * 关闭连接
	 * @param connection
	 */
	public static void closeConnection(Connection connection) {
		if (connection != null) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 获取清单对象
	 * @param connection
	 * @return
	 */
	public static Statement getStatement(Connection connection) {
		Statement statement = null;
		try {
			//判断连接是否为空 如果为空创建一个新的
			if (connection == null) {
				connection = getConnection();
			}
			statement = connection.createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return statement;
	}

	/**
	 * 关闭清单对象
	 * @param statement
	 */
	public static void closeStatement(Statement statement) {
		if (statement != null) {
			try {
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 获取预处理清单对象
	 * @param connection
	 * @param sql
	 * @return
	 */
	public static PreparedStatement getPstmt(Connection connection, String sql) {
		PreparedStatement preparedStatement = null;
		try {
			//判断连接是否为空 如果为空创建一个新的
			if (connection == null) {
				connection = getConnection();
			}
			preparedStatement = connection.prepareStatement(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return preparedStatement;
	}

	/**
	 * 关闭结果集合
	 * @param resultSet
	 */
	public static void closeResultSet(ResultSet resultSet) {
		if (resultSet != null) {
			try {
				resultSet.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 释放所有的资源
	 * @param connection
	 * @param statement
	 * @param resultSet
	 */
	public static void closeAll(Connection connection, Statement statement, ResultSet resultSet) {
		closeResultSet(resultSet);
		closeStatement(statement);
		closeConnection(connection);
	}

}

十一、数据库配置文件DataBaseUtil.properties

##choose which database
datatype=mysql
##oracle
oracleDriver=oracle.jdbc.driver.OracleDriver
oracleUrl=jdbc:oracle:thin:@192.168.1.11:1521:orcl
oracleUser=scott
oraclePassword=tiger
##mysql
mysqlDriver=com.mysql.jdbc.Driver
mysqlUrl=jdbc:mysql://127.0.0.1:3306/bjsxt20150309
mysqlUser=root
mysqlPassword=123456

十二、jstl自定义标签。针对不同的用户,显示对应的模块

package com.bjsxt.util.tags;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.bjsxt.user.vo.User;

public class ShowMenuTag extends TagSupport{
	private int mp;
	
	@Override
	public int doStartTag() throws JspException {
		//获取用户的权限
		User user = (User) this.pageContext.getSession().getAttribute("user");
		if(user !=null){
			//判断是否有权限查看
			if(user.getPower()>=mp){
				//可以查看
				return this.EVAL_BODY_INCLUDE;//显示标签之间的文字
			}
		}
		return this.SKIP_BODY;//skip跳过。不显示标签之间的文字
	}

	public int getMp() {
		return mp;
	}

	public void setMp(int mp) {
		this.mp = mp;
	}
	
}

十三、自定义标签tld文件



    
  BJSXT 1.1 core library
  SXT core
  1.1
  sxt
  http://www.bjsxt.com/sxttag

  
    
       	根据权限显示对应菜单
    
    showMenu
    com.bjsxt.util.tags.ShowMenuTag
    JSP
    
        
			查看本菜单最少需要需要的权限值
        
        mp
        true
        true
        int
    
  

补充:这个页面是先用管理员账号登陆,再到页面里面去注册用户发布给需要登陆的人。不足之处请补充


你可能感兴趣的:(javaEE)