項目中獲取服務器端時間在前臺顯示

<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>回顧傳統Web應用請求和響應特點【顯示當前時間】</title>
  </head>
  <body>
	
	當前時間:${requestScope.nowStr}<br/>
	<input id="buttonID" type="button" value="獲取當前時間"/><p/>
	
	<script type="text/javascript">
		//定位按鈕,同時添加單擊事件
		document.getElementById("buttonID").onclick = function(){
			//發送請求到服務器
			var url = "${pageContext.request.contextPath}/TimeServlet";
			window.location.href = url;
		}
	</script>
  </body>
</html>

package cn.itcast.javaee.js.time;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 回顧傳統Web應用請求和響應特點【顯示當前時間】 
 * @author AdminTC
 */
public class TimeServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		//測試
		System.out.println("GET");
		//構造SimpleDateFormat對象
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//將當前日期按照指定格式輸出成字符串
		String nowStr = sdf.format(new Date());
		//將結果綁定到request域對象中
		request.setAttribute("nowStr",nowStr);
		//轉發到06_time.jsp頁面
		request.getRequestDispatcher("/06_time.jsp").forward(request,response);
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章