JSP实时显示当前系统时间的四种方式示例解析(在jsp页面中显示变量的值)满满干货

随心笔谈2年前发布 admin
206 0 0

文章摘要

这篇文章介绍了在JSP中显示当前系统时间的四种方法: 1. **使用Java内置的`java.util.Date`类** 通过`java.text.SimpleDateFormat`和`java.util.Date`对象格式化当前时间。 2. **使用JSP内置的`jsp:useBean`方法** 将`java.util.Date`类实例化并实时更新。 3. **使用`jsp:useBean`(`type`与`beanName`结合使用)** 根据指定的`beanName`和`type`动态加载并使用`java.util.Date`类实例。 4. **使用`jsp:setProperty`设置属性** 通过设置属性的方式动态获取小时、分钟、秒。 这些方法均可直接复制到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=”https://www.jb51.net/article/<%=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=”https://www.jb51.net/article/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=”https://www.jb51.net/article/<%=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=”https://www.jb51.net/article/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=”https://www.jb51.net/article/<%=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=”https://www.jb51.net/article/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=”https://www.jb51.net/article/<%=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=”https://www.jb51.net/article/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实时显示当前系统时间内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:jsp实现页面实时显示当前系统时间的方法

© 版权声明

相关文章