1、Servlet簡介
css
Servlet是sun公司提供的一門用於開發動態web資源的技術。html
Sun公司在其API中提供了一個Servlet接口,用戶若是相開發一個動態的web資源(即開發一個Java程序向瀏覽器輸出數據),須要完成如下2個步驟:java
編寫一個Java類,實現Servlet接口。web
把開發好的Java類部署到web服務器中。
編程
2、示例瀏覽器
HelloServlet.java緩存
package cn.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @SuppressWarnings("serial") public class HelloServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println("Hello Servlet!"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
web.xmltomcat
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>day09</display-name> <!-- 配置Servlet信息 --> <servlet> <!-- 配置Servlet名稱,名稱必須惟一 --> <servlet-name>HelloServlet</servlet-name> <!-- 配置Servlet的徹底路徑(包名+類名) --> <servlet-class>cn.servlet.HelloServlet</servlet-class> </servlet> <!-- 配置Servlet的一你射(訪問路徑) --> <servlet-mapping> <!-- 配置Servlet名稱,和上面的名稱必須相同 --> <servlet-name>HelloServlet</servlet-name> <!-- 配置訪問路徑 --> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>
在瀏覽器中輸入:http://localhost:8080/day09/hello 服務器
3、Servlet的運行過程app
Servlet程序是由web服務器調用,web服務器接收客戶端的Servlet請求後:
1)web服務器首先檢查是否已經裝載並建立了該Servlet的實例對象。若是是,則直接執行第4)步,不然,執行第2)步。
2)裝載並建立該Servlet的一個實例對象。
3)調用Servlet實例對象的init()方法。
4)建立一個用於封裝HTTP請求消息的HttpServletRequest對象和一個表明HTTP響應消息的HttpServletResponse對象,而後調用Servlet的service()方法並將請求和響應對象做爲參數傳遞進去。
5)WEB應用程序被中止或從新啓動以前,Servlet引擎將卸載Servlet,並在卸載以前調用Servlet的destory()方法。
4、Servlet接口定義了Servlet的生命週期
init()方法:服務器調用此方法初始化Servlet。
service()方法:初始化完畢,服務器調用該方法響應客戶的請求。
destory()方法:服務器調用該方法消滅Servlet對象。
其中,init()方法只在Servlet第一次被請求加載的時候被調用一次,當有客戶再請求Servlet服務的時候,Web服務器將啓動一個新的線程,在該線程中,調用service()方法響應客戶的請求。
5、Servlet的自動加載
默認狀況下,Servlet是在用戶第一次訪問的時候才建立的。
若是在<servlet>元素中配置了一個<load-on-startup>的元素,那麼web應用程序在啓動的時候,就會裝載並建立Servlet的實例對象以及調用Servlet實例對象的init()方法。
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>day09</display-name> <!-- 配置Servlet信息 --> <servlet> <!-- 配置Servlet名稱,名稱必須惟一 --> <servlet-name>HelloServlet</servlet-name> <!-- 配置Servlet的徹底路徑(包名+類名) --> <servlet-class>cn.servlet.HelloServlet</servlet-class> <!-- 服務器啓動並加載Servlet --> <load-on-startup>1</load-on-startup> </servlet> <!-- 配置Servlet的一你射(訪問路徑) --> <servlet-mapping> <!-- 配置Servlet名稱,和上面的名稱必須相同 --> <servlet-name>HelloServlet</servlet-name> <!-- 配置訪問路徑 --> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>
<load-on-startup>中的值越小,優先級越高。
6、配置Servlet的虛擬路徑(訪問路徑)
徹底路徑匹配
以/開頭 /demo5 /servlet/demo5
訪問:http://localhost:8080/day09/demo5
目錄匹配
以/開頭 /*
訪問:http://localhost:8080/day09/demo/demo5
擴展名匹配
不能以/開頭 *.do *.action
訪問:http://localhost:8080/day09/aa/bbb/a.action
優先級:徹底路徑匹配>目錄匹配>擴展名匹配
7、web開發中的路徑問題
1.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h2>1.html</h2> </body> </html>
2.html
<!DOCTYPE html> <html> <head> <title>2.html</title> <meta name="keywords" content="keyword1,keyword2,keyword3"> <meta name="description" content="this is my page"> <meta name="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <h2>2.html</h2> </body> </html>
HelloServlet
package cn.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @SuppressWarnings("serial") public class HelloServlet extends HttpServlet { @Override public void init() throws ServletException { System.out.println("init方法"); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); response.getWriter().println("哇 這是Servlet"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
對應的web.xml文件
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>day09</display-name> <!-- 配置Servlet信息 --> <servlet> <!-- 配置Servlet名稱,名稱必須惟一 --> <servlet-name>HelloServlet</servlet-name> <!-- 配置Servlet的徹底路徑(包名+類名) --> <servlet-class>cn.servlet.HelloServlet</servlet-class> <!-- 服務器啓動並加載Servlet --> <load-on-startup>1</load-on-startup> </servlet> <!-- 配置Servlet的一你射(訪問路徑) --> <servlet-mapping> <!-- 配置Servlet名稱,和上面的名稱必須相同 --> <servlet-name>HelloServlet</servlet-name> <!-- 配置訪問路徑 --> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>
目錄結構圖
相對路徑:
一個文件相對於另外一個文件的位置的關係
不能以/開頭 寫法: ./hello hello
訪問1.html: http://localhost:8080/day09/1.html
訪問HelloServlet: http://localhost:8080/day09/hello
訪問2.html http://localhost:8080/day09/html/2.html
因此,在1.html中訪問HelloServlet
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h2>1.html</h2> <a href="./hello">HelloServlet</a> <a href="hello">HelloServlet</a> </body> </html>
同理,在2.html中訪問HelloServlet
<!DOCTYPE html> <html> <head> <title>2.html</title> <meta name="keywords" content="keyword1,keyword2,keyword3"> <meta name="description" content="this is my page"> <meta name="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <h2>2.html</h2> <a href="../hello">Servlet</a> <a href=".././hello">Servlet</a> </body> </html>
絕對路徑:以/開頭 而後截取項目名後面的
訪問Servlet:http://localhost:8080/day09/hello
在1.html使用絕對路徑訪問Servlet:http://localhost:8080/day09/hello
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h2>相對路徑</h2> <a href="./hello">HelloServlet</a> <a href="hello">HelloServlet</a> <h2>絕對路徑</h2> <a href="http://localhost:8080/day09/hello">HelloServlet</a> <a href="/day09/hello">HelloServlet</a> </body> </html>
客戶端絕對路徑:/day09/hello 須要寫項目名
服務器端絕對路徑:/hello 不須要寫項目名
客戶端絕對路徑和服務器端絕對路徑的理解:設想這樣一個場景,小A和小B是1班級的同窗,小C是2班級的,有一天小C要找小A,那麼小C先要去1班,而後再找小A;可是若是小B要找小A,此時就不要去1班級了,由於小A和小B原本就是1班級的,因此只須要直接找小A就能夠了。
客戶端關於路徑問題的編程結論:
*.html,*.jsp內部使用絕對路徑
*.css內部使用相對路徑
*.js中使用絕對路徑
總之:推薦使用絕對路徑
8、ServletConfig對象和配置文件相關
public String getServletName() 獲取配置文件中的servlet的名稱 public String getInitPaarameter(String name) 獲取初始化參數 public Enumeration getInitParameterNames() 獲取初始化參數的名稱s
如何獲取ServletConfig對象,方法一:是在init(ServletConfig config)方法中
package cn.servlet; import java.io.IOException; import java.util.Enumeration; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @SuppressWarnings("serial") public class HelloServlet extends HttpServlet { @Override public void init(ServletConfig config) throws ServletException { System.out.println("Servlet的名稱:"+config.getServletName()); Enumeration<String> initParameterNames = config.getInitParameterNames(); while(initParameterNames.hasMoreElements()){ String parameterName = initParameterNames.nextElement(); String parameterValue = config.getInitParameter(parameterName); System.out.println("初始化參數名稱:"+parameterName+",初始化參數值"+parameterValue); } } @Override public void init() throws ServletException { System.out.println("init方法"); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); response.getWriter().println("哇 這是Servlet"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
web.xml
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>day09</display-name> <!-- 配置Servlet信息 --> <servlet> <!-- 配置Servlet名稱,名稱必須惟一 --> <servlet-name>HelloServlet</servlet-name> <!-- 配置Servlet的徹底路徑(包名+類名) --> <servlet-class>cn.servlet.HelloServlet</servlet-class> <!-- 配置初始化參數 --> <init-param> <param-name>你是誰</param-name> <param-value>我不知道我是誰</param-value> </init-param> <!-- 服務器啓動並加載Servlet --> <load-on-startup>1</load-on-startup> </servlet> <!-- 配置Servlet的一你射(訪問路徑) --> <servlet-mapping> <!-- 配置Servlet名稱,和上面的名稱必須相同 --> <servlet-name>HelloServlet</servlet-name> <!-- 配置訪問路徑 --> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>
另外一種方法時:Servlet接口中就有獲取ServletConfig對象的方法
public abstract ServletConfig getServletConfig();
package cn.servlet; import java.io.IOException; import java.util.Enumeration; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @SuppressWarnings("serial") public class HelloServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); response.getWriter().println("哇 這是Servlet"); ServletConfig config = this.getServletConfig(); System.out.println("Servlet的名稱:"+config.getServletName()); Enumeration<String> initParameterNames = config.getInitParameterNames(); while(initParameterNames.hasMoreElements()){ String parameterName = initParameterNames.nextElement(); String parameterValue = config.getInitParameter(parameterName); System.out.println("初始化參數名稱:"+parameterName+",初始化參數值"+parameterValue); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
web.xml
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>day09</display-name> <!-- 配置Servlet信息 --> <servlet> <!-- 配置Servlet名稱,名稱必須惟一 --> <servlet-name>HelloServlet</servlet-name> <!-- 配置Servlet的徹底路徑(包名+類名) --> <servlet-class>cn.servlet.HelloServlet</servlet-class> <!-- 配置初始化參數 --> <init-param> <param-name>你是誰</param-name> <param-value>我不知道我是誰</param-value> </init-param> <!-- 服務器啓動並加載Servlet --> <load-on-startup>1</load-on-startup> </servlet> <!-- 配置Servlet的一你射(訪問路徑) --> <servlet-mapping> <!-- 配置Servlet名稱,和上面的名稱必須相同 --> <servlet-name>HelloServlet</servlet-name> <!-- 配置訪問路徑 --> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>
9、ServletContext對象(很是重要)
定義:WEB容器啓動的時候,它會爲每一個WEB應用程序都建立一個對應的ServletContext對象,它表明當前應用。
一個WEB應用對應一個ServletContext對象。
一個WEB應用下有多個Servlet程序。
因此,全部的Servlet程序都共享一個ServletContext對象。
做用:
獲取web應用的全局初始化參數。
public Enumeration getInitParameterNames() public String getInitParameter(String name)
package cn.servlet; import java.io.IOException; import java.util.Enumeration; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 獲取全局初始化參數 */ @SuppressWarnings("serial") public class HelloServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); response.getWriter().println("哇 這是Servlet"); /** * 在GenericServlet中有這個方法,而HttpServlet繼承了GenericServlet,因此直接使用this.getServletContext()就能夠得到ServletContext對象 * public ServletContext getServletContext() { ServletConfig sc = getServletConfig(); if (sc == null) { throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized")); } return sc.getServletContext(); } */ //獲取ServletContext對象 ServletContext context = this.getServletContext(); Enumeration<String> parameterNames = context.getInitParameterNames(); while(parameterNames.hasMoreElements()){ String paramterName = parameterNames.nextElement(); String paramterValue = context.getInitParameter(paramterName); System.out.println("全局初始化參數名稱:"+paramterName+",全局初始化參數值:"+paramterValue); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>day09</display-name> <!-- 配置全局初始化參數 --> <context-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </context-param> <!-- 配置Servlet信息 --> <servlet> <!-- 配置Servlet名稱,名稱必須惟一 --> <servlet-name>HelloServlet</servlet-name> <!-- 配置Servlet的徹底路徑(包名+類名) --> <servlet-class>cn.servlet.HelloServlet</servlet-class> </servlet> <!-- 配置Servlet的一你射(訪問路徑) --> <servlet-mapping> <!-- 配置Servlet名稱,和上面的名稱必須相同 --> <servlet-name>HelloServlet</servlet-name> <!-- 配置訪問路徑 --> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>
實現數據的共享。
public void setAttribute(String name,Object object) 存入數據 public Object getAttribute(String name) 獲取數據 public void removeAttribute(String name) 刪除數據
計算訪問次數的CountServlet
package cn.servlet; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 統計網站的訪問次數 * @author Administrator * */ @SuppressWarnings("serial") public class CountServlet extends HttpServlet { /** * 實例一被建立,調用init方法進行初始化 * 在ServletContext對象中存入一個變量,賦值爲0 */ @Override public void init() throws ServletException { ServletContext context = this.getServletContext(); context.setAttribute("count", 0); } /** * 每個訪問,都會執行該方法 * 拿出count變量,值自增,而後存入ServletContext對象之中 */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context = this.getServletContext(); Integer count = (Integer) context.getAttribute("count"); context.setAttribute("count", ++count); response.setCharacterEncoding("utf-8"); response.getWriter().println("歡迎歡迎"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
顯示訪問次數的ShowServlet
package cn.servlet; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 顯示網站被訪問的此時 * */ @SuppressWarnings("serial") public class ShowServlet extends HttpServlet { /** * 獲取網站的訪問次數 */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context = this.getServletContext(); Integer count = (Integer) context.getAttribute("count"); if(count != null ){ response.setContentType("text/html;charset=utf-8"); response.getWriter().println("<h3>該網站一共被訪問了:"+count+"次</h3>"); }else{ response.setContentType("text/html;charset=utf-8"); response.getWriter().println("<h3>該網站一共被訪問了:0次</h3>"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
web.xml
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>day09</display-name> <!-- 配置Servlet信息 --> <servlet> <!-- 配置Servlet名稱,名稱必須惟一 --> <servlet-name>HelloServlet</servlet-name> <!-- 配置Servlet的徹底路徑(包名+類名) --> <servlet-class>cn.servlet.HelloServlet</servlet-class> </servlet> <servlet> <servlet-name>CountServlet</servlet-name> <servlet-class>cn.servlet.CountServlet</servlet-class> </servlet> <servlet> <servlet-name>ShowServlet</servlet-name> <servlet-class>cn.servlet.ShowServlet</servlet-class> </servlet> <!-- 配置Servlet的一你射(訪問路徑) --> <servlet-mapping> <!-- 配置Servlet名稱,和上面的名稱必須相同 --> <servlet-name>HelloServlet</servlet-name> <!-- 配置訪問路徑 --> <url-pattern>/hello</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>CountServlet</servlet-name> <url-pattern>/count</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ShowServlet</servlet-name> <url-pattern>/show</url-pattern> </servlet-mapping> </web-app>
讀取資源文件。
public InputStream getResourceAsStream(String path) 經過文件的地址獲取輸入流 public String getRealPath(String path) 經過文件的地址能夠獲取文件的絕對磁盤路徑
傳統方式讀取資源文件
package cn.servlet; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 讀取資源文件 */ @SuppressWarnings("serial") public class ReadServlet extends HttpServlet { /** * 傳統方式讀取資源文件 * 交給服務器處理,相對的位置是tomcat/bin目錄 * */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { InputStream is = new FileInputStream("/day09/src/db.properties"); Properties p = new Properties(); p.load(is); String username = p.getProperty("username"); String password = p.getProperty("password"); String desc = p.getProperty("desc"); System.out.println("用戶名:"+username+",密碼:"+password+",描述:"+desc); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
web.xml
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>day09</display-name> <!-- 配置Servlet信息 --> <servlet> <!-- 配置Servlet名稱,名稱必須惟一 --> <servlet-name>HelloServlet</servlet-name> <!-- 配置Servlet的徹底路徑(包名+類名) --> <servlet-class>cn.servlet.HelloServlet</servlet-class> </servlet> <servlet> <servlet-name>CountServlet</servlet-name> <servlet-class>cn.servlet.CountServlet</servlet-class> </servlet> <servlet> <servlet-name>ShowServlet</servlet-name> <servlet-class>cn.servlet.ShowServlet</servlet-class> </servlet> <servlet> <servlet-name>ReadServlet</servlet-name> <servlet-class>cn.servlet.ReadServlet</servlet-class> </servlet> <!-- 配置Servlet的一你射(訪問路徑) --> <servlet-mapping> <!-- 配置Servlet名稱,和上面的名稱必須相同 --> <servlet-name>HelloServlet</servlet-name> <!-- 配置訪問路徑 --> <url-pattern>/hello</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>CountServlet</servlet-name> <url-pattern>/count</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ShowServlet</servlet-name> <url-pattern>/show</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ReadServlet</servlet-name> <url-pattern>/read</url-pattern> </servlet-mapping> </web-app>
這種辦法是不行的啊,會出錯,由於找不到文件,由於此時的相對位置是Tomcat的bin目錄。
package cn.servlet; import java.io.IOException; import java.util.Properties; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 讀取資源文件 */ @SuppressWarnings("serial") public class ReadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Properties p = new Properties(); //傳入服務器端地址 p.load(this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties")); String username = p.getProperty("username"); String password = p.getProperty("password"); String desc = p.getProperty("desc"); System.out.println("用戶名:"+username+",密碼:"+password+",描述:"+desc); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>day09</display-name> <!-- 配置Servlet信息 --> <servlet> <!-- 配置Servlet名稱,名稱必須惟一 --> <servlet-name>HelloServlet</servlet-name> <!-- 配置Servlet的徹底路徑(包名+類名) --> <servlet-class>cn.servlet.HelloServlet</servlet-class> </servlet> <servlet> <servlet-name>CountServlet</servlet-name> <servlet-class>cn.servlet.CountServlet</servlet-class> </servlet> <servlet> <servlet-name>ShowServlet</servlet-name> <servlet-class>cn.servlet.ShowServlet</servlet-class> </servlet> <servlet> <servlet-name>ReadServlet</servlet-name> <servlet-class>cn.servlet.ReadServlet</servlet-class> </servlet> <!-- 配置Servlet的一你射(訪問路徑) --> <servlet-mapping> <!-- 配置Servlet名稱,和上面的名稱必須相同 --> <servlet-name>HelloServlet</servlet-name> <!-- 配置訪問路徑 --> <url-pattern>/hello</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>CountServlet</servlet-name> <url-pattern>/count</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ShowServlet</servlet-name> <url-pattern>/show</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ReadServlet</servlet-name> <url-pattern>/read</url-pattern> </servlet-mapping> </web-app>
用getRealPath()獲取資源文件
package cn.servlet; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 讀取資源文件 */ @SuppressWarnings("serial") public class ReadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Properties p = new Properties(); //傳入服務器端地址 p.load(new FileInputStream(this.getServletContext().getRealPath("/WEB-INF/classes/db.properties"))); String username = p.getProperty("username"); String password = p.getProperty("password"); String desc = p.getProperty("desc"); System.out.println("用戶名:"+username+",密碼:"+password+",描述:"+desc); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>day09</display-name> <!-- 配置Servlet信息 --> <servlet> <!-- 配置Servlet名稱,名稱必須惟一 --> <servlet-name>HelloServlet</servlet-name> <!-- 配置Servlet的徹底路徑(包名+類名) --> <servlet-class>cn.servlet.HelloServlet</servlet-class> </servlet> <servlet> <servlet-name>CountServlet</servlet-name> <servlet-class>cn.servlet.CountServlet</servlet-class> </servlet> <servlet> <servlet-name>ShowServlet</servlet-name> <servlet-class>cn.servlet.ShowServlet</servlet-class> </servlet> <servlet> <servlet-name>ReadServlet</servlet-name> <servlet-class>cn.servlet.ReadServlet</servlet-class> </servlet> <!-- 配置Servlet的一你射(訪問路徑) --> <servlet-mapping> <!-- 配置Servlet名稱,和上面的名稱必須相同 --> <servlet-name>HelloServlet</servlet-name> <!-- 配置訪問路徑 --> <url-pattern>/hello</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>CountServlet</servlet-name> <url-pattern>/count</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ShowServlet</servlet-name> <url-pattern>/show</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ReadServlet</servlet-name> <url-pattern>/read</url-pattern> </servlet-mapping> </web-app>
11、Http協議
響應頭
location 和302狀態碼一塊兒完成重定向的操做
重定向:
我向小A借錢,小A沒有,而後小A給我一個暗號302和地址,我接收到這個暗號302,我就知道小A沒有,我接收到的地址,就知道小A告訴我誰有錢,而後我就去向誰借錢。
package cn.http; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 重定向 * */ @SuppressWarnings("serial") public class DemoServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); //response.getWriter().print("我向小A借錢"); //發送暗號 302 以表示沒錢 response.setStatus(302); //發送地址 用來告訴我誰有錢 response.setHeader("location", "/day09/xu.html"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>day09</display-name> <servlet> <servlet-name>ReadServlet</servlet-name> <servlet-class>cn.servlet.ReadServlet</servlet-class> </servlet> <servlet> <servlet-name>DemoServlet</servlet-name> <servlet-class>cn.http.DemoServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DemoServlet</servlet-name> <url-pattern>/demo</url-pattern> </servlet-mapping> </web-app>
在webRoot下新建xu.html
<!DOCTYPE html> <html> <head> <title>xul.html</title> <meta name="keywords" content="keyword1,keyword2,keyword3"> <meta name="description" content="this is my page"> <meta name="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <h1> 老許我是土豪,有的是錢</h1> </body> </html>
由於是客戶端請求,因此使用的是客戶端絕對路徑。並且經過f12,咱們知道客戶端發送了2次請求,此時的請求地址變爲了http://localhost:8080/day09/xu.html,而不是http://localhost:8080/day09/demo。
refresh 頁面的定時刷新
package cn.http; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @SuppressWarnings("serial") public class RefreshServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); response.getWriter().println("定時刷新"); response.setHeader("refresh", "5;URL=/day09/xu.html"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <servlet> <servlet-name>RefreshServlet</servlet-name> <servlet-class>cn.http.RefreshServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>RefreshServlet</servlet-name> <url-pattern>/refresh</url-pattern> </servlet-mapping> </web-app>
last-modefied和if-modefied-since以及304狀態碼控制頁面的緩存