JSP顯示當前系統(tǒng)時間的四種方式:
第一種java內(nèi)置時間類實例化對象:
%@ page language="java" import="java.util.*" pageEncoding="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%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
title>My JSP 'time4.jsp' starting page/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" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
-->
/head>
body>
%
java.text.SimpleDateFormat simpleDateFormat = new java.text.SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
java.util.Date currentTime = new java.util.Date();
String time = simpleDateFormat.format(currentTime).toString();
out.println("當前時間為:"+time);
%>
/body>
/html>
第二種方式使用JSP內(nèi)置USEBEAN實例化時間類:
%@ page language="java" import="java.util.*" pageEncoding="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%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
title>顯示系統(tǒng)時間方法一:/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" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
-->
/head>
body>
jsp:useBean id="time" class="java.util.Date"/>
現(xiàn)在時間:%=time%>
/body>
/html>
第三種方式使用JSP USEBEAN type與beanName配對使用:
%@ page language="java" import="java.util.*" pageEncoding="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%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
title>My JSP 'time2-useBean-type-beanName.jsp' starting page/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" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
-->
/head>
body>
jsp:useBean id="time" type="java.io.Serializable" beanName="java.util.Date"/>
現(xiàn)在時間:%=time%>
/body>
/html>
第四種方式使用JSP setproperty設(shè)置屬性:
%@ page language="java" import="java.util.*" pageEncoding="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%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
title>My JSP 'time3-setproperty.jsp' starting page/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" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
-->
/head>
body>
jsp:setproperty 實例hr>
jsp:useBean id="time" class="java.util.Date">
jsp:setProperty name="time" property="hours" param="hh"/>
jsp:setProperty name="time" property="minutes" param="mm"/>
jsp:setProperty name="time" property="seconds" param="ss"/>
/jsp:useBean>
br>
設(shè)置屬性后的時間:${time} }
br>
/body>
/html>
所有代碼均能直接復制到MYECLIPSE2010
到此這篇關(guān)于JSP實時顯示當前系統(tǒng)時間的四種方式示例解析的文章就介紹到這了,更多相關(guān)JSP實時顯示當前系統(tǒng)時間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- jsp實現(xiàn)頁面實時顯示當前系統(tǒng)時間的方法