首页 > 学院 > 开发设计 > 正文

JavaWeb开发介绍

2019-11-14 15:05:47
字体:
来源:转载
供稿:网友

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表