java中response對象用法實例分析
本文實例講述了java中response對象用法。分享給大家供大家參考,具體如下:
<jsp:forward>動作元素用于運行時在服務器端結束當前頁面的執(zhí)行,并從當前頁面轉向指定頁面。
使用response對象的setHeader()方法可以設置頁面的自動刷新時間間隔。實現每隔60秒重新加載本頁面的語句為:
而實現3秒后瀏覽器加載新頁面http://www.dhdzp.com的語句為:
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程序設計有所幫助。
相關文章
IntelliJ IDEA(2019)之mybatis反向生成的實現
這篇文章主要介紹了IntelliJ IDEA(2019)之mybatis反向生成,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10
Jenkins初級應用之Invoke?Phing?targets插件配置
這篇文章主要為大家介紹了Jenkins初級應用之Invoke?Phing?targets的插件配置,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪<BR>2022-04-04

