Day3_J2EE(Java 2 Enterprise Edition)_Servlet API_練習匯入war檔案_使用Java Servlet生成網頁


Web Application和網頁之差別!!!

html靜態網頁多數用來做資料靜態呈現,給人瀏覽。
主要處裡資訊顯示的樣式!!!!

Web應用系統則是一個放在Server上運行的應用程式
小可小到只做檔案文件中找出特定key word ,大則可到電子商務網站。
像目前接觸的Java Servlets 就是一種 web application
主要處裡



在最早一開始的動態網頁內容互動採用的是一種名為
「共同閘道介面(CGI)」的技術
主要協助網頁與WWW server做互動,達到跟使用者有所來往。
CGI主要會針對每一項來自client端的請求去產生一個process (處裡程序)
也因此會造成大量資源的耗損。


也因應資訊技術進步
產生像是如下三種動態網頁程式語言
ASP(Active Server Pages) : 採用IIS 的特定Server (只可搭配微軟產品) 易學、人性化
PHP(Personal Home Page) : for free、可跨平台
JSP(Java Server Pages) : 主打 Write Once , Run Anywhere,可跨平台!!


共同性:都是可穿插一些特定程式語言至html當中使其具備互動性(提交表單、....等等)




link:https://corejava25hours.com/category/advance-java-in-25-hours/


Servlet API

基本主要兩部分
javax.servlet
javax.servlet.http

Java Servlet 程式主要是實作Servlet介面or繼承HttpServlet類的Class file
https://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpServlet.html
有很多常用的開發手段是去繼承HttpServlet類來建出Java Servlet程式
覆寫 父類中的  doGet() 或 doPost() 等等  method去實作!!!!!




練習匯入war檔案


網站備存檔案.war (web application archive)
一般會用在將系統佈署至其他地方的時候,
就會需要先把系統集中成一個稱為web archive(.war)的檔案中













當你中途修改將 hello 改成 hi的時候
此時若重新執行就會要求你重啟Server



程式部分

第一範例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package servlet_examples;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloGet extends HttpServlet {
 private static final long serialVersionUID = 1L;

 public void doGet(HttpServletRequest req, HttpServletResponse res)
                        throws ServletException, IOException {

  res.setContentType("text/html; charset=UTF-8");
  PrintWriter out = res.getWriter();

  String name = req.getParameter("name");
  out.println("<HTML>");
  out.println("<HEAD><TITLE>Hello, " + name + "</TITLE></HEAD>");
  out.println("<BODY>");
  out.println("Hi, 你好: " + name);
  out.println("</BODY></HTML>");
 }

}







使用Java Servlet生成網頁

從無到有建立並寫出此段程式

Step1. 建立新的Dynamic Web Project




Step2.新增一個Servlet

對Package以及Class Name 做命名

 選定要實作的方法
 生成出來Code



執行出來所呈現的結果
程式碼
上方import部分收闔一開會發現自動添加進來的三個和javax.servlet相關之類別
javax.servlet.http.HttpServlet
javax.servlet.http.HttpServletRequest
javax.servlet.http.HttpServletResponse


其他還有Servlet之例外  或是  annotation等等

第二範例


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package KYChou;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HelloGet
 */
@WebServlet("/HelloGet")
public class HelloGet extends HttpServlet {
 private static final long serialVersionUID = 1L;

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) 
   throws ServletException, IOException {
  // TODO Auto-generated method stub
  response.getWriter().append("Served at: ").append(request.getContextPath());
 }

}


那先來解釋此短篇幅程式
response 這部分就是client發出請求給 server端後的回傳
在HTTP 表頭
摘自: https://nkongkimo.wordpress.com/2010/04/28/http-header入門/

如上面這張圖的結構我們可以清楚看到
Client端發出request的時候主要只會有兩個部分
Request Line 以及 Request Header
Server端回傳則有
Status Line
Response Header
Response Content


我們可以去藉由 getWriter() 獲取 PrintWriter Class進行打印
字串串接最後一部分的
request.getContextPath()

則是幫助我們獲取Server站台的根目錄



最一開始的 第一範例中

setContentType("text/html; charset=UTF-8")

則是去設定回傳時的  Response Header 部分
設置為UTF-8 (對 Unicode 的可變長度字元編碼)

getParameter() 傳回的是 String 型態
對請求參數名稱(Key)來取得對應的值(Value)
當前請求並未指定特定參數名稱 因此 為NULL



參考網站:

What are Java Servlet different HTTP Request Handling Methods
http://javawebaction.blogspot.tw/2011/07/what-are-different-request-handling.html


Java Server-Side Programming_Java Servlets
https://www.ntu.edu.sg/home/ehchua/programming/java/JavaServlets.html


Java:25 Hour
https://corejava25hours.com/category/advance-java-in-25-hours/










留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題