Javaweb Simple Login Example

Source: Internet
Author: User

Jsp+servlet+javabean Simple Program Example-user name password login, from the "Tomcat&javaweb Technical Manual", pro-Test available.

IDE Environment: MYECLIPSE10

1, build Web Project, named Login_test. To create a Userlogn table:

CREATE TABLE Userlogin (

User_name varchar (TEN) is not NULL,

User_pwd varchar (TEN) is not NULL,

Constraint USER_PK primary KEY (USER_NAME)

);

2. Add JavaBean file to create new class Userloginbean,java

Package Login;import java.sql.*;p ublic class Userloginbean {private Connection con;     Database connection private ResultSet RS;      Result set private Statement stmt;    private static final String driver_name= "Com.mysql.jdbc.Driver";    private static final String url_str= "Jdbc:mysql://localhost/test";            Constructor public Userloginbean () {try{//load driver class.forname (driver_name);    Get database connection con=drivermanager.getconnection (URL_STR, "root", "Jiangshan");        own user name and password} catch (SQLException se) {se.printstacktrace ();        } catch (ClassNotFoundException Cnfe) {cnfe.printstacktrace ();        }}//Business method public boolean login (String username,string password) {Boolean b = false;            try{stmt=con.createstatement ();            String sql= "Select User_pwd from Userlogin Where user_name= '" +username+ "'";            Rs=stmt.executequery (SQL);      while (Rs.next ()) {          String pwd= rs.getstring (1);                if (pwd.equals (password)) {b=true;            }}}catch (SQLException se) {se.printstacktrace ();        B=false;   }finally{This.close (CON,RS,STMT);    The following defines the Close method} return B; The//close method is used to close the corresponding resource public void close (Connection con,resultset rs,statement stmt) {try{if (con!=nu            ll) {con.close ();            } if (Rs!=null) {rs.close ();            } if (Stmt!=null) {stmt.close ();        }} catch (SQLException se) {se.printstacktrace (); }    }}

 3, add servlet class, name Userloginservlet.java

Import java.io.*;import javax.servlet.*;import javax.servlet.http.*;import login. Userloginbean;public class Userloginservlet extends httpservlet{public void doget (HttpServletRequest request,httpserv    Letresponse response) throws servletexception,ioexception{This.dopost (request, response); } public void DoPost (HttpServletRequest request,httpservletresponse response) throws Servletexception,ioexc        eption{PrintWriter out=response.getwriter ();            try{String username=request.getparameter ("UserName"). Trim ();            String password=request.getparameter ("PassWord"). Trim ();            Userloginbean ubl=new Userloginbean ();            Boolean b =ubl.login (UserName, PassWord);            if (b) {This.forward (Request,response, "/loginok.jsp");                            } else{This.forward (Request,response, "/loginfail.jsp"); }} catch (Exception e) {Out.println ("Login fail.<br>");        Out.println (E.tostring ());  }}//private void Forward (httpservletrequest request,httpservletresponse response,string URL) throws        servletexception,ioexception{requestdispatcher rd=request.getrequestdispatcher (URL);    Rd.forward (request, response); }}

Import java.io.*;import javax.servlet.*;import javax.servlet.http.*;import login. Userloginbean;public class Userloginservlet extends httpservlet{public void doget (HttpServletRequest request,httpserv    Letresponse response) throws servletexception,ioexception{This.dopost (request, response); } public void DoPost (HttpServletRequest request,httpservletresponse response) throws Servletexception,ioexc        eption{PrintWriter out=response.getwriter ();            try{String username=request.getparameter ("UserName"). Trim ();            String password=request.getparameter ("PassWord"). Trim ();            Userloginbean ubl=new Userloginbean ();            Boolean b =ubl.login (UserName, PassWord);            if (b) {This.forward (Request,response, "/loginok.jsp");                            } else{This.forward (Request,response, "/loginfail.jsp"); }} catch (Exception e) {Out.println ("Login fail.<br>");        Out.println (E.tostring ());  }}//private void Forward (httpservletrequest request,httpservletresponse response,string URL) throws        servletexception,ioexception{requestdispatcher rd=request.getrequestdispatcher (URL);    Rd.forward (request, response); }}

4. Write Login interface: Create index.jsp

<%@ page contenttype= "text/html; CHARSET=GBK "%>
<%@ page contenttype= "text/html; CHARSET=GBK "%>

5. Login Successful Interface: loginok.jsp

<%@ page contenttype= "text/html; CHARSET=GBK "%>

<%@ page contenttype= "text/html; CHARSET=GBK "%>

6, Login failure interface: loginfail.jsp

%@ page contenttype= "text/html; CHARSET=GBK "%>

<%@ page contenttype= "text/html; CHARSET=GBK "%>

7. Start Tomcat, deploy this project to Tomcat's WebApps file, open the Web-inf folder in the project under WebApps, modify the Web. xml file, <web-app> and </web-app> Add the following code between them:

<servlet>
<servlet-name>UserLoginServlet</servlet-name>
<servlet-class>UserLoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserLoginServlet</servlet-name>
<url-pattern>/UserLoginServlet</url-pattern>
</servlet-mapping>


You can also add the following code to set the home page to index.jsp:
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

8, open the browser, enter the http://localhost:8080/Login_test/can enter the login interface.

Javaweb Simple Login Example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.