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請求的Jav??a類。最新的正式版的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").text2行搞定,可見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'新聞熱點
疑難解答