The software used in this experiment is the myeclipse10,tomcat7,dreamweaver,sqlserver2008 database. You can implement a user login with a user name and password. If the login succeeds, the page displays the login success, and if the password is incorrect, the page displays a login failure. Connection database use of things JavaBean method, you need to implement download good sqlserver2008 driver, in the Web project folder under the SRC folder under the new package "Bean", and under this package to create a new "Dbbean.java" file.
The Dbbean.java file code is as follows:
PackageBean;ImportJava.sql.*; Public classDbbean {PrivateString driverstr = "Com.microsoft.sqlserver.jdbc.SQLServerDriver"; PrivateString connstr = "jdbc:sqlserver://localhost:1433; Databasename=jxp "; PrivateString dbusername = "sa"; PrivateString Dbpassword = "123456"; PrivateConnection conn =NULL; PrivateStatement stmt =NULL; PublicDbbean () {Try{class.forname (DRIVERSTR); Conn=drivermanager.getconnection (ConnStr, Dbusername, Dbpassword); stmt=conn.createstatement (); } Catch(Exception ex) {System.out.println ("Data connection Failed!" "); } } Public intexecuteupdate (String s) {intresult = 0; System.out.println ("--UPDATE statement:" +s+ "\ n"); Try{result=Stmt.executeupdate (s); } Catch(Exception ex) {System.out.println ("Perform update Error! "); } returnresult; } PublicResultSet executeQuery (String s) {ResultSet rs=NULL; System.out.print ("--Query statement:" +s+ "\ n"); Try{RS=Stmt.executequery (s); } Catch(Exception ex) {System.out.println ("? Execute a query Error! "); } returnrs; } Public voidExecQuery (String s) {Try{stmt.executeupdate (s); } Catch(SQLException e) {//TODO auto-generated Catch blockSystem.out.println ("Execute insert Error! "); } } Public voidClose () {Try{stmt.close (); Conn.close (); } Catch(Exception e) {}}}
There are three JSP paging files in the Webroot directory: login.jsp,logincheck.jsp,loginsuccess.jsp. In the login.jsp page, you can enter the user name, password, click the login button, To achieve the login Success loginsucccess.jsp page jump, if the password is wrong, then the page will jump to the login failed page. (Of course, before the page jump, you need to create a new database in sqlserver2008, create a new table in the database directory, and fill in the table information)
Folder structure:
Login.jsp Code:
<%@ pageImport= "java.sql.*" language= "java" contenttype= "text/html; Charset=utf-8 " pageencoding = "Utf-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Indexcheck.jsp Code:
<%@ pageImport= "java.sql.*" language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding= "Utf-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >class= "Bean.dbbean" scope= "page"/><%request.setcharacterencoding ("UTF-8"); String username= (String) request.getparameter ("username"); String Password= (String) request.getparameter ("password");//Remove the value of the login.jsp//here is the database operationString sql= "SELECT * from login where username=" + "'" "+username+" ";//define a query statementResultSet rs=db.executequery (SQL);//run the above statement if(Rs.next ()) {/*if (Password.equals (rs.getstring (2))) {}*/ if(Password.equals (Rs.getobject ("Password")) {response.sendredirect ("Loginsuccess.jsp"); } Else{out.print ("<script language= ' JavaScript ' > alert (' Password error ');</script>"); Response.setheader ("Refresh", "0;url=login.jsp"); } } Else{out.print ("<script language= ' JavaScript ' > alert (' Account error--else ');</script>"); Response.setheader ("Refresh", "0;url=login.jsp"); } %></body>
Indexsuccess.jsp Code:
Import= "java.sql.*" language= "java" contenttype= "text/html; Charset=utf-8 " pageencoding=" Utf-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
The final page effect is as follows:
If the password is incorrect, the following page is displayed:
JAVA Web Simple Login Interface JSP implementation