JSP实时显示当前系统时间的四种方式示例解析
jsp显示当前系统时间的四种方式:
第一种java内置时间类实例化对象:
<%@ 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内置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>显示系统时间方法一:</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"/> 现在时间:<%=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"/> 现在时间:<%=time%> </body> </html>
第四种方式使用jsp setproperty设置属性:
<%@ 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> 设置属性后的时间:${time} } <br> </body> </html>
所有代码均能直接复制到myeclipse2010
到此这篇关于jsp实时显示当前系统时间的四种方式示例解析的文章就介绍到这了,更多相关jsp实时显示当前系统时间内容请搜索硕编程以前的文章或继续浏览下面的相关文章希望大家以后多多支持硕编程!