编写一个JSP程序,实现用户登陆,当用户输入的用户或者密码错误时,将页面重定向到错误提示页,并在该页面显示10秒后,自动返回用户登陆页面。javascript
思路:从题目分析,主要涉及到登陆页面(index.jsp)、处理页面(deal.jsp)及错误页面(erro.jsp)。里面须要用到requset对象访问请求参数,response对象的重定向网页,定时跳转网页等。html
index.jspjava
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!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=GB18030"> <title>Insert title here</title> </head> <body> <form name="form" method="post" action="deal.jsp"> 用户名:<input name="username" type="text" id="username"><br> 密 码:<input name="pwd"type="text" id="pwd"><br> <input name="submit"type="submit" id="submit"> <input name="reset" type="reset" id="submit"> </body> </html>
deal.jspjsp
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <% request.setCharacterEncoding("GB18030"); String username=request.getParameter("username"); String pwd=request.getParameter("pwd"); if("lee".equals(username)&&"123".equals(pwd)){ out.print("<script language='javascript'>alert('登陆成功!');window.location.href='index.jsp';</script>"); }else{ response.sendRedirect("erro.jsp"); } %>
erro.jspide
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <% response.setHeader("refresh","10;URL=index.jsp");//定时跳转网页 %> <!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=GB18030"> <title>错误提示页</title> </head> <body> 你输入的用户名或者密码错误,请从新输入! </body> </html>
实现效果:post
登录成功后:ui
错误页,10秒后自动跳转回登陆页:spa