1. 程式人生 > >JavaWeb學習總結(四):Servlet開發(二)

JavaWeb學習總結(四):Servlet開發(二)

一、ServletConfig講解

1.1、配置Servlet初始化引數

  在Servlet的配置檔案web.xml中,可以使用一個或多個<init-param>標籤為servlet配置一些初始化引數。

例如:

複製程式碼
 1 <servlet>
 2     <servlet-name>ServletConfigDemo1</servlet-name>  3 <servlet-class>gacl.servlet.study.ServletConfigDemo1</servlet-class>  4 <!--配置ServletConfigDemo1的初始化引數 -->  5 <init-param>  6 <param-name>name</param-name>  7 <param-value>gacl</param-value>  8 </init-param>  9 <init-param> 10 <param-name>password</param-name> 11 <param-value>123</param-value> 12 </init-param> 13 <init-param> 14 <param-name>charset</param-name> 15 <param-value>UTF-8</param-value> 16 </init-param> 17 </servlet>
複製程式碼

1.2、通過ServletConfig獲取Servlet的初始化引數

  當servlet配置了初始化引數後,web容器在建立servlet例項物件時,會自動將這些初始化引數封裝到ServletConfig物件中,並在呼叫servlet的init方法時,將ServletConfig物件傳遞給servlet。進而,我們通過ServletConfig物件就可以得到當前servlet的初始化引數資訊。

例如:

複製程式碼
 1 package gacl.servlet.study;
 2 
 3 import java.io.IOException;  4 import java.util.Enumeration;  5 import javax.servlet.ServletConfig;  6 import javax.servlet.ServletException;  7 import javax.servlet.http.HttpServlet;  8 import javax.servlet.http.HttpServletRequest;  9 import javax.servlet.http.HttpServletResponse; 10 11 public class ServletConfigDemo1 extends HttpServlet { 12 13 /** 14  * 定義ServletConfig物件來接收配置的初始化引數 15 */ 16 private ServletConfig config; 17 18 /** 19  * 當servlet配置了初始化引數後,web容器在建立servlet例項物件時, 20  * 會自動將這些初始化引數封裝到ServletConfig物件中,並在呼叫servlet的init方法時, 21  * 將ServletConfig物件傳遞給servlet。進而,程式設計師通過ServletConfig物件就可以 22  * 得到當前servlet的初始化引數資訊。 23 */ 24  @Override 25 public void init(ServletConfig config) throws ServletException { 26 this.config = config; 27  } 28 29 public void doGet(HttpServletRequest request, HttpServletResponse response) 30 throws ServletException, IOException { 31 //獲取在web.xml中配置的初始化引數 32 String paramVal = this.config.getInitParameter("name");//獲取指定的初始化引數 33  response.getWriter().print(paramVal); 34 35 response.getWriter().print("<hr/>"); 36 //獲取所有的初始化引數 37 Enumeration<String> e = config.getInitParameterNames(); 38 while(e.hasMoreElements()){ 39 String name = e.nextElement(); 40 String value = config.getInitParameter(name); 41 response.getWriter().print(name + "=" + value + "<br/>"); 42  } 43  } 44 45 public void doPost(HttpServletRequest request, HttpServletResponse response) 46 throws ServletException, IOException { 47 this.doGet(request, response); 48  } 49 50 }
複製程式碼

執行結果如下:

  

二、ServletContext物件

  WEB容器在啟動時,它會為每個WEB應用程式都建立一個對應的ServletContext物件,它代表當前web應用。
  ServletConfig物件中維護了ServletContext物件的引用,開發人員在編寫servlet時,可以通過ServletConfig.getServletContext方法獲得ServletContext物件。
  由於一個WEB應用中的所有Servlet共享同一個ServletContext物件,因此Servlet物件之間可以通過ServletContext物件來實現通訊。ServletContext物件通常也被稱之為context域物件。

三、ServletContext的應用

  3.1、多個Servlet通過ServletContext物件實現資料共享

  範例:ServletContextDemo1和ServletContextDemo2通過ServletContext物件實現資料共享

複製程式碼
 1 package gacl.servlet.study;
 2 
 3 import java.io.IOException;  4 import javax.servlet.ServletContext;  5 import javax.servlet.ServletException;  6 import javax.servlet.http.HttpServlet;  7 import javax.servlet.http.HttpServletRequest;  8 import javax.servlet.http.HttpServletResponse;  9 10 public class ServletContextDemo1 extends HttpServlet { 11 12 public void doGet(HttpServletRequest request, HttpServletResponse response) 13 throws ServletException, IOException { 14 String data = "xdp_gacl"; 15 /** 16  * ServletConfig物件中維護了ServletContext物件的引用,開發人員在編寫servlet時, 17  * 可以通過ServletConfig.getServletContext方法獲得ServletContext物件。 18 */ 19 ServletContext context = this.getServletConfig().getServletContext();//獲得ServletContext物件 20 context.setAttribute("data", data); //將data儲存到ServletContext物件中 21  } 22 23 public void doPost(HttpServletRequest request, HttpServletResponse response) 24 throws ServletException, IOException { 25  doGet(request, response); 26  } 27 }
複製程式碼 複製程式碼
 1 package gacl.servlet.study;
 2 
 3 import java.io.IOException;  4 import javax.servlet.ServletContext;  5 import javax.servlet.ServletException;  6 import javax.servlet.http.HttpServlet;  7 import javax.servlet.http.HttpServletRequest;  8 import javax.servlet.http.HttpServletResponse;  9 10 public class ServletContextDemo2 extends HttpServlet { 11 12 public void doGet(HttpServletRequest request, HttpServletResponse response) 13 throws ServletException, IOException { 14 ServletContext context = this.getServletContext(); 15 String data = (String) context.getAttribute("data");//從ServletContext物件中取出資料 16 response.getWriter().print("data="+data); 17  } 18 19 public void doPost(HttpServletRequest request, HttpServletResponse response) 20 throws ServletException, IOException { 21  doGet(request, response); 22  } 23 }
複製程式碼

  先執行ServletContextDemo1,將資料data儲存到ServletContext物件中,然後執行ServletContextDemo2就可以從ServletContext物件中取出資料了,這樣就實現了資料共享,如下圖所示:

  

  3.2、獲取WEB應用的初始化引數

  在web.xml檔案中使用<context-param>標籤配置WEB應用的初始化引數,如下所示:

複製程式碼
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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  3  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  4 <display-name></display-name>  5 <!-- 配置WEB應用的初始化引數 -->  6 <context-param>  7 <param-name>url</param-name>  8 <param-value>jdbc:mysql://localhost:3306/test</param-value>  9 </context-param> 10 11 <welcome-file-list> 12 <welcome-file>index.jsp</welcome-file> 13 </welcome-file-list> 14 </web-app>
複製程式碼

  獲取Web應用的初始化引數,程式碼如下:

複製程式碼
 1 package gacl.servlet.study;
 2 
 3 import java.io.IOException;  4 import javax.servlet.ServletContext;  5 import javax.servlet.ServletException;  6 import javax.servlet.http.HttpServlet;  7 import javax.servlet.http.HttpServletRequest;  8 import javax.servlet.http.HttpServletResponse;  9 10 11 public class ServletContextDemo3 extends HttpServlet { 12 13 public void doGet(HttpServletRequest request, HttpServletResponse response) 14 throws ServletException, IOException { 15 16 ServletContext context = this.getServletContext(); 17 //獲取整個web站點的初始化引數 18 String contextInitParam = context.getInitParameter("url"); 19  response.getWriter().print(contextInitParam); 20  } 21 22 public void doPost(HttpServletRequest request, HttpServletResponse response) 23 throws ServletException, IOException { 24  doGet(request, response); 25  } 26 27 }
複製程式碼

執行結果:

  

  3.3、用servletContext實現請求轉發

ServletContextDemo4
複製程式碼
 1 package gacl.servlet.study;
 2 
 3 import java.io.IOException;  4 import java.io.PrintWriter;  5 import javax.servlet.RequestDispatcher;  6 import javax.servlet.ServletContext;  7 import javax.servlet.ServletException;  8 import javax.servlet.http.HttpServlet;  9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 public class ServletContextDemo4 extends HttpServlet { 13 14 public void doGet(HttpServletRequest request, HttpServletResponse response) 15 throws ServletException, IOException { 16 String data = "<h1><font color='red'>abcdefghjkl</font></h1>"; 17  response.getOutputStream().write(data.getBytes()); 18 ServletContext context = this.getServletContext();//獲取ServletContext物件 19 RequestDispatcher rd = context.getRequestDispatcher("/servlet/ServletContextDemo5");//獲取請求轉發物件(RequestDispatcher) 20 rd.forward(request, response);//呼叫forward方法實現請求轉發 21  } 22 23 public void doPost(HttpServletRequest request, HttpServletResponse response) 24 throws ServletException, IOException { 25  } 26 }
複製程式碼
ServletContextDemo5
複製程式碼
 1 package gacl.servlet.study;
 2 
 3 import java.io.IOException;  4 import javax.servlet.ServletException;  5 import javax.servlet.http.HttpServlet;  6 import javax.servlet.http.HttpServletRequest;  7 import javax.servlet.http.HttpServletResponse;  8  9 public class ServletContextDemo5 extends HttpServlet { 10 11 public void doGet(HttpServletRequest request, HttpServletResponse response) 12 throws ServletException, IOException { 13 response.getOutputStream().write("servletDemo5".getBytes()); 14  } 15 16 public void doPost(HttpServletRequest request, HttpServletResponse response) 17 throws ServletException, IOException { 18 this.doGet(request, response); 19  } 20 21 }
複製程式碼

  執行結果:

  

  訪問的是ServletContextDemo4,瀏覽器顯示的卻是ServletContextDemo5的內容,這就是使用ServletContext實現了請求轉發

  3.4、利用ServletContext物件讀取資原始檔

  專案目錄結構如下:

   

程式碼範例:使用servletContext讀取資原始檔

複製程式碼
  1 package gacl.servlet.study;
  2 
  3 import java.io.FileInputStream;  4 import java.io.FileNotFoundException;  5 import java.io.IOException;  6 import java.io.InputStream;  7 import java.text.MessageFormat;  8 import java.util.Properties;  9 import javax.servlet.ServletException;  10 import javax.servlet.http.HttpServlet;  11 import javax.servlet.http.HttpServletRequest;  12 import javax.servlet.http.HttpServletResponse;  13  14 /**  15  * 使用servletContext讀取資原始檔  16  *  17  * @author gacl  18  *  19 */  20 public class ServletContextDemo6 extends HttpServlet {  21  22 public void doGet(HttpServletRequest request, HttpServletResponse response)  23 throws ServletException, IOException {  24 /**  25  * response.setContentType("text/html;charset=UTF-8");目的是控制瀏覽器用UTF-8進行解碼;  26  * 這樣就不會出現中文亂碼了  27 */  28 response.setHeader("content-type","text/html;charset=UTF-8");  29 readSrcDirPropCfgFile(response);//讀取src目錄下的properties配置檔案  30 response.getWriter().println("<hr/>");  31 readWebRootDirPropCfgFile(response);//讀取WebRoot目錄下的properties配置檔案  32 response.getWriter().println("<hr/>");  33 readPropCfgFile(response);//讀取src目錄下的db.config包中的db3.properties配置檔案  34 response.getWriter().println("<hr/>");  35 readPropCfgFile2(response);//讀取src目錄下的gacl.servlet.study包中的db4.properties配置檔案  36  37  }  38  39 /**  40  * 讀取src目錄下的gacl.servlet.study包中的db4.properties配置檔案  41  * @param response  42  * @throws IOException  43 */  44 private void readPropCfgFile2(HttpServletResponse response)  45 throws IOException {  46 InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/gacl/servlet/study/db4.properties");  47 Properties prop = new Properties();  48  prop.load(in);  49 String driver = prop.getProperty("driver");  50 String url = prop.getProperty("url");  51 String username = prop.getProperty("username");  52 String password = prop.getProperty("password");  53 response.getWriter().println("讀取src目錄下的gacl.servlet.study包中的db4.properties配置檔案:");  54  response.getWriter().println(  55  MessageFormat.format(  56 "driver={0},url={1},username={2},password={3}",  57  driver,url, username, password));  58  }  59  60 /**  61  * 讀取src目錄下的db.config包中的db3.properties配置檔案  62  * @param response  63  * @throws FileNotFoundException  64  * @throws IOException  65 */  66 private void readPropCfgFile(HttpServletResponse response)  67 throws FileNotFoundException, IOException {  68 //通過ServletContext獲取web資源的絕對路徑  69 String path = this.getServletContext().getRealPath("/WEB-INF/classes/db/config/db3.properties");  70 InputStream in = new FileInputStream(path);  71 Properties prop = new Properties();  72  prop.load(in);  73 String driver = prop.getProperty("driver");  74 String url = prop.getProperty("url");  75 String username = prop.getProperty("username");  76 String password = prop.getProperty("password");  77 response.getWriter().println("讀取src目錄下的db.config包中的db3.properties配置檔案:");  78  response.getWriter().println(  79  MessageFormat.format( 80 "driver={0},url={1},username={2},password={3}", 81 driver,url, username, password)); 82 } 83 84 /** 85 * 通過ServletContext物件讀取WebRoot目錄下的properties配置檔案 86 * @param response 87 * @throws IOException 88 */ 89 private void readWebRootDirPropCfgFile(HttpServletResponse response) 90 throws IOException { 91 /** 92 * 通過ServletContext物件讀取WebRoot目錄下的properties配置檔案 93 * “/”代表的是專案根目錄 94 */ 95 InputStream in = this.getServletContext().getResourceAsStream("/db2.properties"); 96 Properties prop = new Properties(); 97 prop.load(in); 98 String driver = prop.getProperty("driver"); 99 String url = prop.getProperty("url"); 100 String username = prop.getProperty("username"); 101 String password = prop.getProperty("password"); 102 response.getWriter().println("讀取WebRoot目錄下的db2.properties配置檔案:"); 103 response.getWriter().print( 104 MessageFormat.format( 105 "driver={0},url={1},username={2},password={3}", 106 driver,url, username, password)); 107 } 108 109 /** 110 * 通過ServletContext物件讀取src目錄下的properties配置檔案 111 * @param response 112 * @throws IOException 113 */ 114 private void readSrcDirPropCfgFile(HttpServletResponse response) throws IOException { 115 /** 116 * 通過ServletContext物件讀取src目錄下的db1.properties配置檔案 117 */ 118 InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db1.properties"); 119 Properties prop = new Properties(); 120 prop.load(in); 121 String driver = prop.getProperty("driver"); 122 String url = prop.getProperty("url"); 123 String username = prop.getProperty("username"); 124 String password = prop.getProperty("password"); 125 response.getWriter().println("讀取src目錄下的db1.properties配置檔案:"); 126 response.getWriter().println( 127 MessageFormat.format( 128 "driver={0},url={1},username={2},password={3}", 129 driver,url, username, password)); 130 } 131 132 public void doPost(HttpServletRequest request, HttpServletResponse response) 133 throws ServletException, IOException { 134 this.doGet(request, response); 135 } 136 137 }
複製程式碼

執行結果如下:

  

程式碼範例:使用類裝載器讀取資原始檔

複製程式碼
  1 package gacl.servlet.study;
  2 
  3 import java.io.FileOutputStream;  4 import java.io.IOException;  5 import java.io.InputStream;  6 import java.io.OutputStream;  7 import java.text.MessageFormat;  8 import java.util.Properties;  9  10 import javax.servlet.ServletException;  11 import javax.servlet.http.HttpServlet;  12 import javax.servlet.http.HttpServletRequest;  13 import javax.servlet.http.HttpServletResponse;  14  15 /**  16  * 用類裝載器讀取資原始檔  17  * 通過類裝載器讀取資原始檔的注意事項:不適合裝載大檔案,否則會導致jvm記憶體溢位  18  * @author gacl  19  *  20 */  21 public class ServletContextDemo7 extends HttpServlet {  22  23 public void doGet(HttpServletRequest request, HttpServletResponse response)  24 throws ServletException, IOException {  25 /**  26  * response.setContentType("text/html;charset=UTF-8");目的是控制瀏覽器用UTF-8進行解碼;  27  * 這樣就不會出現中文亂碼了  28 */  29 response.setHeader("content-type","text/html;charset=UTF-8");  30  test1(response);  31 response.getWriter().println("<hr/>");  32  test2(response);  33 response.getWriter().println("<hr/>");  34 //test3();  35  test4();  36  37  }  38  39 /**  40  * 讀取類路徑下的資原始檔  41  * @param response  42  * @throws IOException  43 */  44 private void test1(HttpServletResponse response) throws IOException {  45 //獲取到裝載當前類的類裝載器  46 ClassLoader loader = ServletContextDemo7.class.getClassLoader();  47 //用類裝載器讀取src目錄下的db1.properties配置檔案  48 InputStream in = loader.getResourceAsStream("db1.properties");  49 Properties prop = new Properties();  50  prop.load(in);  51 String driver = prop.getProperty("driver");  52 String url = prop.getProperty("url");  53 String username = prop.getProperty("username");  54 String password = prop.getProperty("password");  55 response.getWriter().println("用類裝載器讀取src目錄下的db1.properties配置檔案:");  56  response.getWriter().println(  57  MessageFormat.format(  58 "driver={0},url={1},username={2},password={3}",  59  driver,url, username, password));  60  }  61  62 /**  63  * 讀取類路徑下面、包下面的資原始檔  64  * @param response  65  * @throws IOException  66 */  67 private void test2(HttpServletResponse response) throws IOException {  68 //獲取到裝載當前類的類裝載器  69 ClassLoader loader = ServletContextDemo7.class.getClassLoader();  70 //用類裝載器讀取src目錄下的gacl.servlet.study包中的db4.properties配置檔案  71 InputStream in = loader.getResourceAsStream("gacl/servlet/study/db4.properties");  72 Properties prop = new Properties();  73  prop.load(in);  74 String driver = prop.getProperty("driver");  75 String url = prop.getProperty("url");  76 String username = prop.getProperty("username");  77 String password = prop.getProperty("password");  78 response.getWriter().println("用類裝載器讀取src目錄下的gacl.servlet.study包中的db4.properties配置檔案:");  79  response.getWriter().println(  80  MessageFormat.format(  81 "driver={0},url={1},username={2},password={3}", 82 driver,url, username, password)); 83 } 84 85 /** 86 * 通過類裝載器讀取資原始檔的注意事項:不適合裝載大檔案,否則會導致jvm記憶體溢位 87 */ 88 public void test3() { 89 /** 90 * 01.avi是一個150多M的檔案,使用類載入器去讀取這個大檔案時會導致記憶體溢位: 91 * java.lang.OutOfMemoryError: Java heap space 92 */ 93 InputStream in = ServletContextDemo7.class.getClassLoader().getResourceAsStream("01.avi"); 94 System.out.println(in); 95 } 96 97 /** 98 * 讀取01.avi,並拷貝到e:\根目錄下 99 * 01.avi檔案太大,只能用servletContext去讀取 100 * @throws IOException 101 */ 102 public void test4() throws IOException { 103 // path=G:\Java學習視訊\JavaWeb學習視訊\JavaWeb\day05視訊\01.avi 104 // path=01.avi 105 String path = this.getServletContext().getRealPath("/WEB-INF/classes/01.avi"); 106 /** 107 * path.lastIndexOf("\\") + 1是一個非常絕妙的寫法 108 */ 109 String filename = path.substring(path.lastIndexOf("\\") + 1);//獲取檔名 110 InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/01.avi"); 111 byte buffer[] = new byte[1024]; 112 int len = 0; 113 OutputStream out = new FileOutputStream("e:\\" + filename); 114 while ((len = in.read(buffer)) > 0) { 115 out.write(buffer, 0, len); 116 } 117 out.close(); 118 in.close(); 119 } 120 121 public void doPost(HttpServletRequest request, HttpServletResponse response) 122 throws ServletException, IOException { 123 124 this.doGet(request, response); 125 } 126 127 }
複製程式碼

  執行結果如下:

  

四、在客戶端快取Servlet的輸出

  對於不經常變化的資料,在servlet中可以為其設定合理的快取時間值,以避免瀏覽器頻繁向伺服器傳送請求,提升伺服器的效能。例如:

複製程式碼
 1 package gacl.servlet.study;
 2 
 3 import java.io.IOException;  4  5 import javax.servlet.ServletException;  6 import javax.servlet.http.HttpServlet;  7 import javax.servlet.http.HttpServletRequest;  8 import javax.servlet.http.HttpServletResponse;  9 10 public class ServletDemo5 extends HttpServlet { 11 12 public void doGet(HttpServletRequest request, HttpServletResponse response) 13 throws ServletException, IOException { 14 String data = "abcddfwerwesfasfsadf"; 15 /** 16  * 設定資料合理的快取時間值,以避免瀏覽器頻繁向伺服器傳送請求,提升伺服器的效能 17  * 這裡是將資料的快取時間設定為1天 18 */ 19 response.setDateHeader("expires",System.currentTimeMillis() + 24 * 3600 * 1000); 20  response.getOutputStream().write(data.getBytes()); 21  } 22 23 public void doPost(HttpServletRequest request, HttpServletResponse response) 24 throws ServletException, IOException { 25 26 this.doGet(request, response); 27  } 28 29 }
複製程式碼

相關推薦

JavaWeb學習總結Servlet開發

一、ServletConfig講解 1.1、配置Servlet初始化引數   在Servlet的配置檔案web.xml中,可以使用一個或多個<init-param>標籤為servlet配置一些初始化引數。 例如: 1 <servlet&g

學習 WebService 第利用WSDLURL生成WebService客戶端<初級>

SM documents eight 生成 web gen get JD OS 我用的是最簡單的方法,利用jdk的命令wsimport -keep -p 包路徑 -d 代碼存放位置 WSDL網址 藍色是命令,粉色是存放位置,橘色是URL C:\Program Files\

Web開發之-JSP學習總結-第自定義標籤總結

一、自定義標籤開發步驟—以高仿<c:if test=""></c:if>標籤為例 1、編寫一個普通的java類,繼承SimpleTagSupport類,叫標籤處理器類。並且覆蓋doTag方法 /** * 標籤處理器類 * 1)繼承

javaweb學習總結—jsp簡單標籤標籤庫開發

一、JspFragment類介紹  javax.servlet.jsp.tagext.JspFragment類是在JSP2.0中定義的,它的例項物件代表JSP頁面中的一段符合JSP語法規範的JSP片段,這段JSP片段中不能包含JSP指令碼元素。  WEB容器在處理簡單標籤的標

Javaweb學習筆記Servlet常見問題

1. 在server.xml中設定context路徑,如果Path值為“”,則可以訪問自己的頁面,無法訪問Tomcat主頁 2. 同樣的context路徑,path為空,卻啟動toncat失敗     解決:原因是有兩個相同的path路徑,空字元算相同的路徑 path=“”

javaweb學習筆記會話管理1

目錄 會話管理 1.概念 2.cookie技術 2.1 Cookie一般處理流程 2.2 Cookie類 會話管理 1.概念 一次會話: 開啟瀏覽器 -> 訪問一些伺服器內容 -> 關閉瀏覽器。(瀏覽器A給伺服器傳送請求,訪問web程式,該次會話就

javaweb學習筆記Servlet

Servlet的詳細解讀目錄 Servlet詳解 1.Servlet概述與執行過程 2.Servlet對映路徑 3.Servlet 的生命週期 3.1生命週期方法 3.2虛擬碼演示生命週期

Javaweb學習筆記servlet初體驗、HTTP協議

目錄 1.Servlet體驗 1.1servlet的繼承體系 1.2手動開發動態web資源 1.3工具開發動態資源 2.HTTP協議 2.1概念 2.2請求資訊 2.2.1請求行 2.2.2請求頭 2.2.3空行與實體內容 2.3HttpServlet

JavaWeb學習總結Http協議

一、什麼是HTTP協議   HTTP是hypertext transfer protocol(超文字傳輸協議)的簡寫,它是TCP/IP協議的一個應用層協議,用於定義WEB瀏覽器與WEB伺服器之間交換資料的過程。客戶端連上web伺服器後,若想獲得web伺服器中的某個web資源,需遵守一定的通訊格式,H

Java for Web學習筆記Servlet2HelloServlet

繼承關係: javax.servlet.GenericServlet –》javax.servlet.http.HttpServlet。 405返回 如果我們不重寫Servlet的doGet而採用HTTP GET的方式,將返回405 將返回405 Method Not Allowed。 如果我們重寫do

javaweb學習總結(十一)——使用Cookie進行會話管理

緩存 利用 iter() 自然 web har oca main end 一、會話的概念   會話可簡單理解為:用戶開一個瀏覽器,點擊多個超鏈接,訪問服務器多個web資源,然後關閉瀏覽器,整個過程稱之為一個會話。  有狀態會話:一個同學來過教室,下次再來教室,我們會知道這個

JavaWeb學習總結(十三)——使用Session防止表單重復提交

Coding etc pub submit exce sdf patch 傳輸 alt  在平時開發中,如果網速比較慢的情況下,用戶提交表單後,發現服務器半天都沒有響應,那麽用戶可能會以為是自己沒有提交表單,就會再點擊提交按鈕重復提交表單,我們在開發中必須防止表單重復提交。

javaweb學習總結(十五)——JSP基礎語法

troy 嚴格 too cal service alt 隱式 情況 當前系統時間  任何語言都有自己的語法,JAVA中有,JSP雖然是在JAVA上的一種應用,但是依然有其自己擴充的語法,而且在JSP中,所有的JAVA語句都可以使用。 一、JSP模版元素   JSP頁面中的H

RabbitMQ學習路由模式direct

exceptio 指定 手動 ack key static ech 保存日誌 sta 1、什麽是路由模式(direct)   路由模式是在使用交換機的同時,生產者指定路由發送數據,消費者綁定路由接受數據。與發布/訂閱模式不同的是,發布/訂閱模式只要是綁定了交換機的隊列都會收

JavaWeb學習總結---httpservletrequest物件

javaweb學習總結(十)——HttpServletRequest物件(一) 一、HttpServletRequest介紹   HttpServletRequest物件代表客戶端的請求,當客戶端通過HTTP協議訪問伺服器時,HTTP請求頭中的所有資訊都封裝在這個物件中,通過這個物件提供的方

Javaweb學習總結—— HttpServletResponse物件

HttpServletResponse物件、用Servlet生成驗證碼圖片。 web伺服器根據每一次請求,建立相對應的請求物件request、和代表響應的物件response。 通過請求物件,我們獲取資料。 通過響應物件,我們傳送資料。 Http

機器學習總結梯度消失vanishing gradient與梯度爆炸exploding gradient問題

(1)梯度不穩定問題: 什麼是梯度不穩定問題:深度神經網路中的梯度不穩定性,前面層中的梯度或會消失,或會爆炸。 原因:前面層上的梯度是來自於後面層上梯度的乘乘積。當存在過多的層次時,就出現了內在本質

Java for Web學習筆記Servlet6doGet()和doPost()是執行緒還是佇列

做一個小實驗,程式碼如下: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ for(i

Java for Web學習筆記Servlet7上傳檔案

上傳檔案 Servlet的引數設定 採用annotation方式如下: @WebServlet( name = "TicketServlet", urlPatterns = {"/tickets"}, loadOnStartup = 1 ) /* MultipartConfig配置了本Servlet

JavaWeb專案開發Servlet+Bean+Dao+MySQL

需要使用的技術: JSP動態網頁 Servlet獲得客戶端請求、轉發請求、跳轉到下一個介面 JavaBean 業務Bean (業務邏輯具體實現類),處理業務邏輯 Dao 訪問資料庫,操作資料庫例表 (增刪查改) MySQL 資料庫 實現系統以下介面和功能: 登入 我的資