Java Web開發介紹,javaweb開發
簡介
Java很好地支援web開發,在案頭上Eclipse RCP談不上成功,JAVA是主要用在伺服器端,和Python一樣是極其重要的Web後台開發語言。
Java Web應用通常不直接在伺服器上運行,而是在Web容器內。容器提供的運行時環境,提供JVM (Java Virtual Machine)運行本地Java應用。容器本身也運行在JVM。
通常Java的分為兩個容器:Web容器和Java EE容器。典型的Web容器是Tomcat或Jetty。Web容器支援Java Servlet和JavaServer Page的執行。 Java EE容器支援更多的功能,例如,伺服器負載的分布。
大部分現代的Java Web架構是基於servlet的。流行的Java Web架構有GWT,JavaServer Faces,Struts和Spring架構。這些Web架構通常需要至少需要Web容器。
Java Web應用程式是動態資源(如Servlet,JavaServer頁,Java類,jar)和靜態資源(HTML頁面和圖片)的集合。 Java Web應用程式可以部署為WAR(Web ARchive)檔案。
WAR檔案是包含相應的Web應用程式的完整內容的zip檔案。
標準的Java技術由Java Community Process (JCP http://jcp.org/)指定。包含如下:
servlet:擴充"HttpServlet",在Web容器中的響應HTTP請求的Java類。最新的正式版的Servlet 3.1,參見https://en.wikipedia.org/wiki/Java_servlet。
JavaServer頁面(JavaServer Page JSP)是含有HTML和Java代碼的檔案。首次執行時web cotainer編譯JSP成servlet。目前的最新版本是2.2。參見https://en.wikipedia.org/wiki/JavaServer_Pages。
JavaServer Pages Standard Tag Library (JSTL)用標籤的形式封裝常見的核心功能。目前的版本是1.2.1,參見https://en.wikipedia.org/wiki/JavaServer_Pages_Standard_Tag_Library。
非標準的Java Web開發。例如,GWT支援Java開發,並編譯成JavaScript。
用戶端操作
Java提供了通用的,輕量級的HTTP用戶端API通過HTTP或HTTPS協議訪問的資源。的主要類訪問網際網路類為java.net.URL類和java.net.HttpURLConnection類。
URL類可指向網路資源,而HttpURLConnection的類可用於訪問網路資源。HttpURLConnection類可建立InputStream(像讀取本地檔案一樣)。
在最新版本的HttpURLConnection支援透明響應壓縮(通過頭:Accept-Encoding: gzip)。
比如訪問:http://automationtesting.sinaapp.com/
package com.company;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;public class DownloadWebpageExample { public static void main(String[] args) { try { URL url = new URL("http://automationtesting.sinaapp.com/"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); String readStream = readStream(con.getInputStream()); // Give output for the command line System.out.println(readStream); } catch (Exception e) { e.printStackTrace(); } } private static String readStream(InputStream in) { StringBuilder sb = new StringBuilder(); try (BufferedReader reader = new BufferedReader(new InputStreamReader(in));) { String nextLine = ""; while ((nextLine = reader.readLine()) != null) { sb.append(nextLine); } } catch (IOException e) { e.printStackTrace(); } return sb.toString(); }}
看看python如何?:
>>> import requests>>> requests.get("http://automationtesting.sinaapp.com").text
2行搞定,可見web訪問這塊Java是相當笨拙。
HttpURLConnection類的Javadoc,建議不要複用HttpURLConnection的。萬一這樣使用HttpURLConnection的不具有線程問題,不同線程之間不能共用。
下面我們把下載放在一個方法:
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.URL;public class ReadWebPage { public static void main(String[] args) { String urlText = "http://automationtesting.sinaapp.com"; BufferedReader in = null; try { URL url = new URL(urlText); in = new BufferedReader(new InputStreamReader(url.openStream())); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); } } catch (Exception e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } }}
從網頁擷取的返回碼
最重要的HTML返回碼為:
| Return Code |
Explaination |
| 200 |
Ok |
| 301 |
Permanent redirect to another webpage |
| 400 |
Bad request |
| 404 |
Not found |
下面的代碼將訪問網頁,列印HTML訪問傳回碼。
import java.io.IOException;import java.net.HttpURLConnection;import java.net.URL;public class ReadReturnCode { public static void main(String[] args) throws IOException { String urltext = "http://automationtesting.sinaapp.com/"; URL url = new URL(urltext); int responseCode = ((HttpURLConnection) url.openConnection()) .getResponseCode(); System.out.println(responseCode); }}
python實現如下:
>>> import requests>>> result = requests.get("http://automationtesting.sinaapp.com")>>> result.status_code200
網際網路媒體類型(MIME,又名Content-type)定義是網路資源的類型。 MIME類型是在網際網路上的檔案格式,由兩部分組成。對於HTML頁面的內容類型為"text/html"的。
import java.io.IOException;import java.net.HttpURLConnection;import java.net.URL;public class ReadMimeType { public static void main(String[] args) throws IOException { String urltext = "http://automationtesting.sinaapp.com"; URL url = new URL(urltext); String contentType = ((HttpURLConnection) url.openConnection()) .getContentType(); System.out.println(contentType); }}
Python實現如下:
>>> import requests>>> result = requests.get("http://automationtesting.sinaapp.com")>>> result.headers['content-type']'text/html;charset=utf-8'