用Jsp+JavaBean+Mysql实现的登录和注册
(2010-11-18 20:54:43)
标签:
软件 |
共同学习和进步:一个简单的用户登录和注册系统。
代码比较简单,希望对刚开始学习的同学有所帮助。
具体代码如下:
*******用户注册页面
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<html>
<head>
<title>用户注册页面</title>
</head>
<body>
<center>
<h1>用户注册页面</h1><hr/>
<form action="FristJsp_for.jsp" method="get">
用户名:<input type="text" name="userId"/><br/>
密码:<input type="password" name="password"> <br/>
<input type="submit" value="确定"/>
<input type="reset" value="重置">
</form>
</center>
</body>
</html>
******注册成功页面
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<center>
<h1>恭喜你,注册成功!!!!三秒后跳转登录页面!!!!</h1>
<%
response.setHeader("Refresh","3;url=login.jsp");
%>
</center>
*******失败页面
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<center>
<h2>该用户已存在!!!!请从新<a href="login_for.jsp">注册</a></h2>
</center>
*******登录页面
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<html>
<head>
<title>用户登录页面</title>
</head>
<body>
<center>
<h1>用户登录页面</h1><hr/>
<form action="FristJsp_1.jsp" method="get">
用户名:<input type="text" name="userId"/><br/>
密码:<input type="password" name="password"> <br/>
<input type="submit" value="登录"/>
<input type="reset" value="重置">
<br/>
<br/>
<h2>如果你是第一次登录请先<a href="login_for.jsp"><h1>注册</h1></a></h2>
</form>
</center>
</body>
</html>
*******登录失败页面
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<center>
<h1>错误的用户名和密码!!!!</h1>
<br/><h3>请<a href="login_for.jsp">注册</a>后在登陆!!!</h3>
</center>
*******登录成功页面
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<center>
<h1>登录成功!!!!</h1>
</center>
*******控制登录页面的转发
<%@ page language="java" import="java.util.*,java.sql.*,com.mysql.jdbc.Driver" pageEncoding="utf-8"%>
<jsp:useBean id="test" scope="page" class="com.jsp.ch08.FristJavaBean">
<jsp:setProperty name="test" property="*"/>
</jsp:useBean>
<%
String userId = test.getUserId();
String password = test.getPassword();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String connectionUrl = "jdbc:mysql://localhost/class?user=root&password=admin";
conn = DriverManager.getConnection(connectionUrl);
String sql = "select * from login where userId=? and password=? ";
pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
pstmt.setString(1,userId);
pstmt.setString(2,password);
rs = pstmt.executeQuery();
if(rs.next()){
response.sendRedirect("login_success.jsp");
}else{
//rs.moveToInsertRow();
//rs.updateString("userId",test.getUserId());
//rs.updateString("password",test.getPassword());
//rs.updateRow();
//rs.insertRow();
response.sendRedirect("login_fai.jsp");
}
}catch(SQLException e){
e.printStackTrace();
}finally{
if(rs != null){
try{
rs.close();
}catch(SQLException sqlEx){}
rs = null;
}
if(pstmt != null){
try{
pstmt.close();
}catch(SQLException sqlEx){}
pstmt = null;
}
if(conn != null){
try{
conn.close();
}catch(SQLException sqlEx){}
conn = null;
}
}
%>
********控制注册页面的转发
代码同登录转发页面 让后将注释的代码取消就可以,让后转发页面改成注册成功和失败页面,不多做解释。
********JavaBean
package com.jsp.ch08;
public class FristJavaBean {
private int id;
private String userId;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
******数据库
只有userId 和 password 相信能做出来