星空网 > 软件开发 > Java

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,StrutsSpring框架。这些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'

 




原标题:Java Web开发介绍

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

珠海荷包岛怎么去?附具体位置:https://www.vstour.cn/a/388212.html
珠海荷包岛门票残疾人有没有优惠:https://www.vstour.cn/a/388213.html
珠海荷包岛门票免费吗:https://www.vstour.cn/a/388214.html
珠海荷包岛门票多少钱?:https://www.vstour.cn/a/388215.html
4/9号的热门产品,来瞅瞅有没你的产品在里面!:https://www.kjdsnews.com/a/1837948.html
关岛/Guam:https://www.kjdsnews.com/a/1837949.html
南京到普陀风景区 南京到普陀风景区怎么走:https://www.vstour.cn/a/404228.html
十堰有哪些官方网站:https://www.vstour.cn/a/404229.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流