java中response對(duì)象用法實(shí)例分析
本文實(shí)例講述了java中response對(duì)象用法。分享給大家供大家參考,具體如下:
<jsp:forward>動(dòng)作元素用于運(yùn)行時(shí)在服務(wù)器端結(jié)束當(dāng)前頁面的執(zhí)行,并從當(dāng)前頁面轉(zhuǎn)向指定頁面。
使用response對(duì)象的setHeader()方法可以設(shè)置頁面的自動(dòng)刷新時(shí)間間隔。實(shí)現(xiàn)每隔60秒重新加載本頁面的語句為:
而實(shí)現(xiàn)3秒后瀏覽器加載新頁面http://www.dbjr.com.cn的語句為:
response的方法:void sendRedirect(String url),將頁面重定向到指定的URL地址上。
實(shí)例:使用response實(shí)現(xiàn)用戶登錄功能
login.html為登錄表單頁面
login.jsp為信息處理頁面,用來驗(yàn)證用戶登錄是否成功。
success.jsp為登錄成功后的跳轉(zhuǎn)頁面。
login.html的源代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>登錄功能實(shí)例</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>登錄功能實(shí)例</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>登錄功能實(shí)例</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'>用戶名或密碼錯(cuò)誤,5秒鐘回到登錄頁面,如果不想等待請(qǐng)點(diǎn)<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>登錄功能實(shí)例</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>
希望本文所述對(duì)大家Java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Java 堆內(nèi)存與棧內(nèi)存詳細(xì)介紹
這篇文章主要介紹了Java 堆內(nèi)存與棧內(nèi)存詳細(xì)介紹的相關(guān)資料,這里對(duì)java 的堆內(nèi)存和棧內(nèi)存進(jìn)行了詳細(xì)的分析,需要的朋友可以參考下2016-11-11Java中創(chuàng)建線程的兩種方式詳細(xì)說明
這篇文章主要介紹了Java中創(chuàng)建線程的兩種方式詳細(xì)說明,Java使用java.lang.Thread類代表線程,所有的線程對(duì)象都必須是Thread類或其子類的實(shí)例,每個(gè)線程的作用是完成一定的任務(wù),實(shí)際上就是執(zhí)行一段程序流即一段順序執(zhí)行的代碼,需要的朋友可以參考下2023-11-11解決Java編譯時(shí)錯(cuò)誤:A JNI error has occurred,ple
這篇文章主要介紹了解決Java編譯時(shí)錯(cuò)誤:A JNI error has occurred,please check your installation and try again,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02java 中List按照date排序的實(shí)現(xiàn)
這篇文章主要介紹了java 中List按照date排序的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-06-06Mybatis Plus代碼生成器(時(shí)間管理大師)
這篇文章主要介紹了Mybatis Plus代碼生成器(時(shí)間管理大師)的相關(guān)知識(shí),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07