JSP / Servlet HelloWorld Example using Eclipse

Posted in :

完成的執行畫面:


要在 eclipse 裡執行 tomcat, 先

Create the dynamic web project

Create the JSP file in eclipse IDE

在 src/main/java/webapp/ 目錄下按右鍵,就可以新增 helloworld.jsp

JSP 檔案內容:

<%@page import="java.time.LocalTime"%>
<%@page import="java.time.LocalDate"%>
<html>
<body>
    <h3>Hello World of Java!</h3>
    Date and Time on Server : <%=LocalDate.now()%> <%=LocalTime.now()%>
</body>
</html>

Run on Server

Right click on the project, select Run As > Run on Server


Creating a Java Servlet

right click on the project, select “New” > “Package“,

我輸入的 package name 是:tw.max.javaee

在 package 按右鍵,選 “New” > “Servlet

只輸入 Class name, 按 Next button.

這個設定畫面,再按Next

取消勾選 doGet, 設定這個 Servlet 只做 POST.

按下 Run, 然後就可以看到這個 url 無法存取 get,

使用下面的 curl 句子,就可以看到Servlet 正確地跟Max說Hello:

curl -d "yourName=max" -X POST http://localhost:8080/jsp-hello-world/HelloServerlet

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *