Java Web development Introduction, Java Web Development
Introduction
Java supports web development well. on the desktop, Eclipse RCP is not successful. JAVA is mainly used on the server side, and is an extremely important Web Background development language like Python.
Java Web applications are usually not run directly on servers, but in Web containers. The runtime environment provided by the container and the JVM (Java Virtual Machine) to run local Java applications. The container itself runs in JVM.
Generally, Java is divided into two containers: Web Container and Java EE container. A typical Web container is Tomcat or Jetty. Web containers support Java Servlet and JavaServer Page execution. Java EE containers support more functions, such as server load distribution.
Most modern Java Web frameworks are servlet-based. Popular Java Web frameworks include GWT, JavaServer Faces, Struts, and Spring frameworks. These Web frameworks usually require at least Web containers.
Java Web applications are a collection of Dynamic resources (such as Servlet, JavaServer pages, Java classes, jar) and static resources (HTML pages and images. Java Web applications can be deployed as WAR (Web ARchive) files.
The WAR file is a zip file that contains the complete content of the corresponding Web application.
Standard Java technology is specified by Java Community Process (JCP http://jcp.org. Including:
Servlet: extends "HttpServlet" to the Java class in the Web container that responds to HTTP requests. For the latest official version of Servlet 3.1, see https://en.wikipedia.org/wiki/java_servlet.
The assumerver Page JSP is a file containing HTML and Java code. At the first execution, web cotainer compiles JSP into a servlet. The latest version is 2.2. For more information, see https://en.wikipedia.org/wiki/javaserver_pages.
JavaServer Pages Standard Tag Library (JSTL) encapsulates common core functions in the form of tags. The current version is 1.2.1. For more information, see https://en.wikipedia.org/wiki/javaserver_pages_standard_tag_library.
Non-standard Java Web development. For example, GWT supports Java Development and is compiled into JavaScript.
Client operations
Java provides common and lightweight HTTP client APIs to access resources over HTTP or HTTPS. The main types of access to the Internet are java.net. URL and java.net. HttpURLConnection.
The URL class can point to network resources, while the HttpURLConnection class can be used to access network resources. The HttpURLConnection class can create InputStream (like reading local files ).
In the latest version of HttpURLConnection, transparent response compression (via header: Accept-Encoding: gzip) is supported ).
For example: 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(); }}
Let's see how python is implemented:
>>> import requests>>> requests.get("http://automationtesting.sinaapp.com").text
The two rows are done. It can be seen that web access to Java is rather clumsy.
The Javadoc of the HttpURLConnection class. We recommend that you do not reuse HttpURLConnection. In this case, different threads cannot be shared if HttpURLConnection is used in this case.
Next we will put the download in a method:
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(); } } } }}
Return code obtained from the webpage
The most important HTML return code is:
Return Code |
Explaination |
200 |
OK |
301 |
Permanent redirect to another webpage |
400 |
Bad request |
404 |
Not found |
The following code accesses the webpage and prints the HTML access return code.
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 implementation is as follows:
>>> import requests>>> result = requests.get("http://automationtesting.sinaapp.com")>>> result.status_code200
Internet media type (MIME, also known as Content-type) is the type of network resources. MIME is a file format on the Internet, which consists of two parts. For HTML pages, the content type is "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 implementation is as follows:
>>> import requests>>> result = requests.get("http://automationtesting.sinaapp.com")>>> result.headers['content-type']'text/html;charset=utf-8'