Introduction to Java Web development

Source: Internet
Author: User

From: Http://www.cnblogs.com/pythontesting/p/4963021.htmlJava Introduction to Web development

Java is a good support for web development, on the desktop Eclipse RCP is not successful, Java is mainly used on the server side, and Python is the most important web background development language.

Java Web applications typically do not run directly on the server, but within the Web container. The runtime environment provided by the container, which provides the JVM (Java Virtual Machine) to run the native Java application. The container itself is also running in the JVM.

Typically Java is divided into two containers: a Web container and a Java EE container. A typical web container is tomcat or jetty. The Web container supports the execution of Java servlet and JavaServer page. The Java EE container supports more features, such as the distribution of server loads.

Most modern Java web Frameworks are servlet-based. The popular Java web Framework has gwt,javaserver faces,struts and spring frameworks. These web frameworks typically require at least a web container.

A Java Web application is a collection of dynamic resources (such as Servlet,javaserver pages, Java classes, jars), and static resources (HTML pages and pictures). Java Web applications can be deployed as war (Web ARchive) files.

The war file is a zip file that contains the full contents of the corresponding Web application.


Standard Java technology is specified by the Java Community Process (JCP http://jcp.org/). Contains the following:

Servlet: Extended "HttpServlet" in the Web container in response to an HTTP request for JAV?? Class A. The latest official version of Servlet 3.1, see Https://en.wikipedia.org/wiki/Java_servlet.

JavaServer pages (JavaServer page JSP) are files that contain HTML and Java code. On first execution, the Web Cotainer compiles the JSP into a servlet. The latest version is now 2.2. See Https://en.wikipedia.org/wiki/JavaServer_Pages.

The JavaServer Pages standard tag Library (JSTL) encapsulates Common Core functionality in the form of labels. The current version is 1.2.1???, see Https://en.wikipedia.org/wiki/JavaServer_Pages_Standard_Tag_Library.

Non-standard Java web development. For example, GWT supports Java development and compiles to JavaScript.

Client actions

Java provides a common, lightweight HTTP client API for accessing resources through the HTTP or HTTPS protocol. The main class of access to the Internet class is the Java.net.URL class and the Java.net.HttpURLConnection class.

The URL class can point to network resources, and HttpURLConnection classes can be used to access network resources. The HttpURLConnection class can create inputstream (like reading local files).

The latest version of HttpURLConnection supports transparent response compression (via header: Accept-encoding:gzip).

For example visit: 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;publicClassdownloadwebpageexample {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 outputforin new StringBuilder (); try (BufferedReader reader = new BufferedReader (new InputStreamReader (in "" while ((nextline = Reader.readline ())!= null) { Sb.append (nextline); }} catch (IOException e) {e.printstacktrace ();} return sb.tostring ();}      

See how Python is implemented:

Import requests>>> requests.get ("http://automationtesting.sinaapp.com"). Text  

2 lines are done, visible Web access to this piece of Java is rather clumsy.

HttpURLConnection class of Javadoc, it is recommended not to reuse httpurlconnection. In case such use of httpurlconnection does not have threading problems, it cannot be shared between different threads.

Here we put the download in one way:

ImportJava.io.BufferedReader;ImportJava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.net.URL;PublicClassReadwebpage {PublicStaticvoidMain (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 trycatch (IOException e) {e.printstacktrace ();}}} }} 

The return code obtained from the Web page


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 Web page and prints the HTML access return code.

ImportJava.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 implementations are as follows:

>>> Import requests>>> result = Requests.get ("http://automationtesting.sinaapp.com") >>> result.status_code200

The Internet media type (MIME, aka Content-type) is defined as the type of the network resource. The MIME type is a file format on the Internet and 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 implementations are as follows:

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

Introduction to Java Web development

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.