当前位置: 代码网 > it编程>编程语言>Java > JSP实时显示当前系统时间的四种方式示例解析

JSP实时显示当前系统时间的四种方式示例解析

2024年05月15日 Java 我要评论
jsp显示当前系统时间的四种方式:第一种java内置时间类实例化对象:<%@ page language="java" import="java.util.*" pageencoding="ut

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实时显示当前系统时间内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com