用户登录注册留言习题
登录页
<%@ 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; //返回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/><br> 密码:<input id="passeord" type="password" name="password" width=30/><br> <input type="submit" value="登录" /> <a href ="zhuce.jsp">注册新用户</a> </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> <% String strMsgId =request.getParameter("msgid"); if(strMsgId == null || strMsgId.trim().length()==0) { out.print("信息错误"); } else { int iMsgid = Integer.parseInt(strMsgId); switch (iMsgid) { 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("无法识别信息"); break; } } response.setHeader("refresh","2;URL=login.jsp"); //信息错误跳转回login.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> <% //接收用户信息 String strUserid =request.getParameter("userid"); String strUsername =request.getParameter("username"); String strPW =request.getParameter("password"); //判断用户输入的信息是否为空 if(strUserid == null || strUserid.trim().length() == 0) { response.sendRedirect("massage.jsp?msgid=1"); } else if(strUsername == null || strUsername.trim().length() == 0) { response.sendRedirect("massage.jsp?msgid=5"); }else if(strPW == null || strPW.trim().length() == 0) { response.sendRedirect("massage.jsp?msgid=2"); } else //查找用户信息 { Object obj=application.getAttribute(strUserid); if(obj!=null){ response.sendRedirect("massage.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("massage.jsp?msgid=6"); } } %> </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> <% String strUserid =request.getParameter("userid"); String strPW =request.getParameter("password"); if(strUserid == null || strUserid.trim().length() == 0) { response.sendRedirect("massage.jsp?msgid=1"); } else if(strPW == null || strPW.trim().length() == 0) { response.sendRedirect("massage.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(strPW.equals(strP)) { out.print("欢迎"+strUsername+"登陆"); //跳转到系统页面 session.setAttribute("login",strUsername); } else { response.sendRedirect("massage.jsp?msgid=3"); } } else { response.sendRedirect("massage.jsp?msgid=4"); } } %> <br><br><br> <a href="liuyan.jsp">留言簿</a> </body> </html>
留言页
<%@ 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 oo =session.getAttribute("login"); if(oo==null) { response.sendRedirect("massage.jsp?msgid=8"); } else { userName=oo.toString(); } String liuy = request.getParameter("liuyan"); //if(liuy != null && liuy.length()!=0) //if(liuy != null && liuy !=null) if(liuy != null && !liuy.trim().equals("")) //判断liuy不能为空且也不能为空字符串以及去掉空格 { String strLiuy =new String(liuy.getBytes("ISO-8859-1"),"UTF-8"); //进行转码 //附加时间信息 Date dt=new Date(); //格式化时间 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-hh:mm:ss"); strLiuy += " "+ df.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); } %> </head> <body> <form> 最新留言:<br><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> <textarea rows="10" cols="30" name="liuyan"></textarea><br> <input type="submit" value="提交"> </form> </body> </html>