javaweb 登陆注册页面

时间:2022-09-04 13:34:03

 


视图的数据修改,表中也修改
引用工具类用<%@ page import=""%>

<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>

引入包可以一条一条分着写,也可以在一条内直接用逗号隔开写

<%@ page import="java.util.Date,java.text.SimpleDateFormat " %>

application.getAttribute存的是键值对。。。
强转

object getattribute(string name):获取指定的属性
enumeration getAttributenames():获取所有的属性的名字组成的enumeration
removeattribute(string name):移除指定的属性
void setattribute(string name,object o):设置属性

pagecontext,request,session,application,都有这些方法,成为作用域对象


pagecontext:属性的作用范围仅限于当前页面
request:仅限于同一个请求
session:仅限于一次回话,浏览器打开直到关闭称之为一个回话(回话不失效)
application:属性的作用范围限于当前web应用,是范围最大的,只要在一处设置属性,在其他各处都可以获取到

登陆注册实例

首页页面:

<%@ 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("用户已经存在");
}
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'>跳转页面</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'>跳转页面</a>");
}
}
else{
out.print("输入有误");
}
%>
</body>
</html>