本文實例講述了java中response對象用法。分享給大家供大家參考,具體如下:
<jsp:forward>動作元素用于運行時在服務器端結束當前頁面的執行,并從當前頁面轉向指定頁面。
使用response對象的setHeader()方法可以設置頁面的自動刷新時間間隔。實現每隔60秒重新加載本頁面的語句為:
實例:使用response實現用戶登錄功能
login.html為登錄表單頁面
login.jsp為信息處理頁面,用來驗證用戶登錄是否成功。
success.jsp為登錄成功后的跳轉頁面。
login.html的源代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>登錄功能實例</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <center> <h1>登錄界面</h1> <form action="login.jsp" method="post"> 姓名:<input type="text" name="name"><br> 密碼:<input type="password" name="pwd"><br> <input type="submit" name="submit" value="登錄"> <input type="reset" name="reset" value="重置"> </form> </center> </body></html>
login.jsp的源代碼如下:
<%@ page language="java" import="java.util.*" contentType="text/html;charset=UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>登錄功能實例</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <center> <h1>登錄功能實例</h1> <% request.setCharacterEncoding("UTF-8"); String name = request.getParameter("name"); String pwd = request.getParameter("pwd"); if(name != null && pwd != null && name.equals("guanlin") && pwd.equals("123")) { //response.sendRedirect("success.jsp"); %> <jsp:forward page="success.jsp"></jsp:forward> <%}else { out.println("<font color='red'>用戶名或密碼錯誤,5秒鐘回到登錄頁面,如果不想等待請點<a href='response/login.html'>返回登錄</a></font>"); response.setHeader("refresh","5;url = login.html"); } %> </center> </body></html>
success.jsp的源代碼如下:
<%@ page language="java" import="java.util.*" contentType="text/html;charset=UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>登錄功能實例</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <center> <h1 style="green">登錄成功!</h1> <% request.setCharacterEncoding("UTF-8"); String name = request.getParameter("name"); String pwd = request.getParameter("pwd"); %> 登錄的用戶名為:<%=name %><br> 登錄的密碼為:<%=pwd %> </center> </body></html>
希望本文所述對大家Java程序設計有所幫助。
新聞熱點
疑難解答