国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

JavaWeb開發介紹

2019-11-14 15:05:47
字體:
來源:轉載
供稿:網友

轉自:http://m.survivalescaperooms.com/pythontesting/p/4963021.html

java Web開發介紹

簡介

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").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 CodeExplaination
200Ok
301Permanent redirect to another webpage
400Bad request
404Not 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'

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 湄潭县| 屏山县| 吴桥县| 海安县| 正镶白旗| 明水县| 东光县| 乌苏市| 安丘市| 大方县| 南康市| 谷城县| 海安县| 镇巴县| 垦利县| 凉城县| 玛曲县| 齐河县| 平谷区| 浦县| 玛多县| 扬州市| 三台县| 乐安县| 昭通市| 德格县| 淳化县| 专栏| 兴海县| 军事| 岚皋县| 平邑县| 海兴县| 金溪县| 平利县| 望都县| 越西县| 华宁县| 巩留县| 岳池县| 鄂托克旗|