JSP實(shí)時(shí)顯示當(dāng)前系統(tǒng)時(shí)間的四種方式示例解析
JSP顯示當(dāng)前系統(tǒng)時(shí)間的四種方式:
第一種java內(nèi)置時(shí)間類實(shí)例化對(duì)象:
<%@ 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("當(dāng)前時(shí)間為:"+time); %> </body> </html>
第二種方式使用JSP內(nèi)置USEBEAN實(shí)例化時(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>顯示系統(tǒng)時(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" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > --> </head> <body> <jsp:useBean id="time" class="java.util.Date"/> 現(xiàn)在時(shí)間:<%=time%> </body> </html>
第三種方式使用JSP USEBEAN type與beanName配對(duì)使用:
<%@ 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)在時(shí)間:<%=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 實(shí)例<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è)置屬性后的時(shí)間:${time} } <br> </body> </html>
所有代碼均能直接復(fù)制到MYECLIPSE2010
到此這篇關(guān)于JSP實(shí)時(shí)顯示當(dāng)前系統(tǒng)時(shí)間的四種方式示例解析的文章就介紹到這了,更多相關(guān)JSP實(shí)時(shí)顯示當(dāng)前系統(tǒng)時(shí)間內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Http請(qǐng)求長時(shí)間等待無結(jié)果返回解決辦法
這篇文章主要介紹了Http請(qǐng)求長時(shí)間等待無結(jié)果返回解決辦法的相關(guān)資料,需要的朋友可以參考下2017-04-04JSP實(shí)現(xiàn)用戶登錄、注冊(cè)和退出功能
這篇文章主要為大家詳細(xì)介紹了JSP實(shí)現(xiàn)用戶登錄、注冊(cè)和退出功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05JSP使用Common FileUpload組件實(shí)現(xiàn)文件上傳及限制上傳類型實(shí)例代碼
這篇文章主要介紹了JSP使用Common FileUpload組件實(shí)現(xiàn)文件上傳及限制上傳類型實(shí)例代碼,需要的朋友可以參考下2017-09-09Spring mvc 分步式session的實(shí)例詳解
這篇文章主要介紹了Spring mvc 分步式session的實(shí)例詳解的相關(guān)資料,這里舉例說明該如何使用spring mvc 分不式的實(shí)例,希望能幫助到大家,需要的朋友可以參考下2017-08-08jsp的注釋可能會(huì)影響頁面加載速度讓代碼扔繼續(xù)執(zhí)行
注釋里面的java代碼還是會(huì)得到執(zhí)行,可以再查看頁面源代碼上看到執(zhí)行完成的內(nèi)容2014-10-10基于jsp+mysql實(shí)現(xiàn)在線水果銷售商城系統(tǒng)
這篇文章主要介紹了全新基于jsp+mysql實(shí)現(xiàn)的一個(gè)在線水果銷售商城系統(tǒng),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08