Is it still necessary to learn JSP in 2020?, Necessary magic weapon to BAT

Now the question comes. Is JSP really old in 2020? Yes, it's really old

Now the question comes again. Why is the technology that has been defined as "old" a few years ago still hot by 2020? Every year, someone still asks, "do you still need to learn jsp?". I think the reason is also very simple: JSP was really used before!

When I first learned Java, I often heard that JSP and PHP can write dynamic web pages - "my teacher".

When we look for relevant learning materials, we find JSP everywhere, which will give me a feeling that if we don't understand JSP, we can't continue to learn at all.

If you are a novice, if you haven't learned JSP, I suggest you can still understand it. You don't need to learn all kinds of contents of JSP, but you can understand it. At least when others talk about JSP, you can know what JSP is and understand the JSP code.

One more thing: you may see the JSP code when you go to the company. Although JSP is an "old thing", we may go to the company to maintain old projects. JSP may not need you to write it yourself, but at least you can understand it, right.

The problem comes again. If JSP is an "old thing", what has replaced it? Or use the common template engine "freemaker", "Thymeleaf" and "Velocity". The usage is not much different from that of "JSP", but their performance will be better. Either the front and back ends are separated, and the back end only needs to return JSON to the front end, and the page does not need back-end management at all.

Having said so much, what I want to say is: "it's still necessary to understand JSP. It doesn't take a lot of time to know. I can take you to know JSP in this article."

What is JSP?

The full name of JSP is Java Server Pages, Java Server Pages. JSP is a text-based program, which is characterized by the common existence of HTML and Java code! JSP is a substitute to simplify the work of Servlet. It is very difficult for Servlet to output HTML. JSP replaces Servlet to output HTML.

In Tomcat blog, I mentioned that Tomcat accesses servlets when accessing any resource!, Of course, JSP is no exception! JSP itself is a Servlet. Why do I say that JSP itself is a Servlet? In fact, JSP will be compiled into HttpJspPage class when it is accessed for the first time (this class is a subclass of HttpServlet)

For example, I randomly find a JSP, and the compiled JSP looks like this:

package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.Date;

public final class _1_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {

  private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();

  private static java.util.List<String> _jspx_dependants;

  private javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.tomcat.InstanceManager _jsp_instancemanager;

  public java.util.List<String> getDependants() {
    return _jspx_dependants;
  }

  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
  }

  public void _jspDestroy() {
  }

  public void _jspService(final HttpServletRequest request, final HttpServletResponse response)
        throws java.io.IOException, ServletException {

    final PageContext pageContext;
    HttpSession session = null;
    final ServletContext application;
    final ServletConfig config;
    JspWriter out = null;
    final Object page = this;
    JspWriter _jspx_out = null;
    PageContext _jspx_page_context = null;

    try {
      response.setContentType("text/html;charset=UTF-8");
      pageContext = _jspxFactory.getPageContext(this, request, response,
                null, true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("\r\n");
      out.write("\r\n");
      out.write("<html>\r\n");
      out.write("<head>\r\n");
      out.write("    <title>Simple use JSP</title>\r\n");
      out.write("</head>\r\n");
      out.write("<body>\r\n");

    String s = "HelloWorda";
    out.println(s);

      out.write("\r\n");
      out.write("</body>\r\n");
      out.write("</html>\r\n");
    } catch (Throwable t) {
      if (!(t instanceof SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try { out.clearBuffer(); } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}

The compilation process is like this: the browser requests 1. 0 for the first time JSP, Tomcat will JSP to 1_jsp.java, and compile the file into a class file. After compiling, run the class file to respond to the browser's request.

Later visit 1 JSP will no longer recompile the JSP file, and directly call the class file to respond to the browser. Of course, if Tomcat detects that the JSP page has changed, it will recompile.

Since JSP is a Servlet, how is the HTML layout tag in JSP page sent to the browser? Let's take a look at the top 1_jsp.java source code will know. It turned out to be just using write(). In the final analysis, JSP is just a java program that encapsulates a Servlet.

out.write("\r\n");
out.write("\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("    <title>Simple use JSP</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");

Someone may also ask: how does the code server of JSP page execute? Look back at 1_jsp.java file, and the java code is directly in the service() in the class.

String s = "HelloWorda";
out.println(s);

**JSP has 9 built-in objects** Built in objects include: out, session, response, request, config, page, application, pageContext and exception.

It is important to remember that the essence of JSP is actually Servlet. Only JSP was originally designed to simplify the HTML code output by Servlet.

When to use JSP

Repeat: the essence of JSP is actually Servlet. Only JSP was originally designed to simplify the HTML code output by Servlet.

Our Java code is still written on servlets, not on JSPS. In Zhihu, I once saw a problem: "how to use JSP to connect JDBC". Obviously, we can do this, but it's not necessary.

JSP looks like an HTML, and then add a lot of Java code to it, which is abnormal and not easy to read.

Therefore, our general mode is: the data processed in the Servlet is forwarded to the JSP, and the JSP only processes a small part of the data and the pages written by the JSP itself.

For example, the following Servlet handles the data of the form, puts it in the request object, and forwards it to the JSP

//Verify whether the data in the form is legal. If not, jump back to the registered page
if(formBean.validate()==false){

  //Pass the formbean object to the registration page before jumping
  request.setAttribute("formbean", formBean);
  request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
  return;
}

The JSP obtains the data processed by the Servlet for display:

What does JSP need to learn

JSP we need to learn two pieces: JSTL and EL expressions

EL expression

**Expression Language (EL). El expression is a script enclosed by ${} to make it easier to read objects! * * el expression is mainly used to read data and display content!

Why use EL expressions? Let's first look at how to read object data without EL expression! In 1 The Session attribute is set in JSP


## summary

So far, the article has finally come to an end. To sum up, we talked about the following three parts that should be paid attention to in the process of resume making, and gave some suggestions respectively:

1.  Technical ability: first write the ability required by the position, then write the bonus ability, not the irrelevant ability;
2.  Project experience: write only star projects and follow the description STAR Law;
3.  Resume impression: resume follows three principles: clear, brief, necessary, targeted, not overseas investment;

> And the last welfare time for everyone: resume template+Java Interview questions+Popular technology series tutorial video
> **[Stamp here to get the information in the article for free](https://gitee.com/vip204888/java-p7)**

![Insert picture description here](https://img-blog.csdnimg.cn/img_convert/7db51b5a50ecb6cf00b039378381b09f.png)

![Insert picture description here](https://img-blog.csdnimg.cn/img_convert/453c87b77c0d265da83cbdf62b6441eb.png)

![Insert picture description here](https://img-blog.csdnimg.cn/img_convert/db85107d6240ffda9689df20baf1714f.png)

+Popular technology series tutorial video
> **[Stamp here to get the information in the article for free](https://gitee.com/vip204888/java-p7)**

[External chain picture transfer...(img-Q7occQk8-1628562280923)]

[External chain picture transfer...(img-bq4vSzpS-1628562280924)]

[External chain picture transfer...(img-2bAvSgT7-1628562280925)]

Keywords: Java Back-end Interview Programmer

Added by greepit on Fri, 31 Dec 2021 06:51:29 +0200