java开发简介_Java Web开发介绍
轉(zhuǎn)自:http://www.cnblogs.com/pythontesting/p/4963021.html
簡介
Java很好地支持web開發(fā),在桌面上Eclipse RCP談不上成功,JAVA是主要用在服務器端,和Python一樣是極其重要的Web后臺開發(fā)語言。
Java Web應用通常不直接在服務器上運行,而是在Web容器內(nèi)。容器提供的運行時環(huán)境,提供JVM (Java Virtual Machine)運行本地Java應用。容器本身也運行在JVM。
通常Java的分為兩個容器:Web容器和Java EE容器。典型的Web容器是Tomcat或Jetty。Web容器支持Java Servlet和JavaServer Page的執(zhí)行。 Java EE容器支持更多的功能,例如,服務器負載的分布。
大部分現(xiàn)代的Java Web框架是基于servlet的。流行的Java Web框架有GWT,JavaServer Faces,Struts和Spring框架。這些Web框架通常需要至少需要Web容器。
Java Web應用程序是動態(tài)的資源(如Servlet,JavaServer頁,Java類,jar)和靜態(tài)資源(HTML頁面和圖片)的集合。 Java Web應用程序可以部署為WAR(Web ARchive)文件。
WAR文件是包含相應的Web應用程序的完整內(nèi)容的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代碼的文件。首次執(zhí)行時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開發(fā)。例如,GWT支持Java開發(fā),并編譯成JavaScript。
客戶端操作
Java提供了通用的,輕量級的HTTP客戶端API通過HTTP或HTTPS協(xié)議訪問的資源。的主要類訪問因特網(wǎng)類為java.net.URL類和java.net.HttpURLConnection類。
URL類可指向網(wǎng)絡資源,而HttpURLConnection的類可用于訪問網(wǎng)絡資源。HttpURLConnection類可創(chuàng)建InputStream(像讀取本地文件一樣)。
在最新版本的HttpURLConnection支持透明響應壓縮(通過頭:Accept-Encoding: gzip)。
比如訪問:http://automationtesting.sinaapp.com/
package com.company;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.net.HttpURLConnection;
importjava.net.URL;
public classDownloadWebpageExample {
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 forthe 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();
}
returnsb.toString();
}
}
看看python如何實現(xiàn):
>>> importrequests
>>> requests.get("http://automationtesting.sinaapp.com").text
2行搞定,可見web訪問這塊Java是相當笨拙。
HttpURLConnection類的Javadoc,建議不要復用HttpURLConnection的。萬一這樣使用HttpURLConnection的不具有線程問題,不同線程之間不能共享。
下面我們把下載放在一個方法:
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.net.URL;
public classReadWebPage {
public static voidmain(String[] args) {
String urlText = "http://automationtesting.sinaapp.com";
BufferedReader in = null;
try{
URL url = newURL(urlText);
in = new BufferedReader(newInputStreamReader(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();
}
}
}
}
}
從網(wǎng)頁獲取的返回碼
最重要的HTML返回碼為:
Return CodeExplaination
200
Ok
301
Permanent redirect to another webpage
400
Bad request
404
Not found
下面的代碼將訪問網(wǎng)頁,打印HTML訪問返回代碼。
importjava.io.IOException;
importjava.net.HttpURLConnection;
importjava.net.URL;
public classReadReturnCode {
public static void main(String[] args) throwsIOException {
String urltext = "http://automationtesting.sinaapp.com/";
URL url = newURL(urltext);
int responseCode =((HttpURLConnection) url.openConnection())
.getResponseCode();
System.out.println(responseCode);
}
}
python實現(xiàn)如下:
>>> import requests
>>> result = requests.get("http://automationtesting.sinaapp.com")
>>> result.status_code
200
因特網(wǎng)媒體類型(MIME,又名Content-type)定義是網(wǎng)絡資源的類型。 MIME類型是在因特網(wǎng)上的文件格式,由兩部分組成。對于HTML頁面的內(nèi)容類型為"text/html"的。
importjava.io.IOException;
importjava.net.HttpURLConnection;
importjava.net.URL;
public classReadMimeType {
public static void main(String[] args) throwsIOException {
String urltext = "http://automationtesting.sinaapp.com";
URL url = newURL(urltext);
String contentType =((HttpURLConnection) url.openConnection())
.getContentType();
System.out.println(contentType);
}
}
Python實現(xiàn)如下:
>>> importrequests
>>> result = requests.get("http://automationtesting.sinaapp.com")
>>> result.headers['content-type']
'text/html;charset=utf-8'
總結(jié)
以上是生活随笔為你收集整理的java开发简介_Java Web开发介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 终于!法拉第未来:FF 91 Futur
- 下一篇: 华为年度机皇来了!P60 Pro开启预售