简单说明:整个整合过程使用mysql数据库、Myeclipse 8.5,框架使用struts2.1.6,hibernate3.x,spring2.5
功能:实现简单的用户登录
0. 简单的流程
1. Resister.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>注册首页</title> </head>
<body> <form action="registerDeal.jsp" method="post"> 用户名:<input type="text" name="username"/><br> 密码:<input type="password" name="password"/><br> 确认密码:<input type="password" name="password2"/><br> <input type="submit" value="提交" /> </form> </body> </html>
|
2. RegisterDeal.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="java.sql.Connection" %> <%@ page import="java.sql.DriverManager" %> <%@ page import="java.sql.PreparedStatement" %> <%@ page import="java.sql.SQLException" %> <%@ page import="java.sql.ResultSet" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>登录处理</title> </head>
<body> <% String username = request.getParameter("username").trim(); String password = request.getParameter("password").trim(); String password2 = request.getParameter("password2").trim();
// 简单验证,不填其他的复杂逻辑 // save in database Connection conn = null; PreparedStatement pstat = null; String url = "jdbc:mysql://localhost:3306/ssh"; String user = "root"; String pw = "mysql123"; try { Class.forName("com.mysql.jdbc.Driver"); try { conn = DriverManager.getConnection(url, user, pw); String selectSql = "select count(*) from tb_user where username=?"; pstat = conn.prepareStatement(selectSql); pstat.setString(1, username); ResultSet rs = pstat.executeQuery(); rs.next(); int count = rs.getInt(1); if(count > 0) { //response.sendRedirect("registerFail.jsp"); request.getRequestDispatcher("registerFail.jsp").forward(request, response); return; }
String sql = "insert into tb_user(id, username, password) values(null, ?, ?)"; pstat = conn.prepareStatement(sql); pstat.setString(1, username); pstat.setString(2, password); pstat.executeUpdate();
//response.sendRedirect("registerSucess.jsp"); request.setAttribute("username", username); request.getRequestDispatcher("registerSucess.jsp").forward(request, response); } catch (SQLException e) { e.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (null != pstat) pstat.close();
if (null != conn) conn.close(); } %> </body> </html>
|
在这个jsp里,实现了从请求里读取数据,连接数据库,判断数据库中是否已经存在要注册的用户名,如果不存在则允许当前用户注册,否则不允许注册。如果 注册成功就跳到显示注册成功信息的页面registerSucess.jsp,否则跳到注册失败的页面registerFail.jsp。为了简单没有添加其他复杂的逻辑和功能。整个过程流程是很简单的。
3. registerSucess.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>注册成功</title> </head>
<body> registerSucess... <br/> welcome <font color="red" size="10px"><%=request.getAttribute("username") %></font> </body> </html>
|
4. registerFail.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>注册失败</title> </head>
<body> registerFail... </body> </html>
|
5. 数据库sql
Create database ssh01; Use ssh01; Drop table tb_user; Create tabel tb_user( Id int auto_increment, Username varchar(30), Password varchar(30), Primary key(id) ); |
在工程的WEB-INFO下的lib目录下添加mysql的驱动jar包。部署项目并运行,测试项目是否成功。当数据库不存在相同的用户名时注册成功,否则注册失败!!
|
|
|
6. 简单分析:
|
此工程没有任何的模式或者是框架,所有的业务逻辑、操作数据库操作等都在jsp里实现,基本上没什么难点。但是维护修改比较麻烦!基本上不能重用。
版权声明:本文为博主原创文章,未经博主允许不得转载。