JSP technology as a once hot technology, in recent years has indeed become less and less used, this article will take you a piece of explore JSP from life to death.

The birth of JSP technology

A long, long time ago, when all of our development was done with servlets, what was this servlet? Let’s get to know them:

Servlet A server-side program written in the Java language. The main function is to interact with the browser and generate page display.


Look like this:

   public class HelloWorld extends HttpServlet {  

        public void doGet(HttpServletRequest request, HttpServletResponse response)  

                throws ServletException, IOException 
{  

            response.setContentType("text/html");  

            PrintWriter out = response.getWriter();  

            out.println("<html>");  

            out.println("<head>");  

            out.println("<title>Hello World</title>");  

            out.println("</head>");  

            out.println("<body>");  

            out.println("

Hello World!

"
);  

            out.println("</body>");  

            out.println("</html>");  

        } 

    }  

Copy the code

We can see that the front end displays pages that require our servlet to generate tags one by one. If a page is extremely complex, with thousands of lines of code at a time, the servlet will be inefficient. And the entire servlet code can be bloated and unreadable.

What to do? Sun recognized this problem early on, and led a group of companies to create a new technology that could generate HTML on the fly, and JSP was born soon after. This effectively solves the problem with the servlet above.


Second, JSP development

Since JSP technology can solve the problem just described in the servlet code, let’s take a look at how: Here’s a small example of a front-end JSP’s ability to send a request book page to a server servlet.

First let’s take a look at servlets:

public class List_book extends HttpServlet {          

     public void doGet(HttpServletRequest request, HttpServletResponse response)  

             throws ServletException, IOException 
{  

        // Declare an ArrayList. To store data in the Book class

         ArrayList<Book> list = new ArrayList<Book>(); 

         for(int i=0; i<10; i++){

                      Book book = new Book();  

         book.setName(res.getString("name"+i));  

         book.setAuthor(res.getString("author"+i));  

         list.add(book);  

         }

         // Send the list data to the.jap file

         request.getRequestDispatcher("ListBook.jsp").forward(request, response);  

     }  

}  

Copy the code

We will see that the servlet now has no HTML code at all. We just need to hand the data to the JSP. At this point our page display to JSP to do. Now let’s take a look at what the JSP looks like:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"% >

<%String path = request.getContextPath(); % >

<! DOCTYPE HTML PUBLIC"- / / / / W3C DTD HTML 4.01 Transitional / / EN">

<html>  

 <head>   

   <meta http-equiv="pragma" content="no-cache"

   <meta http-equiv="description" content="This is my page"

   <script></script> 

  </head>  

  <body>     

   <% ArrayList list = (ArrayList) request.getAttribute("list"); % >

   <h2 align = "center"> Book list </h2>

   <table border = 1px align = "center">   

<tr><th> Book name </th><th> Book author </th></tr>

<! Continue using the JSP statement to loop data into the Book entity class stored in the list -->

The < %

    for(int i = 0; i<list.size(); i++){

     Book book =(Book) 

list.get(i); % >

     <tr><th><%=book.getName() %></th><th><%=book.getAuthor()%></th><tr> 

     }

% >

   </table>  

  </body>

 </html>

Copy the code

This is JSP, we can write some Java code in the HTML page. For us programmers, we only need to use HTML and CSS to write some tags to display the static pages in development, and for those dynamic parts we can use Java code.

So JSPS and servlets are not this kind of collaborative relationship, what is the essential difference?

In fact, JSP is only a special form of servlet, each JSP page is a servlet instance, in popular terms: JSP is a servlet, but the servlet has stripped off some business functions to give or form JSP. You get the idea. When we compile our project, we compile JSPS into servlets.

As you can see, it’s fine, the market validates it, and JSP technology catches on pretty quickly, but as time goes on and businesses get more complex, JSPS start to fall behind.

JSP crisis

Let’s start with a dialogue scene:

Java programmer: Having finally written the functionality, it’s time to show it in the interface.

Front-end programmer: you finished writing the function, I have no data, in the page what can not display

Java programmer: I have written the data, you can call XX method in JSP can get,

Front-end programmer: I already wrote this method in JSP, why did you write it yourself?

As a result, the endless quarrel continues.


This is the downside of JSP, why? We can sum it up:

(1) Dynamic and static resources are put together. Once the server is in a state, the front and back ends together, resulting in poor user experience.

(2) Once there is a problem with JSP, you need to open a front-end and back-end personnel to analyze and solve, low efficiency.

(3) JSP can not use nginx, etc.

(4) JSP page complex, difficult to modify.

(5) the first time to load JSP needs to be compiled into servlet, time is long, and when the volume of business is large, JSP burden is too big.

(6) JSP for developers is simply a lingering pain, too difficult!!

In view of these shortcomings, another set of mechanisms emerged, namely front – end separation. What is front end separation?

The separation of the front and back ends means that the backend engineer only focuses on the development of the back-end pages and does not deal with the front-end problems. Front-end engineers focus on their own page development. When data interaction is needed, the two have an interface document.


In this way, the ideological framework quickly became popular, which is the real reason why JSP fell apart. Since then, Java has shifted from JSP to restful architecture, and springMCV has become popular and gradually dominates the market. What are the advantages of front and rear end separation? Let’s sum it up:

(1) Dynamic and static resources are stored separately.

(2) The front end or back end can be quickly located when a bug occurs.

(3) Support nginx. Excellent at high concurrency.

(4) direct request page, do not compile, speed and efficiency are brought up.

(5) from now on the front end and back end is a loving family !!!!


Four, JSP curtain

And with the passage of time, JSP era basically gone forever, because we have entered the mobile Internet era, this time the client is not the front end of the page, but also including mobile phones, cars, television and so on a variety of devices, in this case, the front and back end must be separated. JSP is basically dead for good.

Technology is always constantly developing and improving, and only technological innovation can bring social progress.