Login and registration with Jsp+javabean+mysql(2010-11-18 20:54:43)
reproduced
Mutual learning and progress: a simple user login and registration system.
The code is relatively simple, I hope to learn from the beginning of the students have some help.
The specific code is as follows:
User Registration page
<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%>
< Head>
<title> User Registration page </title>
<body>
<center>
<form action= "fristjsp_for.jsp" method= "get";
user name: <input type= " Text "Name=" UserId "/><BR/>
Password: <input type=" password "name=" password "> <br/
<input type= "Submit" value= "OK"/>
<input type= "reset" value= "reset",
</form>
</center>
</body>
****** Registration Success page
<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%>
<center>
<%
Response.setheader ("Refresh", "3;url=login.jsp");
%>
</center>
Failed page
<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%>
<center>
</center>
Login Page
<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%>
<title> User Login Page </title>
<body>
<center>
<form action= "fristjsp_1.jsp" method= "Get" >
User name: <input type= "text" name= "UserId"/><br/>
Password: <input type= "password" name= "password" > <br/>
<input type= "Submit" value= "Login"/>
<input type= "reset" value= "reset" >
<br/>
<br/>
</form>
</center>
</body>
Login Failed page
<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%>
<center>
<br/></center>
Login Success Page
<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%>
<center>
</center>
Control the forwarding of login pages
<%@ 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;
}
}
%>
Control the forwarding of registration pages
The code with the Login forward page let the code after the comment is canceled, so that the post-forwarding page to the registration success and failure page, not much to explain.
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;
}
}
Database
Only UserID and password believe they can make it.
Copy of the login and registration implemented with JSP+JAVABEAN+MYSQL