纯JSP实现用户登录注册,记事本
没有美化,没有格式,没有样式
1.JSP登陆注册
将用户注册的信息保存在application对象中,用于登录时的验证。
首页如下:
如果未登录,在 session 中找不到 currentUser 的值,则直接跳转到其他页面。
<%@ 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>
</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>
<form action="operation.jsp" method="post">
<input type="hidden" name="sub_type" value="reg" />
<input type="text" name="username" placeholder="输入用户名" />
<input type="password" name="password" placeholder="输入密码" />
<input type="password" name="password1" placeholder="确认密码" />
<input type="submit" value="注册" /><a href="login.jsp">返回登陆</a>
</form>
</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>
<form action="operation.jsp" method="post">
<input type="hidden" name="sub_type" value="log" />
<input type="text" name="username" placeholder="输入用户名" />
<input type="password" name="password" placeholder="输入密码" />
<input type="submit" value="登录" /><a href="register.jsp">注册新用户</a>
</form>
</body>
</html>
用户类,用于存放用户信息:
仅有用户名和代码两个属性。
package test;
public class User {
private String username;
private String password;
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;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
}
}
逻辑控制页面如下:
首先根据给登录页面和注册页面设置的隐藏的 sub_type 属性判断是登录还是注册,然后进行相应的判断。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="test.User"%>
<%
//获取请求的信息
String sub_type = request.getParameter("sub_type");
String username = request.getParameter("username");
String password = request.getParameter("password");
String password1 = request.getParameter("password1");
//判断是登录页面还是注册页面
if("reg".equals(sub_type)) {
if (!password.equals(password1)) {
out.print("两次输入的密码不一致 !");
} else {
Object obj = application.getAttribute(username);
if (obj != null) { //用传来的用户名找用户,如果不为空则用户已存在
out.print("用户名已经存在 !");
} else {
User user = new User(); //实例化一个用户并设置信息
user.setUsername(username);
user.setPassword(password);
application.setAttribute(username, user);
out.print("注册成功 !");
}
}
}
if("log".equals(sub_type)) {
Object obj = application.getAttribute(username);
if(obj!=null) {
User u = (User)obj;//将获取到的对象强转型,然后获取信息进行判断
if(password.equals(u.getPassword())) {
session.setAttribute("currentUser", u);
response.sendRedirect("index.jsp");
} else {
out.print("密码不对啊 !");
}
} else {
out.print("用户名不存在 !");
}
}
out.print("<br>");
out.print("<a href='login.jsp'>跳转登陆</a>");
%>
2.记事本:
在用户登录注册的基础上进行修改,登陆后的用户可以在记事本留言,其他用户登录后可以看到,按照时间倒序排序,可以看到用户名,留言信息,留言时间。
添加一个留言类, Says类,有 用户,留言时间,留言内容三个属性。
主页如下,除了判断登录用户,设置 utf-8 之外,还有注意输出的时候先倒序,遍历输出之后再次倒序。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="test.Says,test.User,java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%
Object obj1 = session.getAttribute("currentUser");
if (obj1 == null) {
response.sendRedirect("denglu.jsp");
}
%>
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="chuli.jsp" method="post">
<input type="hidden" name="sub_type" value="tex" />
<textarea rows="4" cols="12" name="text"></textarea>
<input type="submit" value="提交">
</form>
<%
Object obj=application.getAttribute("s");
if(obj!=null){
ArrayList<Says> list=(ArrayList)obj;
Collections.reverse(list);//倒序,以保证后写入的内容排在上边
for(Says say : list){
out.print(say.getUname()+" "+"|"+" ");
out.print(say.getDate()+"<br>");
out.print(say.getSays()+"<br>");
}
Collections.reverse(list);//再次倒序,否则输出的后会出问题
}
%>
</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>
<form action="chuli.jsp" method="post">
<input type="hidden" name="sub_type" value="reg" />
<input type="text" name="username" placeholder="输入用户名" />
<input type="password" name="password" placeholder="输入密码" />
<input type="password" name="password1" placeholder="确认密码" />
<input type="submit" value="注册" /><a href="denglu.jsp">返回登陆</a>
</form>
</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>
<form action="chuli.jsp" method="post">
<input type="hidden" name="sub_type" value="log" />
<input type="text" name="username" placeholder="输入用户名" />
<input type="password" name="password" placeholder="输入密码" />
<input type="submit" value="登录" /><a href="zhuce.jsp">注册新用户</a>
</form>
</body>
</html>
用户类:
package test;
public class User {
private String username;
private String password;
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;
}
}
留言类:
package test;
import java.util.Date;
public class Says {
private String uname;//暂无用
private String date;
private String says;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getSays() {
return says;
}
public void setSays(String says) {
this.says = says;
}
@Override
public String toString() {
return "Says [uname=" + uname + ", date=" + date + ", says=" + says + "]";
}
}
逻辑处理:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="test.Says,test.User,java.util.*"%>
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
%>
<%
String sub_type = request.getParameter("sub_type");
String password = request.getParameter("password");
String password1 = request.getParameter("password1");
Says says=new Says();
if("reg".equals(sub_type)) {
String username = request.getParameter("username");
System.out.println(username);
if (!password.equals(password1)) {
out.print("两次输入的密码不一致 !");
} else {
Object obj = application.getAttribute(username);
if (obj != null) {
out.print("用户名已经存在 !");
} else {
User user = new User();
user.setUsername(username);
user.setPassword(password);
application.setAttribute(username, user);
out.print("注册成功 !");
}
}
out.print("<br>");
out.print("<a href='denglu.jsp'>跳转登陆</a>");
}
if("log".equals(sub_type)) {
String username = request.getParameter("username");
System.out.println(username);
application.setAttribute("name",username);
Object obj = application.getAttribute(username);
if(obj!=null) {
User u = (User)obj;
if(password.equals(u.getPassword())) {
session.setAttribute("currentUser", u);
//String name=u.getUsername();
//application.setAttribute("name",name);
response.sendRedirect("main.jsp");
} else {
out.print("密码不对啊 !");
}
} else {
out.print("用户名不存在 !");
}
out.print("<br>");
out.print("<a href='denglu.jsp'>跳转登陆</a>");
}
if("tex".equals(sub_type)){
String say = request.getParameter("text");
Date d=new Date();
Object sobj=application.getAttribute("name");//获取用户名
String sname=(String)sobj;
System.out.println(sname);
says.setUname(sname);
says.setSays(say);
says.setDate(d.toLocaleString());
Object obj=application.getAttribute("s");
if(obj==null){
List list =new ArrayList();
list.add(says);
application.setAttribute("s",list);
}else{
List<Says> list=(List)obj;
list.add(says);
application.setAttribute("s",list);
}
response.sendRedirect("main.jsp");
}
%>
纯JSP实现用户登录注册,记事本的更多相关文章
- javaweb学习总结(二十二)——基于Servlet+JSP+JavaBean开发模式的用户登录注册
一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...
- JavaWeb学习 (二十一)————基于Servlet+JSP+JavaBean开发模式的用户登录注册
一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...
- 基于Servlet+JSP+JavaBean开发模式的用户登录注册
http://www.cnblogs.com/xdp-gacl/p/3902537.html 一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBea ...
- javaweb(二十二)——基于Servlet+JSP+JavaBean开发模式的用户登录注册
一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...
- JavaWeb实现用户登录注册功能实例代码(基于Servlet+JSP+JavaBean模式)
一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...
- Java Spring+Mysql+Mybatis 实现用户登录注册功能
前言: 最近在学习Java的编程,前辈让我写一个包含数据库和前端的用户登录功能,通过看博客等我先是写了一个最基础的servlet+jsp,再到后来开始用maven进行编程,最终的完成版是一个 Spri ...
- 使用JSP实现用户登录
本文讲述使用JSP实现用户登录,包括用户登录.注册和退出功能等. 1.系统用例图 2.页面流程图 3.数据库设计 本例使用oracle数据库 创建用户表 包括id,username,password和 ...
- JavaWeb_(session和application)用户登录注册模板_进阶版
用户登录注册模板_基础版 传送门 用户登录注册模板进阶版 添加了获得获得当前登录用户信息及存储登录人数 用户登录后,在首页.注册页.登录页显示登录用户信息和存储登录人数信息 目录结构 <%@pa ...
- JavaWeb_(request和response)用户登录注册模板_基础版
用户登录注册模板进阶版 传送门 用户登录注册模板基础版 登录:当用户登录成功时,跳转到personCenter.jsp,当用户登录失败时,跳转到login.jsp并给出提示 注册:当用户注册成功时,跳 ...
随机推荐
- 分享一次Oracle数据导入导出经历
最近工作上有一个任务要修改一个比较老的项目,分公司这边没有这个项目数据库相关的备份,所以需要从正式环境上面导出数据库备份出来在本地进行部署安装,之前在其它项目的时候也弄过这个数据库的部署和安装,也写了 ...
- Vue--props
组件实例的作用域是孤立的.这意味着不能 (也不应该) 在子组件的模板内直接引用父组件的数据.要让子组件使用父组件的数据,我们需要通过子组件的 props 选项. 字面量语法 vs 动态语法 初学者常犯 ...
- input file 上传图片问题
html代码如下: <input id="fileup" type="file" accept="image/*" capture=& ...
- python基础(4):条件语句与循环语句
今天我们看看条件语句与循环语句. 预习: 1.使用while循环输出1 2 3 4 5 6 8 9 10 2.求1-100的所有数的和 3.输出 1-100 内的所有奇数 4.输出 1-100 内的所 ...
- 阿里巴巴2018届应届生在线编程测验-研发工程师C/C++
刚才去做了阿里巴巴的编程测验,好消息是抽到的题相对别的题简单一些,坏消息是编的太慢了,没有做完. 现在把题目和自己后来编出来的代码贴在这里,供大家参考. 题目: 1. 从命令行输入若干个正整数(大于等 ...
- 不同浏览器创建 ajax XMLHTTPRequest对象的方法及兼容性问题总结
XMLHttpRequest 对象是AJAX功能的核心,要开发AJAX程序必须从了解XMLHttpRequest 对象开始. 了解XMLHttpRequest 对象就先从创建XMLHttpReques ...
- Java之面向对象例子(二)
定义一个Book类,在定义一个JavaBook类继承他 //book类 package com.hanqi.maya.model; public class Book { public String ...
- Java基础语法<十一> 异常 断言 日志 调试
1 处理错误 1.1 异常分类 Error类层次描述了Java运行时系统的内部错误和资源耗尽错误. 设计Java程序时,主要关注Exception层次结构. 由程序错误导致的异常属于RuntimeEx ...
- hdu_1907:John(Nim变形)
题目链接 仍是取石子,不过取到最后一个的败 参考链接:http://www.voidcn.com/blog/liwen_7/article/p-3341825.html 简单一句话就是T2 S0必败 ...
- 第2章 rsync(二):inotify+rsync详细说明和sersync
本文目录: inotify+rsync 1.1 安装inotify-tools 1.2 inotifywait命令以及事件分析 1.3 inotify应该装在哪里 1.4 inotify+rsync示 ...