1. 程式人生 > 程式設計 >JSP實時顯示當前系統時間的四種方式示例解析

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,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,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,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實時顯示當前系統時間內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!