The essence of jsp
- The essence of jsp is servlet. You have deployed Java Web project with tomcat, and you know how to generate the corresponding servlet of jsp page in the work directory.
- The essence of servlet is to encapsulate socket
jsp page
<%-- Created by IntelliJ IDEA. User: wy Date: 2019/11/26 Time: Afternoon11:21 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h2>Hello</h2> <a href="login">Sign in</a> </body> </html>
Corresponding Servlrt
ublic final class index_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent, org.apache.jasper.runtime.JspSourceImports { ....... public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException { ......... 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("\n"); out.write("\n"); out.write("<html>\n"); out.write("<head>\n"); out.write(" <title>Title</title>\n"); out.write(" 111111111111\n"); out.write("</head>\n"); out.write("<body>\n"); out.write("<h2>Hello</h2>\n"); out.write("<a href=\"login\">Sign in</a>\n"); out.write("</body>\n"); out.write("</html>\n"); } catch (java.lang.Throwable t) { ..... } finally { _jspxFactory.releasePageContext(_jspx_page_context); } } }
Using jsp in SpringBoot
- Spring boot does not support jsp by default. To make spring boot support jsp, you need to introduce the following dependencies
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency>
- The complete dependencies are as follows:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
- Write test program
package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.view.InternalResourceView; @Controller public class HelloController { @RequestMapping("/hello") public ModelAndView hello(){ ModelAndView mv = new ModelAndView(); InternalResourceView internalResourceView = new InternalResourceView("index.jsp"); mv.setView(internalResourceView); return mv; } }
Create webapp folder in maven project, create index.jsp file, access http://localhost:8080/hello
Now we use the view parser of jsp in springboot, so where is the generated jsp file?
- What is certain here is that as long as you use jsp as the view, you must generate the corresponding servlet file.
- In linux system, the servlet page generated by jsp is in the / tmp file
├── tomcat.6136740723794350932.8080 │ └── work │ └── Tomcat │ └── localhost │ └── ROOT │ └── org │ └── apache │ └── jsp │ ├── index_jsp.class │ └── index_jsp.
Note: the corresponding servlet page will be generated only when the corresponding jsp page is visited for the first time