To be a javaweb programmer, the foundation of Java Web is very important, now there are a variety of mature design frameworks such as jquery, Spring, Struts, Mybatis, to encapsulate the complex and common logic of the Java Web Foundation, reduce the Programmer's Code volume, Improved coding efficiency, but these frameworks are very detrimental to our understanding of how the Java Web bottom works, not even the HTML (hypertext Markuplanguage), HTTP (hypertext Transfer Protocol), URL ( Uniform Resource Locator) and the question of text encoding. When we are accustomed to using these frameworks without exploring the principles of their implementation, our level of development is imprisoned by these frameworks, unable to learn the core, and cannot innovate.
I am a small program ape that is trapped by these frameworks. As soon as you learn something, you will soon be able to get started in the project and generate labor. But the more you write, the more uncomfortable it is, "Why write this?" "," STRUTS2 is how the JSP page, Controller, model linked together? "," How the front desk request is received accurately in the background? What is a "," servlet? ”...... If you have not thought about these issues, you have been imprisoned by the current work, programming only to achieve a certain function, almost no improvement in technology.
After recognizing these, I started the Java Web Foundation learning, the following is my study notes, for your reference.
About HTML
This presumably everyone knows what is, HTML is to sign (TAG) way to define the file structure, is the basis of the building interface, is placed on the server for client access. The browser will draw the page's appearance based on these tags. The following Web site has HTML tutorials: http://www.w3school.com.cn/html/
About URLs, URIs, urns
HTML is placed on the server, how do clients access HTML? Some people say "through the url", "Through the URI", may say that the person does not know what is the URL, URI, the following is the full spell of the three abbreviations:
- URL: Uniform Resource locator--Unified Resource Locator
- URI: Uniform Resource identifier--Unified Resource Identity
- URN: Uniform Resource name-—— Uniform Resource Names
URL represents the address information of the resource, its structure is < protocol >:< specific Protocol section, such as http://www.baidu.com, ftp://192.168.1.1/new Folder/test.txt
A urn represents a unique name for a resource, such as the urn of my computer as "Lenovo-t450s-custom-s3-123456789", which identifies the computer in the world, and now, for example, a computer with the same brand configuration as mine may be called " Lenovo-t450s-custom-s3-987654321 ".
A URI is a string that identifies the name of an Internet resource. This type of identification allows users to interact with any resource, including local and internet, through a specific protocol. The URL is one way to use the URI, in addition, the URI can also use specific reserved characters, different encoding to specify the resources to be accessed, such as to send a message to someone, you can build a uri:mailto:[email protected].
Dynamic Web pages, static Web pages
Static Web pages are pure HTML, and the contents of the pages do not change with time and space.
Dynamic Web page can be based on the client's request parameters, time and space, so that the Web page on different clients to see the content is different. (This is the most stupid explanation)
How does a Dynamic Web page move? is the server according to the client's request parameters and other information, through the execution of the program to generate response content, and then return the content to the client, because the execution of the program is a change, so the resulting response content is also changeable.
What are the execution programs on this server? Currently, there are CGI, PHP, ASP, servlet/jsp to deal with Dynamic Web pages.
It is noted that servlet/jsp is written together, which means that these two are a thing, and you can know why by the knowledge behind them.
servlet/jsp Introduction
Finally said Servlet/jsp, what is a servlet? What is a JSP?
We know that Java programs are made up of. class files, and these. class files are run in the Jvm--java virtual machine, so when we write Java code, we know that the code we write can eventually be parsed and used by the JVM. Sometimes it is also necessary to consider how the JVM manages objects in Java programs.
To make an analogy: servlet/jsp runs on an HTTP server, and a server containing a Web container is called an HTTP server, so servlet/jsp actually runs in the Web container, which is the bridge between the client and servlet/jsp. The servlet/jsp we write can eventually be parsed and used by the Web container, and sometimes the JVM can be considered for managing various objects in the servlet/jsp.
Important Concept: Web container
Web containers are defined by a large number of specifications, standards, as long as they are written according to these specifications/standards, it can achieve a specific function (generate static/Dynamic Web pages, achieve various effects, complete communication between the client and the HTTP server, etc.). And Servlet/jsp is one of the technologies that implements this large stack of norms and standards.
You must have heard of the famous Apache Tomcat (http://tomcat.apache.org), which is the Web container.
So how does a Web container handle a request/response? Here is an example:
- The browser makes an HTTP request to the Web server (for example, to access a URL)
- The HTTP server receives an HTTP request, gives the request to the Web container processing, and the container resolves some information about the HTTP request, creating various objects (such as HttpServletRequest, HttpServletResponse, httpsession, etc.)
- The container assigns the next process to the specified servlet, based on the requested URL
- The servlet determines what to do with the information that has been encapsulated by the request object (HttpServletRequest) and then creates the response through the corresponding object (HttpServletResponse) (a critical step that developers can write various business logic)
- The Web container notifies the HTTP server that the response has been created and is converted to an HTTP response, and returns the response back to the browser.
The relationship between Servlet and JSP
Finally, you may be eager to write a JSP page with HTML to look at the effect, but in fact, JSP is not just written in HTML, all JSP pages, will eventually be compiled into the Web container ". Class" file, and then loaded into the container. Why is the ". Class" File? Because the Web container interprets the JSP page first as a ". Java" file. This ". Java" File is inherited by HttpServlet directly or indirectly (directly or indirectly), so that the ". Java" file becomes a servlet, so whether it's a servlet or a JSP page, The final service is a servlet instance. To master JSP, you must first have a certain understanding of the servlet. Let's take a look at how a servlet is written, on the code!
<span style= "Font-family:microsoft yahei;font-size:14px;" >package com.web;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;p ublic class SimpleServlet Extends HttpServlet {@Overridepublic void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {response.setcontenttype ("text/html"); PrintWriter out = Response.getwriter (); Out.println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > ") out.println (" <HTML> "); Out.println (" < Head><title>a servlet</title>
Through the above procedure, we notice that the servlet class must inherit from HTTPSERVLT (direct inheritance), and then if you want to output HTML, you must pass the Java input/output function, and you can generate dynamic HTML content (for example, the date above) through Java code.
Then look at a JSP page that can achieve the same effect:
<span style= "Font-family:microsoft yahei;font-size:14px;" ><%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
The above two pieces of code can achieve the same effect, but it is easier to write with JSP. Servlet is the implementation of the output and logic of the page through Java code, JSP is the HTML to populate the Java code to implement interface output and logic processing. How does the JSP implement the same functionality as the servlet? What is the underlying operating principle?
First we publish the project in Tomcat (Web container) and start Tomcat, then visit the following JSP page, then go to the Tomcat root directory, find the Work/catalina/localhost directory and enter, find your published project directory and enter, In the inside you will find that the JSP page you write is compiled into Xxx_jsp.class, Xxx_jsp.java,xxx is the name of your JSP page, we open the Xxx_jsp.java:
See, this is what the Web container does for us: it translates the JSP page into a ". Java" file, which inherits the Httpjspbase, and Httpjspbase inherits from HttpServlet (so it's indirectly inherited), And the HTML output is the same as before.
To sum up, servlets and JSPs are actually two sides of the thing, know this, for our future programming career will have a great help, at least when we encounter problems, can be more than a layer of thinking to solve.
The concept of Mvc/model 2
Speaking of which, although the web framework is based on our servlet/jsp, I think there is a very important basic knowledge, which is the concept of MVC and Model 2. It is the foundation of most web frameworks, original aim, understanding MVC and Model 2, allows us to further understand the implementation principle of a web framework.
From the above program you may notice a particularly awkward place (if your code is strong), that is, whether it is a servlet or JSP, the Java code and HTML will be mixed together, this is very bad, on the one hand the code is not easy to understand, on the other hand is not easy to maintain, For the future team work is also very big trouble.
In response to this problem, I do not know which one of the elder ancestors, through the transformation of the MVC model, to create a suitable MODEL2 model for Web use.
Let's first look at what MVC is, MVC is the abbreviation for model, view, controller, usually translated models, views, controllers. The first MVC model was designed to implement the different presentation and update of data in a desktop application, and for a detailed introduction to MVC Click here, here are just a few things we need to be concerned about:
- The model will not have a picture-related program code
- View is responsible for screen-related logic
- The controller knows which models must be called by an operation
- after the model is updated, you can notify the view to do the next step-- Querying model State
Some people think that the assignment of responsibilities such as MVC can be applied to Web applications:
- The view section can be implemented by Web pages.
- Data access and business logic on the server can be handled by the model
- The controller receives a request from the browser, decides which models to call to process and returns the results to the browser
However, there is a very important process, that is the above bold yellow 4th: After the model is updated, you cannot directly tell the view what to do next. The reason for this is that the Web is based on HTTP, must be based on the request/response model, no request will not respond, the server will not be "active drip" notification browser to make changes to the view.
Based on this problem, our elder ancestors proposed a solution called Model 2, which interacts as follows:
I'm not going to say it in detail here, look at the above to find that the Model 2 is based on MVC, HTTP request interception, then the controller first obtains the model state from the model, then notifies the view to query the model state, and finally returns the view to the HTTP response. After understanding this, you can understand why we have a loading process when we visit a website, because the HTTP request to the response time, the server did a lot of things ah!
The purpose of the so-called design pattern is to separate the changes and the invariant, using MVC to make the project structure a lot clearer, and it seems to be easier to understand.
Java EE
Finally put a lot of people may not know but also very important knowledge.
Java represents an open platform, depending on the domain, is divided into Java SE (Java platform,standard Edition), Java ME (Java platform,micro Edition), Java EE (Java Platform,enterprise Edition).
The Java SE book is a beginner of Java's basic version, addressing standard desktop application requirements. Java me for micro device phone PDA what's. Java EE is a lot more, can be a comprehensive solution to all areas of the problem, servlet/jsp belongs to Java EE.
Java Web Foundation (HTML, servlet/jsp)