jsp的本质
- jsp的本质就是
servlet
,使用过tomcat部署过javaweb项目,都知道在work目录生成jsp页面对应的sevlet。 - servlet的本质就是封装了socket
jsp页面
<%--
Created by IntelliJ IDEA.
User: wy
Date: 2019/11/26
Time: 下午11: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">登录</a>
</body>
</html>
对应的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\">登录</a>\n");
out.write("</body>\n");
out.write("</html>\n");
} catch (java.lang.Throwable t) {
.....
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
SpringBoot中使用jsp
- springboot默认不支持jsp的,要让springboot支持jsp需要引入以下依赖
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
- 完整的依赖如下:
<?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>
- 编写测试程序
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;
}
}
在maven项目中创建webapp文件夹,创建index.jsp文件,访问http://localhost:8080/hello
现在我们在springboot
中使用了jsp的视图解析器,那么生成的jsp文件在哪里?
- 这里可以确定的是,只要使用jsp作为视图必定是要生成相应的
servlet
文件。 - 在linux系统中,jsp生成的servlet页面在
/tmp
文件中
├── tomcat.6136740723794350932.8080
│ └── work
│ └── Tomcat
│ └── localhost
│ └── ROOT
│ └── org
│ └── apache
│ └── jsp
│ ├── index_jsp.class
│ └── index_jsp.
注意:当第一次访问对应的jsp页面才会生成对应的
servlet
页面
参考
来源:CSDN
作者:技术之虫
链接:https://blog.csdn.net/Wang____Yang/article/details/104263475