java web--servlet(2)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?servlet(2)
? ? ? ? ? ? ? ? ? ? 1. 使用 JavaEE 版的 Eclipse 開發動態的 WEB 工程(JavaWEB 項目)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?1). 把開發選項切換到 JavaEE
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?2). 可以在 Window -> Show View 中找到 Package Explorer, 并把其拖拽到開發區的左邊
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?3). 在 Servers 面板中新建 Tomcat 服務器. 一定要關聯到 Tomcat 安裝的根目錄
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?4). 新建一個 Dynamic Web Project. 其中 Target Runtime 需選擇 Tomcat6.0
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?5). 開發 Java WEB 應用
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?6). 可以通過 run on server 來運行 WEB 項目.
? ? ? ? ? ? ? ? ? ? ?2. Servlet 的 HelloWorld
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 1). 創建一個 Servlet 接口的實現類.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?public class HelloServlet implements Servlet
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??2). 在 web.xml 文件中配置和映射這個 Servlet
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <!-- 配置和映射 Servlet -->
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <servlet>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<!-- Servlet 注冊的名字 -->
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <servlet-name>helloServlet</servlet-name>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<!-- Servlet 的全類名 -->
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <servlet-class>com.atguigu.javaweb.HelloServlet</servlet-class>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?</servlet>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<servlet-mapping>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <!-- 需要和某一個 servlet 節點的 serlvet-name 子節點的文本節點一致 -->
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <servlet-name>helloServlet</servlet-name>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <!-- 映射具體的訪問路徑: / 代表當前 WEB 應用的根目錄. -->
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <url-pattern>/hello</url-pattern>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?</servlet-mapping>
? ? ? ? ? ? ? ? ? ?3. Servlet 容器: 運行 Servlet、JSP、Filter 等的軟件環境.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 1). 可以來創建 Servlet, 并調用 Servlet 的相關生命周期方法.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 2). JSP, Filter, Listener, Tag ...
? ? ? ? ? ? ? ? ? ? ? ??4. Servlet 生命周期的方法: 以下方法都是由 Serlvet 容器負責調用.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?1). 構造器: 只被調用一次. 只有第一次請求 Servlet 時, 創建 Servlet 的實例. 調用構造器.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 這說明 Serlvet 的單實例的!
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?2). init 方法: 只被調用一次. 在創建好實例后立即被調用. 用于初始化當前 Servlet.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?3). service: 被多次調用. 每次請求都會調用 service 方法. 實際用于響應請求的.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?4). destroy: 只被調用一次. 在當前 Servlet 所在的 WEB 應用被卸載前調用. 用于釋放當前 Servlet 所占用的資源.
? ? ? ? ? ? ? ? ? ?5 load-on-startup 參數:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?1). 配置在 servlet 節點中:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<servlet>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<!-- Servlet 注冊的名字 -->
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<servlet-name>secondServlet</servlet-name>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<!-- Servlet 的全類名 -->
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<servlet-class>com.atguigu.javaweb.SecondServlet</servlet-class>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<!-- 可以指定 Servlet 被創建的時機 -->
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <load-on-startup>2</load-on-startup>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?</servlet>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ???2). load-on-startup: 可以指定 Serlvet 被創建的時機. 若為負數, 則在第一次請求時被創建.若為 0 或正數, 則在當前 WEB 應用被
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Serlvet 容器加載時創建實例, 且數組越小越早被創建.
? ? ? ? ? ? ? ? ? ? 6. 關于 serlvet-mapping:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?1). 同一個Servlet可以被映射到多個URL上,即多個 <servlet-mapping> 元素的<servlet-name>子元素的設置值可以是同一個
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Servlet的注冊名。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?2). 在Servlet映射到的URL中也可以使用 * 通配符,但是只能有兩種固定的格式:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 一種格式是“*.擴展名”, ? ? ? ?另一種格式是以正斜杠(/)開頭并以“/*”結尾。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <servlet-mapping>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <servlet-name>secondServlet</servlet-name>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <url-pattern>/*</url-pattern>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </servlet-mapping>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? OR
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <servlet-mapping>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<servlet-name>secondServlet</servlet-name>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <url-pattern>*.do</url-pattern>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?</servlet-mapping>
? ? ? ? ? ? ? ? ? ? ? ? ? ? 注意: 以下的既帶 / 又帶擴展名的不合法.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<servlet-mapping>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<servlet-name>secondServlet</servlet-name>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <url-pattern>/*.action</url-pattern>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?</servlet-mapping>
? ? ? ? ? ? ? ? ? ? ? 7. ServletConfig: 封裝了 Serlvet 的配置信息, 并且可以獲取 ServletContext 對象
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 1). 配置 Serlvet 的初始化參數
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<servlet>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <servlet-name>helloServlet</servlet-name>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <servlet-class>com.atguigu.javaweb.HelloServlet</servlet-class>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<!-- 配置 Serlvet 的初始化參數。 且節點必須在 load-on-startup 節點的前面 -->
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <init-param>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<!-- 參數名 -->
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<param-name>user</param-name>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<!-- 參數值 -->
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<param-value>root</param-value>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </init-param>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<init-param>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <param-name>password</param-name>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <param-value>1230</param-value>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?</init-param>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <load-on-startup>-1</load-on-startup>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?</servlet>
? ? ? ? ? ? ? ? ? ? ? ? ? ?2). 獲取初始化參數:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?> getInitParameter(String name): ? ?獲取指定參數名的初始化參數
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?> getInitParameterNames(): ? ? ? ? ? ?獲取參數名組成的 Enumeration 對象.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?String user = servletConfig.getInitParameter("user");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("user: " + user);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Enumeration<String> names = servletConfig.getInitParameterNames();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?while(names.hasMoreElements()){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String name = names.nextElement();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String value = servletConfig.getInitParameter(name);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("^^" + name + ": " + value);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? ? ? ? 3). 獲取 Serlvet 的配置名稱(了解)
? ? ? ? ? ? ? ? ? ? 8. ServletContext
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?1). 可以由 SerlvetConfig 獲取: ? ? ??ServletContext servletContext = servletConfig.getServletContext();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?2). 該對象代表當前 WEB 應用: 可以認為 SerlvetContext 是當前 WEB 應用的一個大管家. 可以從中獲取到當前 WEB 應用的各個方面的信息.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?①. 獲取當前 WEB 應用的初始化參數
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 設置初始化參數: 可以為所有的 Servlet 所獲取, 而 Servlet 的初始化參數只用那個 Serlvet 可以獲取.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<!-- 配置當前 WEB 應用的初始化參數 -->
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<context-param>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<param-name>driver</param-name>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <param-value>com.mysql.jdbc.Driver</param-value>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </context-param>
? ? ? ? ? ? ? ? ? ? ? ? ? ?方法:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? getInitParameter
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? getInitParameterNames
? ? ? ? ? ? ? ? ? ? ? ? ? 代碼:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?ServletContext servletContext = servletConfig.getServletContext();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?String driver = servletContext.getInitParameter("driver");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?System.out.println("driver:" + driver);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Enumeration<String> names2 = servletContext.getInitParameterNames();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?while(names2.hasMoreElements()){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?String name = names2.nextElement();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println("-->" + name);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ?? 3). ?取當前 WEB 應用的某一個文件在服務器上的絕對路徑, 而不是部署前的路徑
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? getRealPath(String path);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?代碼:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?String realPath = servletContext.getRealPath("/note.txt");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?System.out.println(realPath);
? ? ? ? ? ? ? ? ? ? ? ? ? ?4). 獲取當前 WEB 應用的名稱:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? getContextPath()
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 代碼:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String contextPath = servletContext.getContextPath();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.out.println(contextPath);
? ? ? ? ? ? ? ? ? ? ? ? ? 5). 獲取當前 WEB 應用的某一個文件對應的輸入流.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? getResourceAsStream(String path): path 的 / 為當前 WEB 應用的根目錄.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 代碼:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? InputStream is2 = servletContext.getResourceAsStream("/WEB-INF/classes/jdbc.properties");
? ? ? ? ? ? ? ? ? ? ? ? ? ?6). 和 attribute 相關的幾個方法:
? ? ? ? ? ? ? ? 9. GET 請求和 POST 請求:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 1). 使用GET方式傳遞參數:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ①. 在瀏覽器地址欄中輸入某個URL地址或單擊網頁上的一個超鏈接時,瀏覽器發出的HTTP請求消息的請求方式為GET。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ②. 如果網頁中的<form>表單元素的 method 屬性被設置為了“GET”,瀏覽器提交這個FORM表單時生成的HTTP請求消息的請求方式也為GET。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ③. 使用GET請求方式給WEB服務器傳遞參數的格式:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?http://www.atguigu.com/counter.jsp?name=lc&password=123
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?④. 使用GET方式傳送的數據量一般限制在 1KB 以下。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??2). 使用 POST 方式傳遞參數:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?①. POST 請求方式主要用于向 WEB 服務器端程序提交 FORM 表單中的數據: form 表單的 method 置為 POST
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?②. POST 方式將各個表單字段元素及其數據作為 HTTP 消息的實體內容發送給 WEB 服務器,傳送的數據量要比使用GET方式傳送的數據量大得多。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?POST /counter.jsp HTTP/1.1
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?referer: http://localhost:8080/Register.html
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?content-type: application/x-www-form-urlencoded
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?host: localhost:8080
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?content-length: 43
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? name=zhangsan&password=123 --請求體中傳遞參數.
? ? ? ? ? ? ? ? ? 10. 如何在 Serlvet 中獲取請求信息:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 1). Servlet 的 service() 方法用于應答請求: 因為每次請求都會調用 service() 方法
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void service(ServletRequest request, ServletResponse response)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? throws ServletException, IOException
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?ServletRequest: 封裝了請求信息. 可以從中獲取到任何的請求信息.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?ServletResponse: 封裝了響應信息, 如果想給用戶什么響應, 具體可以使用該接口的方法實現.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 這兩個接口的實現類都是服務器給予實現的, 并在服務器調用 service 方法時傳入.
? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? 2). ServletRequest: 封裝了請求信息. 可以從中獲取到任何的請求信息.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ①. 獲取請求參數:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?> String getParameter(String name): 根據請求參數的名字, 返回參數值.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 若請求參數有多個值(例如 checkbox), 該方法只能獲取到第一個提交的值.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?> String[] getParameterValues(String name): 根據請求參數的名字, 返回請求參數對應的字符串數組.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? > Enumeration getParameterNames(): 返回參數名對應的 Enumeration 對象,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 類似于 ServletConfig(或 ServletContext) 的 getInitParameterNames() 方法.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?> Map getParameterMap(): 返回請求參數的鍵值對: key: 參數名, value: 參數值, String 數組類型.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?②. 獲取請求的 URI:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? HttpServletRequest httpServletRequest = (HttpServletRequest) request;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String requestURI = httpServletRequest.getRequestURI();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?System.out.println(requestURI); // /day_29/loginServlet
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ③. 獲取請求方式:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?String method = httpServletRequest.getMethod();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?System.out.println(method); //GET
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ④. 若是一個 GET 請求, 獲取請求參數對應的那個字符串, 即 ? 后的那個字符串.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?String queryString = httpServletRequest.getQueryString();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?System.out.println(queryString); //user=atguigu&password=123456&interesting=game&interesting=party&interesting=shopping
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?⑤. 獲取請求的 Serlvet 的映射路徑
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String servletPath = httpServletRequest.getServletPath();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?System.out.println(servletPath); // /loginServlet
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ⑥. 和 attribute 相關的幾個方法:
? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ?3). HttpServletRequest: 是 SerlvetRequest 的子接口. 針對于 HTTP 請求所定義. 里邊包含了大量獲取 HTTP 請求相關的方法.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?4). ServletResponse: 封裝了響應信息, 如果想給用戶什么響應, 具體可以使用該接口的方法實現.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ①. *getWriter(): 返回 PrintWriter 對象. 調用該對象的 print() 方法, 將把 print() 中的參數直接打印
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?到客戶的瀏覽器上.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ②. 設置響應的內容類型: response.setContentType("application/msword");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ③. void sendRedirect(String location): 請求的重定向. (此方法為 HttpServletResponse 中定義.)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?注意:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?在 web.xml 文件中設置兩個 WEB 應用的初始化參數, user, password.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 定義一個 login.html, 里邊定義兩個請求字段: user, password. 發送請求到 loginServlet
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 在創建一個 LoginServlet, 在其中獲取請求的 user, password. 比對其和 web.xml 文件中定義的請求參數是否一致
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 若一致, 響應 Hello:xxx, 若不一致, 響應 Sorry: xxx xxx 為 user.
? ? ? 11.代碼區
? ? ? ? ? ? ? ? ??
?
package com.atguigu.javaweb;import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.Properties;import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse;public class HelloServlet implements Servlet{public HelloServlet() {System.out.println("HelloServlet's constructor");}@Overridepublic void destroy() {System.out.println(" HelloServlet destroy");}@Overridepublic ServletConfig getServletConfig() {System.out.println("getServletConfig");return null;}@Overridepublic String getServletInfo() {System.out.println("getServletInfo");return null;}@Overridepublic void init(ServletConfig servletConfig) throws ServletException {//load-on-startup 在web.xml 配置的是3 所以第三啟動initSystem.out.println("Holloservlet init");String user = servletConfig.getInitParameter("user");System.out.println("user: " + user);//user: rootEnumeration<String> names = servletConfig.getInitParameterNames();while(names.hasMoreElements()){String name = names.nextElement();String value = servletConfig.getInitParameter(name);System.out.println("^^" + name + ": " + value);}/* ^^password: 1230^^user: root*/String serlvetName = servletConfig.getServletName();System.out.println(serlvetName); //helloServlet//獲取 ServletContext 對象ServletContext servletContext = servletConfig.getServletContext();String driver = servletContext.getInitParameter("driver");System.out.println("driver:" + driver);//driver:com.mysql.jdbc.Driver Enumeration<String> names2 = servletContext.getInitParameterNames();while(names2.hasMoreElements()){String name = names2.nextElement();System.out.println("-->" + name); }/*-->driver-->jdbcUrl*/String realPath = servletContext.getRealPath("/note.txt");//不是: E:\Java\Source\atguigu\java-1\day_29\WebContent\note.txt System.out.println(realPath);//真實路徑:D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\day_29\note.txt String contextPath = servletContext.getContextPath();System.out.println(contextPath); ///day_29Properties pros = new Properties();try {ClassLoader classLoader = getClass().getClassLoader();InputStream is = classLoader.getResourceAsStream("jdbc.properties");System.out.println("1. " + is);pros.load(is);System.out.println(pros.getProperty("name")); } catch (Exception e) {e.printStackTrace();}pros = new Properties();try {InputStream is2 = servletContext.getResourceAsStream("/WEB-INF/classes/jdbc.properties");System.out.println("2. " + is2);pros.load(is2);System.out.println(pros.getProperty("name")); } catch (Exception e) {e.printStackTrace();}String picPath = servletContext.getRealPath("/WEB-INF/lib");System.out.println("picPath"+picPath);//picPathD:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\day_29\WEB-INF\lib }@Overridepublic void service(ServletRequest arg0, ServletResponse arg1)throws ServletException, IOException {System.out.println("service");}} HelloServlet package com.atguigu.javaweb;import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; import java.util.Enumeration; import java.util.Map;import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest;public class LoginServlet implements Servlet{@Overridepublic void destroy() {// TODO Auto-generated method stubSystem.out.println("loginservlet destory");}@Overridepublic ServletConfig getServletConfig() {// TODO Auto-generated method stubreturn null;}@Overridepublic String getServletInfo() {// TODO Auto-generated method stubreturn null;}@Overridepublic void init(ServletConfig arg0) throws ServletException {// TODO Auto-generated method stubSystem.out.println("loginservlet init");}@Overridepublic void service(ServletRequest request, ServletResponse response)throws ServletException, IOException {System.out.println("請求來了...");//org.apache.catalina.connector.RequestFacade@7a2308c3 System.out.println(request); String user = request.getParameter("user");String password = request.getParameter("password");//zhouwuji, 123456System.out.println(user + ", " + password); //gameString interesting = request.getParameter("interesting");System.out.println(interesting); /*-->game-->party-->shopping*/String [] interestings = request.getParameterValues("interesting");for(String interest: interestings){System.out.println("-->" + interest);}/* ^^user: zhouwuji^^password: 123456^^interesting: game*/Enumeration<String> names = request.getParameterNames();while(names.hasMoreElements()){String name = names.nextElement();String val = request.getParameter(name);System.out.println("^^" + name + ": " + val);}/* **user:[zhouwuji]**password:[123456]**interesting:[game, party, shopping]*/Map<String, String[]> map = request.getParameterMap();for(Map.Entry<String, String[]> entry: map.entrySet()){System.out.println("**" + entry.getKey() + ":" + Arrays.asList(entry.getValue())); }HttpServletRequest httpServletRequest = (HttpServletRequest) request;String requestURI = httpServletRequest.getRequestURI();///day_29/login System.out.println(requestURI);//POSTString method = httpServletRequest.getMethod();System.out.println(method); // nullString queryString = httpServletRequest.getQueryString();System.out.println(queryString); // /loginString servletPath = httpServletRequest.getServletPath();System.out.println(servletPath); //告訴瀏覽器這是一個下載文本response.setContentType("application/msword");PrintWriter out = response.getWriter();out.println("helloworld...");}} LoginServlet package com.atguigu.javaweb;public class Person {public String getInfo(){return "helloworld";}} Person package com.atguigu.javaweb;import java.io.IOException;import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public class SecondServlet implements Servlet{@Overridepublic void destroy() {// TODO Auto-generated method stubSystem.out.println("SecondServlet destroy ");}@Overridepublic ServletConfig getServletConfig() {// TODO Auto-generated method stubreturn null;}@Overridepublic String getServletInfo() {// TODO Auto-generated method stubreturn null;}@Overridepublic void init(ServletConfig arg0) throws ServletException {System.out.println("second Servlet init");}@Overridepublic void service(ServletRequest arg0, ServletResponse arg1)throws ServletException, IOException {// TODO Auto-generated method stubSystem.out.println("second service");String person=new Person().getInfo();HttpServletRequest request=(HttpServletRequest)arg0;HttpServletResponse response=(HttpServletResponse)arg1;request.setAttribute("person", person);request.getRequestDispatcher("/hello.jsp").forward(request, response);;} } SecondServlet name=atguigu jdbc.properties <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><!-- 配置當前 WEB 應用的初始化參數 --><context-param><param-name>driver</param-name><param-value>com.mysql.jdbc.Driver</param-value></context-param><context-param><param-name>jdbcUrl</param-name><param-value>jdbc:mysql:///atguigu</param-value></context-param><!-- 配置和映射 Servlet --><servlet><servlet-name>loginServlet</servlet-name><servlet-class> com.atguigu.javaweb.LoginServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>loginServlet</servlet-name><url-pattern>/login</url-pattern> </servlet-mapping><servlet><!-- Servlet 注冊的名字 --><servlet-name>helloServlet</servlet-name><!-- Servlet 的全類名 --><servlet-class>com.atguigu.javaweb.HelloServlet</servlet-class><!-- 配置 Serlvet 的初始化參數 --><init-param><!-- 參數名 --><param-name>user</param-name><!-- 參數值 --><param-value>root</param-value></init-param><init-param><param-name>password</param-name><param-value>1230</param-value></init-param><!-- 可以指定 Servlet 被創建的時機 --><load-on-startup>3</load-on-startup></servlet><servlet-mapping><!-- 需要和某一個 servlet 節點的 serlvet-name 子節點的文本節點一致 --><servlet-name>helloServlet</servlet-name><!-- 映射具體的訪問路徑: / 代表當前 WEB 應用的根目錄. --><url-pattern>/hello</url-pattern></servlet-mapping><servlet-mapping><!-- 需要和某一個 servlet 節點的 serlvet-name 子節點的文本節點一致 --><servlet-name>helloServlet</servlet-name><!-- 映射具體的訪問路徑: / 代表當前 WEB 應用的根目錄. --><url-pattern>/hello2</url-pattern></servlet-mapping><servlet><!-- Servlet 注冊的名字 --><servlet-name>secondServlet</servlet-name><!-- Servlet 的全類名 --><servlet-class>com.atguigu.javaweb.SecondServlet</servlet-class><!-- 可以指定 Servlet 被創建的時機 里面是數組 --><load-on-startup>2</load-on-startup></servlet><servlet-mapping><servlet-name>secondServlet</servlet-name><url-pattern>/second</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list> </web-app> web.xml <%@page import="com.atguigu.javaweb.Person"%> <%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body><%=request.getAttribute("person") %>wer </body> </html> hello.jsp <%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body>this is a first page </body> </html> index.jsp <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body><form action="login" method="post">user: <input type="text" name="user"/>password: <input type="password" name="password"/><br><br>interesting:<input type="checkbox" name="interesting" value="reading"/>Reading<input type="checkbox" name="interesting" value="game"/>Game<input type="checkbox" name="interesting" value="party"/>Party<input type="checkbox" name="interesting" value="shopping"/>Shopping<input type="checkbox" name="interesting" value="sport"/>Sport<input type="checkbox" name="interesting" value="tv"/>TV<input type="submit" value="Submit"/></form></body> </html> login.html?
轉載于:https://www.cnblogs.com/ou-pc/p/8150099.html
總結
以上是生活随笔為你收集整理的java web--servlet(2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 曼哈顿距离(坐标投影距离之和)d(i,j
- 下一篇: 学习进度条(第六周)