java中response對象用法執行個體分析_java

來源:互聯網
上載者:User

本文執行個體講述了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程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.