本文執行個體講述了java中response對象用法。分享給大家供大家參考,具體如下:
<jsp:forward>動作元素用於運行時在伺服器端結束當前頁面的執行,並從當前頁面轉向指定頁面。
使用response對象的setHeader()方法可以設定頁面的自動重新整理時間間隔。實現每隔60秒重新載入本頁面的語句為:
複製代碼 代碼如下:
response.setHeader("refresh",60);
而實現3秒後瀏覽器載入新頁面http://www.jb51.net的語句為:
複製代碼 代碼如下:
response.setHeader("refresh","3;URL=http://www.jb51.net");
response的方法:void sendRedirect(String url),將頁面重新導向到指定的URL地址上。
執行個體:使用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程式設計有所協助。