且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JSP例子

更新时间:2022-09-17 18:54:51

编写一个JSP程序,实现用户登录,当用户输入的用户或者密码错误时,将页面重定向到错误提示页,并在该页面显示10秒后,自动返回用户登录页面。

思路:从题目分析,主要涉及到登录页面(index.jsp)、处理页面(deal.jsp)及错误页面(erro.jsp)。里面需要用到requset对象访问请求参数,response对象的重定向网页,定时跳转网页等。

index.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ 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>
密&nbsp;&nbsp;码:<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.jsp

1
2
3
4
5
6
7
8
9
10
11
12
<%@ 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.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ 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>

实现效果:

JSP例子

登陆成功后:

JSP例子

错误页,10秒后自动跳转回登录页:

JSP例子


本文转自lixiyu 51CTO博客,原文链接:http://blog.51cto.com/lixiyu/1351364,如需转载请自行联系原作者