首先,搭建登录页面并且可以链接注册页面
<%@ 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="paramType" value="log" /> <table align="center"> <tr> <td>用户名: </td><td><input type="text" name="username" /></td> </tr> <tr> <td>密码: </td><td><input type="password" name="password" /></td> </tr> <tr> <td colspan="2"> <input type="submit" value="登录" /> <a href="register.jsp">前往注册</a> </td> </tr> </table> </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="paramType" value="log" /> <table align="center"> <tr> <td>用户名: </td><td><input type="text" name="username" /></td> </tr> <tr> <td>密码: </td><td><input type="password" name="password" /></td> </tr> <tr> <td colspan="2"> <input type="submit" value="登录" /> <a href="register.jsp">前往注册</a> </td> </tr> </table> </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="paramType" value="reg" /> <table align="center"> <tr> <td>用户名: </td><td><input type="text" name="username" /></td> </tr> <tr> <td>密码: </td><td><input type="password" name="password" /></td> </tr> <tr> <td>确认密码: </td><td><input type="password" name="password1" /></td> </tr> <tr> <td colspan="2"> <input type="submit" value="注册" /> <a href="login.jsp">登录</a> </td> </tr> </table> </form> </body> </html>
然后是后台页面,分别处理登录和注册的信息
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("utf-8"); String s = request.getParameter("paramType"); if(s.equals("log")) { String username = request.getParameter("username"); String password = request.getParameter("password"); if(username!=null&&username.trim().length() > 0) { Object obj = application.getAttribute(username); if(obj==null) { response.sendRedirect("message.jsp?code=4"); } else { String userinfo = (String)obj; if(password.equals(userinfo.split(",")[1])) { session.setAttribute("currentUser", username); response.sendRedirect("index.jsp"); } else { response.sendRedirect("message.jsp?code=5"); } } } } else if(s.equals("reg")) { String username = request.getParameter("username"); String password = request.getParameter("password"); String password1 = request.getParameter("password1"); Object obj = application.getAttribute(username); if(obj==null) { if(password.equals(password1)) { application.setAttribute(username, username+","+password); response.sendRedirect("message.jsp?code=3"); } else { response.sendRedirect("message.jsp?code=2"); } } else { response.sendRedirect("message.jsp?code=1"); } } else { response.sendRedirect("message.jsp?code=6"); } %>
插入一个信息页面
<%@ 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"> <meta http-equiv="refresh" content="5; url='login.jsp'"> <title>Insert title here</title> </head> <body> <% String code = request.getParameter("code"); if(code.equals("1")) { out.print("<span style='color:red;'><h1>该用户已经存在 !</h1></span>"); } if(code.equals("2")) { out.print("<span style='color:red;'><h1>输入的两次不一致 !</h1></span>"); } if(code.equals("3")) { out.print("<span style='color:green;'><h1>注册成功 !</h1></span>"); } if(code.equals("4")) { out.print("<span style='color:red;'><h1>用户不存在 !</h1></span>"); } if(code.equals("5")) { out.print("<span style='color:red;'><h1>密码错误 !</h1></span>"); } if(code.equals("6")) { out.print("<span style='color:red;'><h1>非法访问 !</h1></span>"); } 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> <% String username = (String)session.getAttribute("currentUser"); if(username==null) { response.sendRedirect("login.jsp"); } %> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body>
<%
request.setCharacterEncoding("utf-8");
String mess = request.getParameter("k");
Date date = new Date();
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = sdf.format(date);
String a = (String)session.getAttribute("Current");
ArrayList list = (ArrayList)application.getAttribute("liuyan");
list.add(s+" "+a+"发表一条评论:<br>"+mess);
application.setAttribute("liuyan", list);
response.sendRedirect("message.jsp");
%>
</body> </html>