Web Server(如Apache)的
- 可以做:
- client 請求網頁,找到該靜態網頁,不會做計算
- 原封不動地拿出此靜態網頁,添加header
- 傳送回client(每個client看到的東西都一樣)
- 不能做:(但是可以找到能夠處理此request的application(如Tomcat))
- 產生動態網頁
- 將資料存在server
Servlet Java檔案
import java.io.IOException; import java.io.PrintWriter; import java.util.Date; import javax.servlet.*; import javax.servlet.http.*; public class Ch1Servlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter writer = resp.getWriter(); Date today = new Date(); writer.println("<html>"+ "<body>" + "<h1 align=center>HF's Chapter Servlet</h1>" + "<br>" + today + "</body>" + "</html>"); } }
Servlet Deployment Descriptor(web.xml)檔,部署描述檔
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <servlet> <servlet-name>Ch1 Servlet</servlet-name> <servlet-class>Ch1Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Ch1 Servlet</servlet-name> <url-pattern>/serv1</url-pattern> </servlet-mapping> </web-app>
- servlet-class: 真實,完整的servlet class名字,但不含.class (file path name)
- servlet-name: alias (deploy name)
- url-pattern: 使用者連線時請求的名稱(public URL name)
以上的命名目的為安全性及可維護性
執行結果:
Ref:
Eclipse 安裝tomcat:
Eclipse下使用tomcat的位置當作deploy位置
Eclipse下tomcat server location無法更改:https://stackoverflow.com/questions/4919846/why-tomcat-server-location-property-is-greyed-in-eclipse
沒有留言:
張貼留言