jsp模拟登录系统
1、登录页面代码
<%@ 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> <% session.invalidate(); %> <form action="panduan.jsp"method="post"> 用户名:<input type="text" name="user"><br> 密    码:<input type="password" name="mima"><br> <input type="submit" value="登录"> </form> </body> </html>
2、判断提交数据代码
<%@ 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> <% String nm=request.getParameter("user"); String mm=request.getParameter("mima"); if(nm!=""&&mm!="") { if(nm.equals("lisi")&&mm.equals("123")) { session.setAttribute("user", nm); response.sendRedirect("zhuye.jsp"); } else { out.print("请输入正确的用户名和密码!"); %> <br> <a href="denglu1.jsp">点击返回登录页</a> <% } } else { out.print("您输入的用户名或密码为空!"); %> <br> <a href="denglu1.jsp">点击返回登录页</a> <% } %> </body> </html>
3、主页代码
<%@ 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> <% Object ob=session.getAttribute("user"); if(ob!=null) { out.print("欢迎您,"+ob.toString()); } else { out.print("连接超时,请3秒后重新登录!"); response.setHeader("refresh", "3;URL=denglu1.jsp"); } %> <br> <a href="denglu1.jsp">退出当前用户!</a> </body> </html>
4、运行结果
【1】输入为空时,运行结果
【2】输入错误时运行结果
【4】输入正确时运行结果