Simple understanding of JavaEE

This article is the study notes in learning and J2EE software engineering courses, aimed at understanding the role of some of the major components of the Java EE Web applications from the general concept.

Java EE

FIG refined described on the MVC model and some how Java EE components distributed over a Web application, illustrated hereinafter referred to refer to FIG.

Web Applications

Before everything started to understand what a Web application, the drawings, the left-most users, the Web application is a set of web pages on the browser, this group of pages according to their own operations, including clicks, typing, etc., to dynamically display of information.
Dynamic means that here, the user's operation is submitted to the back-end servers for some kind of treatment, after the server returns some information displayed on the web browser has been updated, so as to achieve dynamic effects, so that a simple the static HTML pages and Web applications can not be considered, even if the page is CSS and JavaScript add some dynamic visual effects.
Common shopping sites such as Taobao, belongs to a Web application, the user clicks on the site continue to submit the data to the back-end server, and then dynamically update the product information by browsing and, finally, type a password to complete the hand chop.
So for the user, the background is all black box, users do not need any care, as long as the commit operation, will be able to get results.
The following Java EE task is to understand how to make the black box up and running.

MVC model

Can imagine a situation where users want to buy a guitar, so can refresh Taobao search box type "guitar" and then click on search pages and look forward to demonstrating full page of goods for their own pick, then request after submitting to the back-end server, the server only runs a helloworld.java procedures to deal with all of these operations (black box that is only a .java file), then what helloworld.java need to do it, we must first resolve submit incoming data, using a specific method to analyze the meaning of these data, it was found that would like to request a new page lists guitar goods, then connect to the database, data related to various requests guitar, then use a specific method to generate a new page, and finally back to the browser, if a user submits a request to another, you also need other specific ways to deal with, once for each user of any operation, helloworld.java to make all these operations and then re again.
We can see this operation is very complicated, extremely inefficient, coupling is very high, the whole process whether it is requested by the user, business logic or page structure, as long as a few changes, helloworld.java file will be rewritten, completely unable to maintain .
MVC model can be a good deal with this problem, compared with the previous single java file, the MVC black box divided into three parts:

  • Controller:
    As shown, the user submits a request arriving at a rear end that is the first station Controller, his original request for the HTTP Get or Post analyzing give real user data, and then processed to the Model, so Model the problem completely do not care how data analysis is also responsible for the Model return to encapsulate the data submitted to the View, let View to update the user's page.
  • Model:
    Model from the Controller to get at the pure data, according to concentrate on business logic to process the data, interact with the database, the final processed data back to the Controller if necessary.
  • View:
    View Controller acquired from the updated data, the data used to dynamically update the user interface to see.

JSP

MVC in the View can use JSP pages to dynamically update the user sees the web interface, can be understood as a JSP page can display both regular HTML pages, but also can execute Java code page, if it does not execute Java code that effects and ordinary HTML identical.
JSP can write Java code to extract the current page in HTML node, and then to update these nodes based on new information received from the Controller to enable them to re-display, so that you do not always have to refresh the entire page, only need to update part of the page that appears change

Servlet

In MVC Controller using servlet to process information, a servlet class is actually implemented in a .java file, such as call ControllerServlet class, class two major rewrite of the method, a method is doGet, doPost is a method, respectively submit to handle the front-end incoming Get and Post request, the data parsing out these requests to the Model to deal with, and the results submitted to the JSP Model returned to update the web pages, the servlet will not do any logical processing of the data.

EJB

Before understanding the need to first understand JavaBean EJB, a JavaBean is actually a Java class that implements some major getter and setter methods, to accept some data setter method, then some processing, returns with getter methods, such as the following code

public class PersonBean implements java.io.Serializable {

private String name = null;

private double score = 100;

public String getName() {
return name;
}

public void setName(final String value) {
name = value;
}

public double getScore() {
return score;
}

public void setScore(final double value) {
score = value;
}}

This PersonBean class that is a the JavaBean, which can accept the name and score, and returns the name and score, of course, may score do some logic in setScore method, for example, score = sqrt (score) * 10 , expressed student scores opening then take the square root of 10, and so on.
You can use it like this

public class TestPersonBean {

public static void main(String[] args) {
PersonBean person = new PersonBean();

person.setName("张三");
person.setScore(88);

System.out.print(person.getName());
System.out.println(person.getScore());
}}

This class has a strong reusability, users can get to know a Bean after it set some values, and then get to the value you want, it played a special JavaBean name, Bean is available in Java way of talking to reuse components of it, it should be realized that this is a reusable Java class when it comes to certain Bean.
And EJB, namely Enterprise JavaBean, by definition is an extension of JavaBean, to achieve more complex functions.
In MVC model, EJB Model in the forefront, to accept parsed data Controller, EJB comprising three categories: Session (Session) Bean, entity (Entity) Bean, driven message (Message Driven) Bean.

  • Session Bean:
    Session Bean's Session Bean basically refers to the user's session, such as the user to complete the selection of items to add to your cart, then settled the action, then the shopping cart you can use a so-called stateful Session Bean to maintain, with to continue to focus on the user, to see if there are new items added in, what products or have been deleted, cart, when the product is added or deleted, the Session Bean runs related methods to deal with these actions, when users choose completed, after checking through the shopping cart by clicking the billing, stateful session Bean this is cleared up, it has completed the session with the user, and in the next billing pages, you can enable a stateless session Bean, stateless means users generally only one request submitted by such input password, and then bills the relevant information such as price, delivery address, contains articles, etc., is a one-time submission to the Session Bean, then the user went to dinner, the Session Bean users will not sustained attention away to deal with these billing information.
  • Bean the Entity:
    the Entity Bean is actually a database tables in a row corresponding to a Bean, for indirect operation of the database, JPA now generally used instead.
  • Driven Bean the Message:
    the MDB can be understood as a message listener, news sources from JMS, should be aware of this message is not the kind of information refers to long-winded, but usually short, frequent short messages, such as a user clicks subscribed magazine subscription then this action will switch to the corresponding messages to JMS, the related action MDB listens to the news before.

ETC.

Imagine a news portal (this is obviously a Web application), after it released the news every day, there are many other news sites also want to get the news, such as some of the tech news site will immediately focus on major news portals tech sector, so among these Web applications need to pass messages to each other, as a set of API and JMS messaging middleware played solve the problem of inter-application communication, when a Web application uses the JMS time, it is conceivable to be the Web application in their own homes the door put a mailbox, the message they are willing to open every day, just on the mailbox, you want people to be free to access, so that he did not care who give the message that those who do not need access to messages greeting messages can be directly removed, because the mail message are willing to be open, and that if he wanted to see someone else's message, go to the mailbox to see each other, an application wants to focus on a number of other applications news as long as these applications can subscribe to JMS-mail, which is JMS publish / subscribe model.
JMS also point to point model, National Chiao Tung University school site and at the National Chiao Tung University Library website to share information, and this information do not want to see other Web applications, then you can use point to point model, namely, to maintain a message queue only between the two , the library has a new message, put it in the message queue, and then go do your own, free to view the queue at the school, access to messages in the queue, waiting for the other parties do not have to send or receive send and receive messages, the message is asynchronous.
Before the MDB is to listen for JMS messaging mailbox or JMS message queue Bean.

JPA

Before understanding JPA need to know what is the physical object, often need to interact with the database, such as teacher queries a student performance on the Senate website in the EJB processing data when the time, EJB need to view the database corresponding to the student's name Student ID students in this table corresponding to the line, and then take back to score this column, if nine physics and chemistry teacher Yushu Wai history Lands students have when you want to modify the results, EJB and need to check and to modify nine times, also there are many other cases, EJB might need a row of the table frequently changes the database, if the investigation is obviously a lot of trouble each time, the so-called entity is actually the one line data such as a database of mapping Java objects, create such a objects can after it has been operating until then release when not to, and this Java object is called a physical object, so the meaning is actually an entity that line data corresponding to the entity, such as the one checked results students, JPA is a specification that details the entity classes and data lines in the end how mapping.
As shown therein, in order to concentrate Session Bean data processing, and generally do not directly contact database, but access to the data entity class JPA specification, Session Bean since this can be a good business logic underlying and more the database details decoupled.

JDBC connection pool and JDBC resource

Web applications in the upper layer of parts, such as JSP part, sometimes you want to interact with the database directly without going through a complicated EJB, JSP envisaged in the Java code directly using JDBC to access the database, you need to enter the address of the database initialization JDBC connection , port, user name, password, and so closely associated with the parameters of the database itself, if the database has changed the day parameter, such as the replacement address or password code that the JSP all had changed, and because at the front end JSP changes very frequently, direct database will result in frequent switching database, JDBC connection with the loss of a great performance, so this is a way to call the database difficult to maintain.
JDBC connection pool that is intermediate between the JSP and a database, it can be thought of as a place for JDBC connection pool, Web application startup, the connection pool a sufficient amount of requests connection to the database and placed in the pool As long as one or more removed from the connection pool for when JSP access, so first of all solve the problem of coupling JSP and database, JSP simply do not care address database, port, user name, password, what is it access to data in the database Article connect to, run out after the connection returned to the connection pool to access other JSP again, when the parameters of the database changes, only need to modify the parameters to the connection pool, front-end JSP do not need to make any changes, Further, the connection pool adequate connection, to access JSP enough, the switch is greatly reduced number database to improve performance.
JDBC resource is the specific use of the JSP JDBC connection pool connection, JSP used JDBC connection resource by reference.

Guess you like

Origin www.cnblogs.com/zillyrex/p/11802836.html