用户登录注册留言程序
登录页
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<%@ 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> <script type="text/javascript"> //判断用户名和密码是否为空 function check() { var uid = document.getElementById("userid"); if(uid.value =="") { alert("用户名不能为空") return false; } if(uform.password.value=="") { alert("密码不能为空") return false; } return true; } </script> </head> <body> //登陆界面 <form id="uform" action="yanzheng.jsp" method="post" onSubmit="return check()"> 用户名: <input id="userid" type="text" name="userid" width="30"/> 密码: <input id="password" type="password" name="mima" width="30"/> <input type="submit" value="登录"/> <a href="zhuce.jsp">注册</a> //若是没有注册点击注册、链接到注册界面 </form> </body> </html>
注册页
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<%@ 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> <script type="text/javascript"> //判断注册时输入的用户信息是否为空 function check() { var uid = document.getElementById("userid"); if (uid.value == "") { alert("用户代码不能为空"); return false; } if(uform.password.value == "") { alert("密码不能为空"); return false; } if(uform.password.value != uform.queren.value) { alert("两次输入密码不一致"); return false; } return true; } </script> </head> <body> //注册界面 <form id="uform" action="saveUser.jsp" onSubmit="return check()"> 用户代码:<input id="userid" type="text" name="userid" width=30/> <br> 用户名称:<input id="username" type="text" name="username" width=30/> <br> 登录密码:<input id="password" type="password" name="password" width=30/> <br> 确认密码:<input id="queren" type="password" name="queren" width=30/> <br> <input type="submit" value="提交"/> </form> </body> </html>
错误消息页
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<%@ 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> <% //接收各个界面传递过来的错误信息 String strMsgID = request.getParameter("msgid"); if(strMsgID ==null || strMsgID.trim().length()==0) { out.print("请正确传递信息"); } else { int msgid = Integer.parseInt(strMsgID); switch(msgid) { case 1: out.print("请正确输入用户名"); break; case 2: out.print("请正确输入密码"); break; case 3: out.print("密码输入错误"); break; case 4: out.print("该用户不存在"); break; case 5: out.print("用户名称不存在"); break; case 6: out.print("提交成功"); break; case 7: out.print("用户已存在"); break; case 8: out.print("请登录系统"); break; default: out.print("传递的msgid不认识"); break; } } //3秒后自动跳转到登陆界面 response.setHeader("refresh", "3;URL=Login.jsp"); %> </body> </html>
保存用户信息
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<%@ 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> <% //接收注册页面请求的参数,接收后判断是否成功并进行处理 String strUserid = request.getParameter("userid"); String strUsername = request.getParameter("username"); String strPW = request.getParameter("mima"); //判断用户注册信息是否为空或空字符串, 如果是则会重定向到消息页面并提示 if(strUserid==null||strUserid.trim().length()==0) { response.sendRedirect("message.jsp?msgid=1"); } else if(strUsername==null||strUsername.trim().length()==0) { response.sendRedirect("message.jsp?msgid=5"); } else if(strPW==null||strPW.trim().length()==0) { response.sendRedirect("message.jsp?msgid=2"); } else { //查找用户信息 Object obj=application.getAttribute(strUserid); //判断此ID下的信息是否为空 if(obj != null) { response.sendRedirect("message.jsp?msgid=7"); } else { strUsername = new String(strUsername.getBytes("ISO-8859-1"),"UTF-8"); //用“#”对用户注册信息进行连接 String strUser = strUserid+ "#" + strUsername +"#" + strPW; application.setAttribute(strUserid, strUser); response.sendRedirect("message.jsp?msgid=6"); } } %> </body> </html>
验证页
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<%@ 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> <% String strUserid = request.getParameter("userid"); String strPW = request.getParameter("mima"); //判断用户填写的登录信息是否为空 if(strUserid==null||strUserid.trim().length()==0) { response.sendRedirect("message.jsp?msgid=1"); } else if(strPW==null||strPW.trim().length()==0) { response.sendRedirect("message.jsp?msgid=2"); } else { //查找用户信息 Object obj =application.getAttribute(strUserid); if(obj != null) { String strUser =obj.toString(); String[] user = strUser.split("#"); String strUID = user[0]; String strUsername = user[1]; String strP = user[2]; if(strUserid.equals(strUID)) { if(strPW.equals(strP)) { out.print("登录成功" + strUsername +"登陆系统"); //跳转到系统页面 session.setAttribute("login", strUsername); } else { response.sendRedirect("message.jsp?msgid=3"); } } else { response.sendRedirect("message.jsp?msgid=4"); } } } %> <br><br> <a href="liuyan.jsp">留言板</a> //点击留言板,链接到留言板界面 </body> </html>
留言板
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.*"%> <%@ page import="java.text.*" %> <!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> <% String userName=""; //检查登录状态 Object o =session.getAttribute("login"); if(o == null) { response.sendRedirect("message.jsp?msgid=8"); } else { userName = o.toString(); } String liuy= request.getParameter("liuy"); if(liuy != null && !liuy.equals("")) { String strLiuy= new String(liuy.getBytes("ISO-8859-1"),"UTF-8"); //附加时间信息 Date dt = new Date(); //格式化时间 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); strLiuy +=" " + sdf.format(dt) + "留言人:" + userName; Object obj= application.getAttribute("liuy"); ArrayList<String> A; if(obj == null) { A = new ArrayList<String>(); } else { A = (ArrayList<String>)obj; } A.add(strLiuy); application.setAttribute("liuy", A); } else { } %> </head> <body> <form action="Liuyan.jsp"> 最新留言: <br> <% int i=1; Object obj = application.getAttribute("liuy"); //使最新的留言永远显示在第一行 if(obj != null) { ArrayList<String> A = (ArrayList<String>)obj; for(int m=A.size()-1; m>0; m--) { out.print(i + "." + A.get(m) + "<br>"); i++; } } %> <br> <br> <br> 留言内容: <br> <textarea rows="10" cols="30" name="liuy"></textarea> <br> <input type="submit" value="提交"> </form> </body> </html>