新建工程
File>New>Web Project
填寫專案名稱Project Name,舉例為andylife,這裡使用的是預設儲存路徑,如需修改,把下面Use default location前面的勾去掉,並在Directory填寫自己的地址
編寫內容
開啟WebRoot目錄下的index.jsp,把編碼改為utf-8
網頁修改內容,顯示日期時間原始碼:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<body>
<%
//設定重新整理頁面的時間,每隔1秒鐘重新整理一次
response.setHeader("refresh", "10");
%>
當前的系統時間是:
<%
//輸出當前時間
Calendar now = Calendar.getInstance();
out.print(now.get(Calendar.YEAR) + "年");
out.print(now.get(Calendar.MONTH) + 1 + "月");
out.print(now.get(Calendar.DATE) + "日");
out.print(now.get(Calendar.HOUR_OF_DAY) + "時");
out.print(now.get(Calendar.MINUTE) + "分");
out.print(now.get(Calendar.SECOND) + "秒 ");
out.print("星期");
switch (now.get(Calendar.DAY_OF_WEEK)) {
case 1:
out.print("日");
break;
case 2:
out.print("一");
break;
case 3:
out.print("二");
break;
case 4:
out.print("三");
break;
case 5:
out.print("四");
break;
case 6:
out.print("五");
break;
case 7:
out.print("六");
}
out.print(""+"<hr>");
out.print("ID Things need to be done IF DAILY "+"<br>");
%>
</body>
<body>
<%
out.print(""+"<hr>");
%>
</body>
</html>
執行
把頁面部署到Tomcat上
當我們在控制檯看到此資訊時,伺服器就開啟成功了
前端瀏覽
在瀏覽器輸入網址:http://localhost:8080/life/index.jsp
它表示的是,在本地8080埠下,life中的index.jsp