JSP例子

編寫一個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>
密&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.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

wKioL1LUCzXDnOmfAACJlVGYLq0654.jpg

登錄成功後:ui

wKiom1LUC0PSfFpPAACs2-_Xp48197.jpg

錯誤頁,10秒後自動跳轉回登陸頁:spa

wKioL1LUCzfD0M3IAACHVdGnjGM504.jpg

相關文章
相關標籤/搜索