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

 

 

  用戶登陸註冊模板進階版  傳送門html

 

用戶登陸註冊模板基礎版java

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

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

  personCenter.jsp展現用戶信息ide

 

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

  

 

<%@ 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版this

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

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

<%@ 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

  

  開發註冊界面3d

  表單

  

  用戶表單信息註冊完畢後提交表單給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>"); // } %>
相關文章
相關標籤/搜索