使用jsp,tomcat实现用户登录注册留言的代码
以下jsp中,未使用样式表对网页进行排版和表单的验证(每个jsp的表单填写的时候应该进行空值与空格的验证,防止提交时出错)
所有错误,链接到error.jsp
<%@ 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>登陆界面</title> </head> <body> 用户登录 <br /> <form action="verify.jsp" method="post"> 用户名<input type="text" name="username" /><br /> 密码<input type="password" name="pwd" /><br /> <input type="submit" /> <a href="register.jsp"><input type="button" value="注册" /></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>登陆验证界面</title> </head> <body> <% //判断输入用户名为空时,跳转至错误页面 if (request.getParameter("username") == null) { %> <jsp:forward page="error.jsp" /> <% } //取得输入的用户名 String name = new String(request.getParameter("username").getBytes("iso-8859-1"), "utf-8"); //判断用户名是否已经存在,存在的话,继续判断密码是否一致 if (application.getAttribute(name) != null) { String storeName = application.getAttribute(name).toString(); String[] stores = storeName.split("#"); String inputPwd = request.getParameter("pwd"); String pwd = stores[1]; if (pwd.equals(inputPwd)) {//密码是否一致 String date = stores[2]; session.setAttribute("name", name); %> <%=name%>登陆成功 <br /> 注册时间<%=date%><br /> 5秒后跳转至留言界面 <% response.setHeader("refresh", "5;URL=message.jsp"); %> <% } } else {//否则跳转 %> 登陆失败 <br /> 5秒后跳转至登陆界面<% response.setHeader("refresh", "5;URL=index.jsp"); %> <% } %> </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>注册界面</title> </head> <body> <form method="post" action="success.jsp"> 用户名<input type="text" name="username" /><br /> 密码<input type="password" name="pwd" /><br /> 确认密码<input type="password" /><br /> <input type="submit" value="注册" /><br /> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.*"%> <!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>注册验证界面</title> </head> <body> <% //判断输入用户名为空时,跳转至错误页面 if (request.getParameter("username") == null) { %> <jsp:forward page="error.jsp" /> <% } //取得输入的用户名 String name = new String(request.getParameter("username").getBytes("iso-8859-1"), "utf-8"); //取得输入的密码 String pwd = request.getParameter("pwd"); //取得服务器中是否已经存在此用户名,如果不存在进行注册录入数据 if (application.getAttribute(name) == null) { //保存用户信息 String userInfo = name + "#" + pwd + "#" + new Date(); //将用户信息保存在服务器 application.setAttribute(name, userInfo); //并且设置当前登陆会话 session.setAttribute("name", name); %> 注册成功 <br /> 注册名:<%=name%><br /> 时间:<%=new Date()%><br /> 5秒后跳转至留言界面 <% response.setHeader("refresh", "5;URL=message.jsp"); %> <% } else {//如果用户名已经存在则跳转 out.print("注册失败,用户名重复"); %> <br />5秒后跳转至登陆界面 <% response.setHeader("refresh", "5;URL=index.jsp"); %> <% } %> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.*"%> <!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>留言界面</title> </head> <body> <form method="post" onsubmit="return checkFlag()"> <textarea cols="20" rows="5" name="mes"> 这里请添加留言 </textarea> <br /> <input type="submit" /> </form> <br /> 实时留言信息 <br /> <% String mes = request.getParameter("mes"); Object name = session.getAttribute("name"); List<String> list = new ArrayList<>(); //session判断用户名为空时,跳转至错误页面 if (name == null) { %> <jsp:forward page="error.jsp" /> <% } //首先判断留言信息是否为空,防止没有留言时报空指针异常 if (application.getAttribute("mes") != null) { list = (List<String>) application.getAttribute("mes"); if (mes == null) { for (String str : list) { out.print(str + "<br/>"); } } else if (mes != null) { response.sendRedirect("message.jsp");//这里加入一个页面重定向,防止页面刷新时重新提交表单 list.add(0, name + ":" + new String(mes.getBytes("iso-8859-1"), "utf-8") + " 时间:" + new Date()); application.setAttribute("mes", list); for (String str : list) { out.print(str + "<br/>"); } } } else if (mes != null) {//否则,判断留言是否为空,直接将当前留言存入客户端,直接输出 response.sendRedirect("message.jsp"); list.add(0, name + ":" + new String(mes.getBytes("iso-8859-1"), "utf-8") + " 时间:" + new Date()); application.setAttribute("mes", list); for (String str : list) { out.print(str + "<br/>"); } } %> </body> <script> <!--此脚本对页面刷新取消提交,自测无效,应该是浏览器自己的设置--> var flag = false; function checkFlag() { if (flag == true) { return false; } flag = true; return true; } </script> </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>错误界面</title> </head> <body> 非法接入 <br /> 5秒后跳转至登陆页面 <% response.setHeader("refresh", "5;URL=index.jsp"); %> </body> </html>