一步一步用Jsp+Javabean+Servlet實現登錄功能


jsp+javabean+servlet實現簡單的登錄功能。

首先打開MySQL數據庫

 

 

新建如下的用於登錄數據庫表user

 

 

 

打開Eclipse,新建web工程:

 

 

 

src文件夾中新建四個包。bean包、db包、servlet包和dao包。下面是bean包的創建過程。其他三個包同樣。

 

 

 

 

 

建完四個包后分別在每個包下新建類。

 

 

 

 

 

建完之后打開Userbean.java 添加如下代碼:

 

package bean;

 

public class Userbean {

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

private String username;

private String password;

 

}

 

保存。打開DBUtil.java 添加如下代碼:

 

package db;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

 

public class DBUtil

{

public static String db_url="jdbc:mysql://localhost:3306/test?uerUnicode=true&characterEncoding=UTF-8";

public static String db_user="root";//數據庫的用戶名

public static String db_password="144214";//數據庫的密碼

public static Connection getConn()

{

Connection conn=null;

try

{

Class.forName("com.mysql.jdbc.Driver");

conn=DriverManager.getConnection(db_url,db_user,db_password);

}

catch(Exception e)

{

e.printStackTrace();

}

return conn;

}

public static void close(Statement state,Connection conn)//關閉函數

{

if(state!=null)//只有狀態和連接時,先關閉狀態

{

try

{

state.close();

}

catch(SQLException e)

{

e.printStackTrace();

}

}

if(conn!=null)

{

try

{

conn.close();

}

catch(SQLException e)

{

 e.printStackTrace();

}

}

}

public static void close(ResultSet rs,Statement state,Connection conn)

{

if(rs!=null)//有結果集,狀態和連接時,先關閉結果集,在關閉狀態,在關閉連接

{

try

{

rs.close();

}

catch(SQLException e)

{

e.printStackTrace();

}

}

if(state!=null)

 

{

try

{

state.close();

}

catch(SQLException e)

{

e.printStackTrace();

}

}

if(conn!=null)

{

try

{

conn.close();

}

catch(SQLException e)

{

e.printStackTrace();

}

}

}

 

}

 

保存。打開Userdao.java 添加如下代碼:

 

public int login(String username,String password)

{

Connection conn = DBUtil.getConn();

Statement state =null;

ResultSet rs = null;

int flag=0;

try

{

String sql = "select * from user where username = '"+username+"'";

state = conn.createStatement();

 

rs=state.executeQuery(sql);

if(rs.next())

{

if(rs.getString("password").equals(password))

{

flag=1;

}

}

}

catch(Exception e)

{

e.printStackTrace();

}

 

finally

{

DBUtil.close(rs, state, conn);

}

return flag;

}

 

保存。在Userservlet.java添加如下代碼:

 

package servlet; 

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import dao.Userdao;

 

public class Userservlet extends HttpServlet{

//service方法是必須的!在Servlet先執行這個方法

protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException

{

req.setCharacterEncoding("utf-8");//設置字符集

String method = req.getParameter("method");//獲取jsp界面的method參數來判斷執行什么方法

if(method.equals("login"))

{

login(req,resp);//登錄方法

}

}

protected void login(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException

{

req.setCharacterEncoding("utf-8");

String username = req.getParameter("username1");

String password = req.getParameter("password1");//獲取用戶名密碼

Userdao userdao = new Userdao();

int flag = userdao.login(username, password);//執行Userdao里面的login方法判斷登錄的用戶名密碼是否正確

if(flag==1)

{

System.out.println("登錄成功!");

resp.sendRedirect(req.getContextPath()+"/index.jsp");//登錄成功跳轉到主界面

}

else

{

System.out.println("登錄失敗!");

resp.sendRedirect(req.getContextPath()+"/login.jsp");

}

}

}

然后在WEB-INF文件夾下新建web.xml文件:

 

 

 

 

打開剛剛創建的web.xml文件,添加如下代碼:

 

<web-app>

<servlet>

<servlet-name>Userservlet</servlet-name>

<servlet-class>servlet.Userservlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Userservlet</servlet-name>

<url-pattern>/servlet/Userservlet</url-pattern>

</servlet-mapping>

 

</web-app>

 

 

 

 然后新建一個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">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>Insert title here</title>

</head>

<body>

<form action="${pageContext.request.contextPath}/servlet/Userservlet?method=login" method="post">

<h1>登錄 界面</h1>

<div>

<input type="text" placeholder="登錄名" required="" id="username" name="username1"/>

</div>

<div>

<input type="password" placeholder="密碼" required="" id="password" name="password1" />

</div>

<div>

<input type="submit" value="登 錄" />

</div>

</form>

</body>

</html>

 

保存。再新建一個index.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">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>Insert title here</title>

</head>

<body>

這是主頁,你已成功登錄!

</body>

</html>

 

 

 

 

保存運行!

 

 

 

恭喜你完成了!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM