Approximate process:
to import sqljdbc4 packages in a Java Web project
to complete java code in Java Resources
Create a folder to write your own jsp under WebContent
Paste the sqljdbc4 and jstl-1.2 packages under the lib file under web-inf file .
Java Web Login Interface source code :
1.data_uil.java
ImportJava.sql.*; Public classData_uil { PublicConnection getconnection () {Try{class.forname ("Com.microsoft.sqlserver.jdbc.SQLServerDriver"); }Catch(ClassNotFoundException e) {e.printstacktrace (); } String User="***"; String Password="***"; String URL= "jdbc:sqlserver://127.0.0.1:1433;databasename=***"; Connection Con=NULL; Try{con=drivermanager.getconnection (Url,user,password); }Catch(SQLException e) {e.printstacktrace (); } returncon; } Publicstring Selectpassword (string username) {Connection Connection=getconnection (); String SQL= "Select *from login where Username=?"; PreparedStatement PreparedStatement=NULL; ResultSet result=NULL; String Password=NULL; Try{PreparedStatement=connection.preparestatement (SQL); Preparedstatement.setstring (1, username); Result=preparedstatement.executequery ();//an executable query if(Result.next ()) Password=result.getstring ("Password"); }Catch(SQLException e) {e.printstacktrace (); }finally{Close (PreparedStatement); Close (result); Close (connection); } System.out.println ("The database password found is:" +password); returnpassword; } Public voidClose (Connection con) {Try{ if(con!=NULL) {con.close (); } }Catch(SQLException e) {e.printstacktrace (); } } Public voidClose (PreparedStatement preparedstatement) {Try{ if(preparedstatement!=NULL) {preparedstatement.close (); } }Catch(SQLException e) {e.printstacktrace (); } } Public voidClose (ResultSet ResultSet) {Try{ if(resultset!=NULL) {resultset.close (); } }Catch(SQLException e) {e.printstacktrace (); } }}
2.LOGIN_CHECK.JSP:
<%@ page 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= "util." Data_uil "scope=" page "/><%String username= (String) request.getparameter ("username"); String Password= (String) request.getparameter ("Password"); if(username==NULL||"". Equals (username)) {Out.print ("<script language= ' JavaScript ' > alert (' User name cannot be null ');</script>"); Response.setheader ("Refresh", "0;url=user_login.jsp"); } Else{System.out.println ("User name entered:" +username); String Passwordindatabase=Util.selectpassword (username); System.out.println ("Password:" +passwordindatabase); if(passwordindatabase==NULL||"". Equals (Passwordindatabase)) {Out.print ("<script language= ' JavaScript ' > alert (' User name does not exist ');</script>"); Response.setheader ("Refresh", "0;url=user_login.jsp"); } Else if(passwordindatabase.equals (password)) {Out.print ("<script language= ' JavaScript ' > alert (' Login successful ');</script>"); Response.setheader ("Refresh", "0;url=loginsucces.jsp"); } Else{out.print ("<script language= ' JavaScript ' > alert (' Password error ');</script>"); Response.setheader ("Refresh", "0;url=user_login.jsp"); } }%></body>3.loginsucces.jsp
<%@ page 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" >
4.user_login.jsp
<%@ page 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" >Operation Result:
Java Web login Interface source code