JavaWeb_(request和response)用戶登錄注冊模板_基礎版


 

 

  用戶登錄注冊模板進階版  傳送門

 

用戶登錄注冊模板基礎版

  登錄:當用戶登錄成功時跳轉到personCenter.jsp當用戶登錄失敗時跳轉到login.jsp並給出提示

  注冊:當用戶注冊成功時跳轉到login.jsp當用戶注冊失敗時跳轉到register.jsp並給出提示

  personCenter.jsp展示用戶信息

 

  用戶登錄注冊模板基礎版  目錄結構【用戶登錄_02版】

  

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

<a href="login.jsp">登錄</a>
<a href="register.jsp">注冊</a>

</body>
</html>
index.jsp

 

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

<%
Object msg = request.getAttribute("message");
if(msg!=null)
    out.println(msg);
%>

登錄<hr>
<form action="login_do.jsp" methon="post">
用戶名:<input type="text" name="username" /><br/>
密碼    :<input type="password" name="password" /><br/>

<input type="submit" value="登錄"/>
</form>
</body>
</html>
login.jsp

 

<%@page import="com.Gary.util.DBUtil" %>
<%@page import="com.Gary.model.User" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");

User user = DBUtil.verifyAccount(username,password);
if(user==null){
//    out.println("登錄失敗,用戶名或密碼錯誤");
    request.setAttribute("message","登錄失敗,用戶名或密碼錯誤");
    request.getRequestDispatcher("login.jsp").forward(request,response);
}else{
//    out.println("登錄成功");
    //登錄請求的轉發,將數據傳遞給personCenter.jsp頁面
    request.setAttribute("user", user);
    request.getRequestDispatcher("personCenter.jsp").forward(request,response);
    
}
%>
</body>
</html>
login_do.jsp

 

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
Object msg = request.getAttribute("message");
if(msg!=null)
    out.println(msg);
%>

<form action="register_do.jsp" methon="post">
用戶名:<input type="text" name="username" /><br/>
密碼:    <input type="password" name="password" /><br/>
年齡:    <input type="text" name="age" /><br/>
性別:男<input type="radio" name="sex" value="男" /><input type="radio" name="sex" value="女"/><br/>

<input type="submit" value="注冊"/>
</form>

</body>
</html>
register.jsp

 

<%@ page import="com.Gary.util.DBUtil" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%

String username = request.getParameter("username");
String password = request.getParameter("password"); 
int age = Integer.parseInt(request.getParameter("age"));
String sex = request.getParameter("sex");

boolean isSuccess = DBUtil.addUser(username,password,age,sex);

//使用請求轉發
if(isSuccess){
    request.setAttribute("message", "注冊成功,請登錄");
    //通過getRequestDispatcher傳遞路徑將請求轉發給login.jsp
    request.getRequestDispatcher("login.jsp").forward(request,response);
}else{
    request.setAttribute("message", "注冊失敗,用戶名重復");
    //通過getRequestDispatcher傳遞路徑將請求轉發給login.jsp
    request.getRequestDispatcher("register.jsp").forward(request,response);
}

// if(isSuccess){
//     out.println(username);
//      out.println("<font color='green'>注冊成功</font>");
// }else{
//     out.println(username);
//     out.println("<font color='red'>注冊失敗</font>");
// }
%>
</body>
</html>
register_do.jsp

 

<%@ page import="com.Gary.model.User" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
User user = (User)request.getAttribute("user");
%>

用戶名:<%=user.getUsername()%><br/>
年齡:<%=user.getAge()%><br/>
性別:<%=user.getSex()%><br/>
</body>
</html>
personCenter.jsp

 

package com.Gary.model;

public class User {
    private String username;
    private String password;
    private int age;
    private String sex;
    public User(String username, String password, int age, String sex) {
        super();
        this.username = username;
        this.password = password;
        this.age = age;
        this.sex = sex;
    }
    
    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;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    
    
    
}
User.java

 

package com.Gary.util;

import java.util.HashMap;
import java.util.Map;

import com.Gary.model.User;


public class DBUtil {
    
    private static Map<String ,User>db = new HashMap<String,User>();
    
    public static boolean addUser(String username,String password,int age,String sex) {
        //TODO
        
        if(db.containsKey(username)) {
            return false;
        }else {
            User user=new User(username,password,age,sex);
            db.put(username, user);
            return true;
        }
    }
    
    //賬號校驗
    public static User verifyAccount(String username,String password) {
        if(db.containsKey(username)) {
            User user = db.get(username);
            if(user.getPassword().equals(password)) {
                return user;
            }else {
                return null;
            }
        }
        return null;
    }
}
DBUtil.java

 

 

用戶注冊_01版

  當用戶點擊主頁面注冊時跳轉到register.jsp頁面,用戶輸入完表單后由register_do.jsp頁面進行處理,戶數據模型驅動User,java和DBUtild.java中對數據進行本地處理

  DBUtild.java對用戶ID進行同名檢測,用戶注冊成功顯示綠色提示信息用戶注冊失敗顯示紅色提示信息

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

<a>登錄</a>
<a href="register.jsp">注冊</a>

</body>
</html>
index.jsp

 

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

<form action="register_do.jsp" methon="post">
用戶名:<input type="text" name="username" /><br/>
密碼:    <input type="password" name="password" /><br/>
年齡:    <input type="text" name="age" /><br/>
性別:男<input type="radio" name="sex" value="男" /><input type="radio" name="sex" value="女"/><br/>

<input type="submit" value="注冊"/>
</form>

</body>
</html>
register.jsp

 

<%@ page import="com.Gary.util.DBUtil" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%

String username = request.getParameter("username");
String password = request.getParameter("password"); 
int age = Integer.parseInt(request.getParameter("age"));
String sex = request.getParameter("sex");

boolean isSuccess = DBUtil.addUser(username,password,age,sex);
if(isSuccess){
    out.println(username);
     out.println("<font color='green'>注冊成功</font>");
}else{
    out.println(username);
    out.println("<font color='red'>注冊失敗</font>");
}
%>
</body>
</html>
register_do.jsp

 

package com.Gary.model;

public class User {
    private String username;
    private String password;
    private int age;
    private String sex;
    public User(String username, String password, int age, String sex) {
        super();
        this.username = username;
        this.password = password;
        this.age = age;
        this.sex = sex;
    }
    
    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;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    
    
    
}
User.java

 

package com.Gary.util;

import java.util.HashMap;
import java.util.Map;

import com.Gary.model.User;


public class DBUtil {
    
    private static Map<String ,User>db = new HashMap<String,User>();
    
    public static boolean addUser(String username,String password,int age,String sex) {
        //TODO
        
        if(db.containsKey(username)) {
            return false;
        }else {
            User user=new User(username,password,age,sex);
            db.put(username, user);
            return true;
        }
    }
}
DBUtil.java

  

  開發注冊界面

  表單

  

  用戶表單信息注冊完畢后提交表單給register_do.jsp

<form action="register_do.jsp" methon="post">
用戶名:<input type="text" name="username" /><br/>
密碼:    <input type="password" name="password" /><br/>
年齡:    <input type="text" name="age" /><br/>
性別:男<input type="radio" name="sex" value="男" /><input type="radio" name="sex" value="女"/><br/>

<input type="submit" value="注冊"/>
</form>

 

  戶數據模型驅動User,java存儲用戶信息,DBUtil.java運用字典集對數據進行本地存儲

    private static Map<String ,User>db = new HashMap<String,User>();

 

  通過DBUtil.java中的addUser()方法對用戶進行本地注冊,返回bool值,當用戶注冊ID相同時返回false,否則返回true

        if(db.containsKey(username)) {
            return false;
        }else {
            User user=new User(username,password,age,sex);
            db.put(username, user);
            return true;
        }

 

  由register_do.java對用戶信息進行校驗

boolean isSuccess = DBUtil.addUser(username,password,age,sex);
if(isSuccess){
    out.println(username);
     out.println("<font color='green'>注冊成功</font>");
}else{
    out.println(username);
    out.println("<font color='red'>注冊失敗</font>");
}

 

 

用戶注冊_02版

  當用戶通過首頁直接進入login.jsp時,login.jsp頁面只顯示登錄字樣,用戶通過register.jsp進入login.jsp頁面時,跳轉到login.jsp並提示用戶注冊成功

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

<a href="login.jsp">登錄</a>
<a href="register.jsp">注冊</a>

</body>
</html>
index.jsp

 

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

<%
Object msg = request.getAttribute("message");
if(msg!=null)
    out.println(msg);
%>

登錄<hr>
<form action="" methon="post">
用戶名:<input type="text" name="username" /><br/>
密碼    :<input type="password" name="password" /><br/>

<input type="submit" value="登錄"/>
</form>
</body>
</html>
login.jsp

 

<%@ page import="com.Gary.util.DBUtil" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%

String username = request.getParameter("username");
String password = request.getParameter("password"); 
int age = Integer.parseInt(request.getParameter("age"));
String sex = request.getParameter("sex");

boolean isSuccess = DBUtil.addUser(username,password,age,sex);

//使用請求轉發
if(isSuccess){
    request.setAttribute("message", "注冊成功,請登錄");
    //通過getRequestDispatcher傳遞路徑將請求轉發給login.jsp
    request.getRequestDispatcher("login.jsp").forward(request,response);
}

// if(isSuccess){
//     out.println(username);
//      out.println("<font color='green'>注冊成功</font>");
// }else{
//     out.println(username);
//     out.println("<font color='red'>注冊失敗</font>");
// }
%>
</body>
</html>
register_do.jsp

 

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

<form action="register_do.jsp" methon="post">
用戶名:<input type="text" name="username" /><br/>
密碼:    <input type="password" name="password" /><br/>
年齡:    <input type="text" name="age" /><br/>
性別:男<input type="radio" name="sex" value="男" /><input type="radio" name="sex" value="女"/><br/>

<input type="submit" value="注冊"/>
</form>

</body>
</html>
register.jsp

 

package com.Gary.model;

public class User {
    private String username;
    private String password;
    private int age;
    private String sex;
    public User(String username, String password, int age, String sex) {
        super();
        this.username = username;
        this.password = password;
        this.age = age;
        this.sex = sex;
    }
    
    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;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    
    
    
}
User.java

 

package com.Gary.util;

import java.util.HashMap;
import java.util.Map;

import com.Gary.model.User;


public class DBUtil {
    
    private static Map<String ,User>db = new HashMap<String,User>();
    
    public static boolean addUser(String username,String password,int age,String sex) {
        //TODO
        
        if(db.containsKey(username)) {
            return false;
        }else {
            User user=new User(username,password,age,sex);
            db.put(username, user);
            return true;
        }
    }
}
DBUtil.Java

 

  當登錄成功時,通過在register_do.jsp頁面中表單傳遞時進行轉發給login,jsp頁面【跳轉到login.jsp頁面】

boolean isSuccess = DBUtil.addUser(username,password,age,sex);

//使用請求轉發
if(isSuccess){
    request.setAttribute("message", "注冊成功,請登錄");
    //通過getRequestDispatcher傳遞路徑將請求轉發給login.jsp
    request.getRequestDispatcher("login.jsp").forward(request,response);
}

  

  login,jsp頁面最開始時進行判斷是否由register.jsp傳遞過來信息

<%
Object msg = request.getAttribute("message");
if(msg!=null)
    out.println(msg);
%>

 

 

用戶登錄_01版

  當用戶登錄后,login.jsp表單中的信息與數據庫中的賬號進行匹配,當用戶的賬號存在時且密碼輸入正確,提示用戶登錄成功用戶名不存在或用戶名與賬號匹配錯誤時提示登錄失敗

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

<a href="login.jsp">登錄</a>
<a href="register.jsp">注冊</a>

</body>
</html>
index.jsp

 

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

<%
Object msg = request.getAttribute("message");
if(msg!=null)
    out.println(msg);
%>

登錄<hr>
<form action="login_do.jsp" methon="post">
用戶名:<input type="text" name="username" /><br/>
密碼    :<input type="password" name="password" /><br/>

<input type="submit" value="登錄"/>
</form>
</body>
</html>
login.jsp

 

<%@page import="com.Gary.util.DBUtil" %>
<%@page import="com.Gary.model.User" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");

User user = DBUtil.verifyAccount(username,password);
if(user==null){
    out.println("登錄失敗,用戶名或密碼錯誤");
}else{
    out.println("登錄成功");
}
%>
</body>
</html>
login_do.html

 

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

<form action="register_do.jsp" methon="post">
用戶名:<input type="text" name="username" /><br/>
密碼:    <input type="password" name="password" /><br/>
年齡:    <input type="text" name="age" /><br/>
性別:男<input type="radio" name="sex" value="男" /><input type="radio" name="sex" value="女"/><br/>

<input type="submit" value="注冊"/>
</form>

</body>
</html>
Vregister.jsp

 

<%@ page import="com.Gary.util.DBUtil" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%

String username = request.getParameter("username");
String password = request.getParameter("password"); 
int age = Integer.parseInt(request.getParameter("age"));
String sex = request.getParameter("sex");

boolean isSuccess = DBUtil.addUser(username,password,age,sex);

//使用請求轉發
if(isSuccess){
    request.setAttribute("message", "注冊成功,請登錄");
    //通過getRequestDispatcher傳遞路徑將請求轉發給login.jsp
    request.getRequestDispatcher("login.jsp").forward(request,response);
}

// if(isSuccess){
//     out.println(username);
//      out.println("<font color='green'>注冊成功</font>");
// }else{
//     out.println(username);
//     out.println("<font color='red'>注冊失敗</font>");
// }
%>
</body>
</html>
register_do.jsp

 

package com.Gary.model;

public class User {
    private String username;
    private String password;
    private int age;
    private String sex;
    public User(String username, String password, int age, String sex) {
        super();
        this.username = username;
        this.password = password;
        this.age = age;
        this.sex = sex;
    }
    
    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;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    
    
    
}
User.java

 

package com.Gary.util;

import java.util.HashMap;
import java.util.Map;

import com.Gary.model.User;


public class DBUtil {
    
    private static Map<String ,User>db = new HashMap<String,User>();
    
    public static boolean addUser(String username,String password,int age,String sex) {
        //TODO
        
        if(db.containsKey(username)) {
            return false;
        }else {
            User user=new User(username,password,age,sex);
            db.put(username, user);
            return true;
        }
    }
    
    //賬號校驗
    public static User verifyAccount(String username,String password) {
        if(db.containsKey(username)) {
            User user = db.get(username);
            if(user.getPassword().equals(password)) {
                return user;
            }else {
                return null;
            }
        }
        return null;
    }
}
DBUtil.java

 

  當用戶提交登錄信息時,由login_do處理用戶登錄的信息,

  通過DBUtil.java中的verifyAccount()進行用戶校驗,當用戶名存在並密碼與用戶相匹配時返回true

    public static User verifyAccount(String username,String password) {
        if(db.containsKey(username)) {
            User user = db.get(username);
            if(user.getPassword().equals(password)) {
                return user;
            }else {
                return null;
            }
        }
        return null;
    }

  

  login_do.jsp中進行賬號校驗

User user = DBUtil.verifyAccount(username,password);
if(user==null){
    out.println("登錄失敗,用戶名或密碼錯誤");
}else{
    out.println("登錄成功");
}

 

 

用戶登錄_02版

  登錄:當用戶登錄成功時,跳轉到personCenter.jsp當用戶登錄失敗時,跳轉到login.jsp並給出提示

  注冊:當用戶注冊成功時,跳轉到login.jsp當用戶注冊失敗時,跳轉到register.jsp並給出提示

  personCenter.jsp展示用戶信息

 

  

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

<a href="login.jsp">登錄</a>
<a href="register.jsp">注冊</a>

</body>
</html>
index.jsp

 

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

<%
Object msg = request.getAttribute("message");
if(msg!=null)
    out.println(msg);
%>

登錄<hr>
<form action="login_do.jsp" methon="post">
用戶名:<input type="text" name="username" /><br/>
密碼    :<input type="password" name="password" /><br/>

<input type="submit" value="登錄"/>
</form>
</body>
</html>
login.jsp

 

<%@page import="com.Gary.util.DBUtil" %>
<%@page import="com.Gary.model.User" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");

User user = DBUtil.verifyAccount(username,password);
if(user==null){
//    out.println("登錄失敗,用戶名或密碼錯誤");
    request.setAttribute("message","登錄失敗,用戶名或密碼錯誤");
    request.getRequestDispatcher("login.jsp").forward(request,response);
}else{
//    out.println("登錄成功");
    //登錄請求的轉發,將數據傳遞給personCenter.jsp頁面
    request.setAttribute("user", user);
    request.getRequestDispatcher("personCenter.jsp").forward(request,response);
    
}
%>
</body>
</html>
login_do.jsp

 

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
Object msg = request.getAttribute("message");
if(msg!=null)
    out.println(msg);
%>

<form action="register_do.jsp" methon="post">
用戶名:<input type="text" name="username" /><br/>
密碼:    <input type="password" name="password" /><br/>
年齡:    <input type="text" name="age" /><br/>
性別:男<input type="radio" name="sex" value="男" /><input type="radio" name="sex" value="女"/><br/>

<input type="submit" value="注冊"/>
</form>

</body>
</html>
register.jsp

 

<%@ page import="com.Gary.util.DBUtil" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%

String username = request.getParameter("username");
String password = request.getParameter("password"); 
int age = Integer.parseInt(request.getParameter("age"));
String sex = request.getParameter("sex");

boolean isSuccess = DBUtil.addUser(username,password,age,sex);

//使用請求轉發
if(isSuccess){
    request.setAttribute("message", "注冊成功,請登錄");
    //通過getRequestDispatcher傳遞路徑將請求轉發給login.jsp
    request.getRequestDispatcher("login.jsp").forward(request,response);
}else{
    request.setAttribute("message", "注冊失敗,用戶名重復");
    //通過getRequestDispatcher傳遞路徑將請求轉發給login.jsp
    request.getRequestDispatcher("register.jsp").forward(request,response);
}

// if(isSuccess){
//     out.println(username);
//      out.println("<font color='green'>注冊成功</font>");
// }else{
//     out.println(username);
//     out.println("<font color='red'>注冊失敗</font>");
// }
%>
</body>
</html>
register_do.jsp

 

<%@ page import="com.Gary.model.User" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
User user = (User)request.getAttribute("user");
%>

用戶名:<%=user.getUsername()%><br/>
年齡:<%=user.getAge()%><br/>
性別:<%=user.getSex()%><br/>
</body>
</html>
personCenter.jsp

 

package com.Gary.model;

public class User {
    private String username;
    private String password;
    private int age;
    private String sex;
    public User(String username, String password, int age, String sex) {
        super();
        this.username = username;
        this.password = password;
        this.age = age;
        this.sex = sex;
    }
    
    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;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    
    
    
}
User.java

 

package com.Gary.model;

public class User {
    private String username;
    private String password;
    private int age;
    private String sex;
    public User(String username, String password, int age, String sex) {
        super();
        this.username = username;
        this.password = password;
        this.age = age;
        this.sex = sex;
    }
    
    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;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    
    
    
}
DBUtil.java

 

  personCenter.jsp通過request內置對象得到用戶信息后輸出其信息

<%
User user = (User)request.getAttribute("user");
%>

用戶名:<%=user.getUsername()%><br/>
年齡:<%=user.getAge()%><br/>
性別:<%=user.getSex()%><br/>

 

  login_do.jsp處理用戶登錄成功時和登錄失敗時事件

<%
String username = request.getParameter("username");
String password = request.getParameter("password");

User user = DBUtil.verifyAccount(username,password);
if(user==null){
//    out.println("登錄失敗,用戶名或密碼錯誤");
    request.setAttribute("message","登錄失敗,用戶名或密碼錯誤");
    request.getRequestDispatcher("login.jsp").forward(request,response);
}else{
//    out.println("登錄成功");
    //登錄請求的轉發,將數據傳遞給personCenter.jsp頁面
    request.setAttribute("user", user);
    request.getRequestDispatcher("personCenter.jsp").forward(request,response);
    
}
%>

 

  register_do.jsp處理用戶注冊成功時和注冊失敗時事件

<%

String username = request.getParameter("username");
String password = request.getParameter("password"); 
int age = Integer.parseInt(request.getParameter("age"));
String sex = request.getParameter("sex");

boolean isSuccess = DBUtil.addUser(username,password,age,sex);

//使用請求轉發
if(isSuccess){
    request.setAttribute("message", "注冊成功,請登錄");
    //通過getRequestDispatcher傳遞路徑將請求轉發給login.jsp
    request.getRequestDispatcher("login.jsp").forward(request,response);
}else{
    request.setAttribute("message", "注冊失敗,用戶名重復");
    //通過getRequestDispatcher傳遞路徑將請求轉發給login.jsp
    request.getRequestDispatcher("register.jsp").forward(request,response);
}

// if(isSuccess){
//     out.println(username);
//      out.println("<font color='green'>注冊成功</font>");
// }else{
//     out.println(username);
//     out.println("<font color='red'>注冊失敗</font>");
// }
%>

 


免責聲明!

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



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