加入收藏 | 设为首页 | 会员中心 | 我要投稿 银川站长网 (https://www.0951zz.com/)- 云通信、基础存储、云上网络、机器学习、视觉智能!
当前位置: 首页 > 综合聚焦 > 编程要点 > 语言 > 正文

怎么用JSP实现显露实时服务器时间

发布时间:2023-06-03 11:16:33 所属栏目:语言 来源:
导读:今天这篇给大家分享的知识是“怎么用JSP实现显示实时服务器时间”,小编觉得挺不错的,对大家学习或是工作可能会有所帮助,对此分享发大家做个参考,希望这篇“怎么用JSP实现显示实时服务器时间&rdquo

今天这篇给大家分享的知识是“怎么用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,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

(编辑:银川站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!