java中response對象用法實例分析
本文實例講述了java中response對象用法。分享給大家供大家參考,具體如下:
<jsp:forward>動作元素用于運行時在服務器端結(jié)束當前頁面的執(zhí)行,并從當前頁面轉(zhuǎn)向指定頁面。
使用response對象的setHeader()方法可以設置頁面的自動刷新時間間隔。實現(xiàn)每隔60秒重新加載本頁面的語句為:
而實現(xiàn)3秒后瀏覽器加載新頁面http://chabaoo.cn的語句為:
response的方法:void sendRedirect(String url),將頁面重定向到指定的URL地址上。
實例:使用response實現(xiàn)用戶登錄功能
login.html為登錄表單頁面
login.jsp為信息處理頁面,用來驗證用戶登錄是否成功。
success.jsp為登錄成功后的跳轉(zhuǎn)頁面。
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程序設計有所幫助。
相關(guān)文章
解決Java編譯時錯誤:A JNI error has occurred,ple
這篇文章主要介紹了解決Java編譯時錯誤:A JNI error has occurred,please check your installation and try again,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02