Java Web开发介绍

时间:2022-11-17 15:59:12

转自:http://www.cnblogs.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/

Java Web开发介绍
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();
}
}
Java Web开发介绍

看看python如何实现:

>>> import requests
>>> requests.get("http://automationtesting.sinaapp.com").text

2行搞定,可见web访问这块Java是相当笨拙。

HttpURLConnection类的Javadoc,建议不要复用HttpURLConnection的。万一这样使用HttpURLConnection的不具有线程问题,不同线程之间不能共享。

下面我们把下载放在一个方法:

Java Web开发介绍
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();
}
}
}
}
}
Java Web开发介绍

从网页获取的返回码

最重要的HTML返回码为:

Return Code Explaination
200 Ok
301 Permanent redirect to another webpage
400 Bad request
404 Not found

下面的代码将访问网页,打印HTML访问返回代码。

Java Web开发介绍
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);
}
}
Java Web开发介绍

python实现如下:

>>> import requests
>>> result = requests.get("http://automationtesting.sinaapp.com")
>>> result.status_code
200

因特网媒体类型(MIME,又名Content-type)定义是网络资源的类型。 MIME类型是在因特网上的文件格式,由两部分组成。对于HTML页面的内容类型为"text/html"的。

Java Web开发介绍
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);
}
}
Java Web开发介绍

Python实现如下:

>>> import requests
>>> result = requests.get("http://automationtesting.sinaapp.com")
>>> result.headers['content-type']
'text/html;charset=utf-8'