【JAVA WEB】學習筆記——Servlet入門

WEB服務器入門

常用的WEB服務器

  • IIS
  • Apache
  • Tomcat

常用的動態網頁技術

  • CGI(Common GateWay Interface)
  • API常用的有(NSAPI,ISAPI)
  • ASP(Active Server Page)->VBScript,ASP.NET->C#
  • PHP(Personal Home Page)
  • JSP/Servlet(Java Server Page)

服務器分類

  • WEB服務器 Apeche
  • 應用服務器 IIS,websphere(EJB等程序)
  • Servlet 容器 Tomcat

Tomcat介紹

  • Tomcat啓動與停止
    Tomcat啓動需要使用JAVA_HOME或JRE_HOME環境變量
  • Tomcat的目錄結構
    • bin:存放各種平臺下啓動和關閉Tomcat的腳本文件
      startup.bat:是Windows下啓動Tomcat的文件
      shutdown.bat:是關閉Tomcat的文件
  • lib:存放tomcat服務器和所有web應用都能訪問的JAR
  • work:web應用工作目錄,JSP編譯後的class,緩存等
  • temp:臨時文件夾,tomcat運行時候存放臨時文件
  • log:存放tomcat的日誌文件
  • webapps:web應用的發佈目錄,把Java開發的web應用(或war文件)放入這個目錄,就可以通過tomcat訪問
  • conf:tomcat的配置文件,最重要的是server.xml

Java EE 規範

Web Application Name
+WEB-INF
|–web.xml (該web App的配置文件)
|–lib (該web App用的jar類庫)
|–classes (存放編譯好的servlet)
+META-INF (該Web App的上下文信息,符合Java EE標準)

Servlet

servlet簡介

  • Servlet是向服務器小應用程序
  • 用來完成B/S架構下,客戶端請求的相應處理
  • 平臺獨立,性能優良,能以多線程方式運行
  • Servlet API爲Servlet提供了統一的編程接口
  • Servlet要在容器中運行
  • 常見的Servlet容器:
    • Tomcat
    • Jetty/Resin

HelloWorldDemo

在HelloWorldServlet項目創建如下Servlet,後放入Tomcat開啓服務器

public class HelloWorldServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub

        System.out.println("HelloWorldServlet doGet() ");
        PrintWriter out = resp.getWriter();
        out.println("Hello Servlet");
        out.flush();
        out.close();

    }
}

在瀏覽器打開
http://localhost:8080/Servlet_HelloWorld/servlet/HelloWorldServlet
顯示:Hello Servlet
服務器端:HelloWorldServlet doGet()

收穫:
1. 所有資源是放在服務器上,由服務器管理
2. web.xml配置Servlet的映射

<servlet>
<servlet-name>Servlet名</sevlet-name>
<servlet-class>Servlet類名</servlet-class>
<sevlet>
<servlet-mapping>
<servlet-name>Servlet名</servlet-name>
<url-pattern>URL<url-pattern>
</servlet-mapping>

3. 讓Tomcat自動重新加載:在context.xml中<Context reloadable="true">

Servlet編程接口

  • GenericServlet是所有Servlet的鼻祖
  • 用於HTTP的Servlet編程都通過繼承javax.servlet.http.HttpServlet實現
  • 請求處理方法:
    • doGet() 響應GET請求,常用
    • doPost() 響應POST請求,常用
    • doPut() 用於HTTP1.1
    • doDelete() 用於HTTP1.1
    • doHead()
    • doOptions()
    • doTrace()
  • Servlet實例個數:在分佈式系統的情況下,通常一個Servlet在服務器中只有一個實例

Servlet生命週期

  • 生命全過程
    • 加載ClassLoader
    • 實例化
    • 初始化init(Servlet config)
    • 處理請求 service() doGet() doPost()
    • 退出服務 destroy()

Servlet接受參數實例:

index.html

<html>
  <head>
    <title>index.html</title>

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>

  <body>
    <h1>GET:提交參數</h1>
    <form action="getParameter.do" method="get">
        用戶名:<input name="username" type="text"/><br/> 
        <input type="submit" value="提交"/>
    </form>
    <hr/>
    <h1>POST:提交參數</h1>
    <form action="getParameter.do" method="POST">
        用戶名:<input name="username" type="text"/><br/> 
        <input type="submit" value="提交"/>
    </form>
  </body>
</html>

GetParemeters類

public class GetParemeters extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        System.out.println("username = "+username);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        this.doGet(req, resp);
    }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>
  <servlet>
    <servlet-name>GetParemeters</servlet-name>
    <servlet-class>servlet.GetParemeters</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>GetParemeters</servlet-name>
    <url-pattern>/getParameter.do</url-pattern>
  </servlet-mapping>    
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章