Reflection on learning the J2EE framework

Source: Internet
Author: User

Nowadays, many beginners and programmers are eager to learn the typical Web development frameworks: Struts2, Spring, and Hibernate. It seems that these frameworks have become the only factual standard for a person who is proficient in Java, whether to write J2EE programs, and the necessary basis for finding a job.

However, if you ask these programmers during the interview, why do you want to learn these frameworks? What is the essence of these frameworks? Few people seem to be able to give me very satisfactory answers. Because they are learning for the purpose of learning, learning for the purpose of work, rather than truly understanding a framework. In fact, everyone should think about the question: why should we learn the framework? What does the framework bring to me? Next, we use login as the simplest example to see how we write Web programs in different ages.

Later, we gave up writing logic on the page.

Later, more and more programs were written. We found that there were many problems in writing Java code in HTML code to complete logic:

1. the Java code is messy because it is mixed in an HTML environment and has poor readability. A JSP file may sometimes become dozens or even hundreds of kb. Looking for a piece of logic, often cannot be located.

2. I am very confused when writing code. I don't know where the code should be written, or whether someone else has implemented similar functions or where to reference them.

3. A demand suddenly changes. As a result, everyone starts to replace the entire process with caution for fear of changing others' logic.

4. The logical processing program needs to maintain its own lifecycle, which is not supported by many modules such as database transactions and logs.

At this time, if there is a product that can extract the Java code on the page, it would be nice to make the page as few Java code as possible. As a result, many people began to use servlet to process those business logic.

  1. Public class LoginServlet extends HttpServlet {
  2. /* (Non-Javadoc)
  3. * @ See javax. servlet. http. HttpServlet # doPost (javax. servlet. http. HttpServletRequest, javax. servlet. http. HttpServletResponse)
  4. */
  5. @ Override
  6. Protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  7. String message = null;
  8. RequestDispatcher dispatcher = req. getRequestDispatcher ("/result. jsp ");
  9. String name = req. getParameter ("name ");
  10. String password = req. getParameter ("password ");
  12. UserHandler userHandler = new UserHandler ();
  13. If (userHandler. authenticate (name, password )){
  14. Message = "congratulations, login successful ";
  15. } Else {
  16. Message = "sorry, Logon Failed ";
  17. }
  18. Req. setAttribute ("message", message );
  19. Dispatcher. forward (req, resp );
  20. }
  21. }

Here, we need to configure the url request relationship for this servlet in web. xml.

  1. <servlet>     
  2.   <servlet-name>Login</servlet-name>     
  3.     <servlet-class>     
  4.       com.demo2do.servlet.LoginServlet     
  5.   </servlet-class>     
  6. </servlet>     
  7. <servlet-mapping>     
  8.   <servlet-name>Login</servlet-name>     
  9.   <url-pattern>     
  10.     /Login     
  11.   </url-pattern>     
  12. </servlet-mapping> 

After code refactoring, we found that our workload was not reduced, but the code was moved from JSP to Servlet, which made the entire process a little clearer. However, what is the price we pay for being so clean? Configure a url request for each servlet in web. xml!

We wrote programs like this many years ago.

Many years ago, it was a poor age. If we wanted to use Java to do some dynamic interaction functions on the web page. Many people will tell you a technology called JSP. When I was still confused about Java, someone told me that JSP is a good thing. It can write Java code in HTML code to complete the logic.

  1. <%
  2. String name = request. getParameter ("name ");
  3. String password = request. getParameter ("password ");
  4. UserHandler userHandler = new UserHandler ();
  5. If (userHandler. authenticate (name, password )){
  6. %>
  7. <P> congratulations, logon successful </p>
  8. <%
  9. } Else {
  10. %>
  11. <P> sorry, Logon Failed </p>
  12. <%
  13. }
  14. %>

As a JSP, it can receive and process login requests sent from other JSPs. In this way, we can complete the logic without any additional configuration files or any framework help.

Later, the framework appeared.

With the development of the times, it is found that simple JSP and Servlet are hard to meet the requirements of laziness. As a result, people began to try to summarize some common Java classes to solve the problems encountered during Web development. At this time, a framework called struts was born. It is very advanced in implementing the MVC model and has become the gospel of many programmers.

I will not post the struts code example. You can search for a bunch of struts code on the Internet. To a certain extent, struts can solve the issue of Responsibility Assignment in web development and separate display from logic. However, for a long period of time, programmers who use struts often cannot determine what the web framework needs to do for us, and what functions do we need to accomplish?

What do we need?

After reviewing the history of code writing, let's look back at what we really want?

Whether using JSP, Struts1, or Struts2, we need at least some necessary elements. If these elements are not available, I may not know what the program will write ):

1. Data

In this example, name and password are used. They constitute the core carrier of the program. In fact, we often have a User class to encapsulate name and password, which will make our program more OO. In any case, data is interspersed across the program and becomes the core of the program.

2. Page display

In this example, login. jsp is used. Without this page, there is no way to talk about all requests, verification, and error presentations. On the page, we need to use HTML to present all the data we need to present. At the same time, we also need to complete certain page logic, such as error display and Branch judgment.

3. Locations for handling specific services

Here, the locations for processing specific services are not the same at different stages. Originally, JSP and Servlet were used, and then Struts1 or Struts2 Action was used.

The elements that must appear above have been given different forms of expression in different ages. Some are bound by the times, and their forms are very backward, and some are no longer used. But with these external forms opened, we can find that this is not the well-known MVC?


Page display -- View

Place for processing specific business-Control

Therefore, the framework is not important and its concept is king. As long as you can deeply understand the concept of MVC, the framework is just a jar package for you.

The concept of MVC is actually so simple. These concepts have already penetrated into our hearts, and what we lack is to explore their essence. Let's take a look at the figure below, which shows the MVC model that has been popular for many years:

In this figure, the three MVC frameworks perform their respective duties, and the structure is clear and clear. However, I think this figure ignores a problem, that is, the data is dynamic. Once the data moves on the View and Control layers, there will be many problems:

1. Data is transferred from the View layer to the Control layer. How can we convert flat strings into Java objects.

2. How to conveniently verify the data format and content when data is transferred from the View layer to the Control layer?

3. Data is transmitted from the Control layer to the View layer, and Java objects are displayed in various forms on the page.

4. If you try to send data requests from the View layer to the Control layer, how do you know which class and method you want to call? How can an Http request establish a relationship with the Control-layer Java code?

In addition, the Control layer does not seem as simple as imagined, because it acts as a controller and at least needs to handle the following problems:

1. What should we do if an exception occurs in the logic processing program as the facade that calls the logic processing program?

2. What kind of processing is required for the logic processing results to meet the rich foreground display needs?

One problem after another is based on the mining of the basic concepts of MVC. Therefore, we need to solve these problems one by one when writing programs. Speaking of this, the questions raised at the beginning of this article should have answers:The framework was created to solve one problem after another in Web development. Different frameworks are designed to solve different problems, but for programmers, they are just jar packages. The comments on the advantages and disadvantages of the Framework fully depend on their comments on the degree of solution and the elegance of the solution. Therefore, we should never learn frameworks for the purpose of learning frameworks, but to solve problems. This is the correct way for programmers to learn.

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: 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.