javaweb 登陆注册页面
?
視圖的數(shù)據(jù)修改,表中也修改
引用工具類用<%@ page import=""%>
引入包可以一條一條分著寫,也可以在一條內(nèi)直接用逗號隔開寫
<%@ page import="java.util.Date,java.text.SimpleDateFormat " %>application.getAttribute存的是鍵值對。。。
強轉(zhuǎn)
object getattribute(string name):獲取指定的屬性
enumeration getAttributenames():獲取所有的屬性的名字組成的enumeration
removeattribute(string name):移除指定的屬性
void setattribute(string name,object o):設(shè)置屬性
pagecontext,request,session,application,都有這些方法,成為作用域?qū)ο?/span>
pagecontext:屬性的作用范圍僅限于當(dāng)前頁面
request:僅限于同一個請求
session:僅限于一次回話,瀏覽器打開直到關(guān)閉稱之為一個回話(回話不失效)
application:屬性的作用范圍限于當(dāng)前web應(yīng)用,是范圍最大的,只要在一處設(shè)置屬性,在其他各處都可以獲取到
登陸注冊實例
首頁頁面:
<%@ 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> <% //防止用戶不登陸進入首頁Object obj=session.getAttribute("currentUser");if(obj==null){response.sendRedirect("login.jsp");}%><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body><h1>這是首頁</h1><a href="login.jsp"><input type="button" name="login" value="登陸"/></a> <br><a href="resist.jsp"><input type="button" name="resist" value="注冊"/></a> </body> </html> <%@ 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><h1>這是注冊頁面</h1><form action="operation.jsp" method="post" ><input type="text" name="username" placeholder="請輸入用戶名"/><br><input type="text" name="password" placeholder="請輸入密碼"/><br><input type="text" name="password1" placeholder="請再次輸入密碼"/><br><a href="login.jsp"><input type="submit" value="注冊"/></a></form> </body> </html> <%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ page import="textloginresist.User" %> <!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><%String username=request.getParameter("username");String password=request.getParameter("password");String password1=request.getParameter("password1");if(password.equals(password1)){Object o=application.getAttribute(username);if(o!=null){out.print("用戶已經(jīng)存在");}else{User u = new User();u.setUsername(username);u.setPassword(password);application.setAttribute(username, u);}}else{out.print("兩次輸入不一致");}out.print("注冊成功");out.print("<a href='login.jsp'>跳轉(zhuǎn)頁面</a>");%> </body> </html>登陸:
<%@ 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><h1>這是登陸頁面</h1><form action="operationlogin.jsp" method="post" ><input type="text" name="username" placeholder="請輸入用戶名"/><br><input type="text" name="password" placeholder="請輸入密碼"/><br><a href="index.jsp"><input type="submit" value="登陸"/></a></form> </body> </html> <%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ page import="textloginresist.User" %> <!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><%String username=request.getParameter("username");String password=request.getParameter("password");if(username!=null){User u = (User)application.getAttribute(username);if(username.equals(u.getUsername())){if(password.equals(u.getPassword())){session.setAttribute("currentUser", u);//防止用戶不登陸進入首頁response.sendRedirect("index.jsp");}else{out.print("密碼錯誤");}}else {out.print("用戶不存在!");out.print("<a href='login.jsp'>跳轉(zhuǎn)頁面</a>");}}else{out.print("輸入有誤");}%> </body> </html>?
轉(zhuǎn)載于:https://www.cnblogs.com/NCL--/p/7398206.html
總結(jié)
以上是生活随笔為你收集整理的javaweb 登陆注册页面的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: url-pattern写成/和/*的区别
- 下一篇: 2017中国大学生程序设计竞赛 - 网络