SpringBoot使用Jetty/tomcat容器访问jsp页面

1 篇文章 0 订阅
1 篇文章 0 订阅

springBoot项目使用jetty/tomcat容器,创建web模块,访问jsp页面。记录一下。

ps:主要是为了能够启动容器,百度了很久……X_X

父模块pom文件

<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <spring-boot.version>2.3.1.RELEASE</spring-boot.version>
</properties>

<modules>
    <module>demo-003-jsp</module>
    <module>demo-004-jetty</module>
</modules>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
            <version>3.8.1</version>
        </plugin>
    </plugins>
</build>

标题springboot使内嵌式tomcat服务器支持jsp

目录结构

在这里插入图片描述

pom文件

<dependencies>
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
   <!-- 添加JS TL标签库依赖模块 -->
   <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>jstl</artifactId>
   </dependency>

   <!-- 添加servlet依赖模块 -->
   <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>javax.servlet-api</artifactId>
       <scope>provided</scope>
   </dependency>
   <!--添加tomcat依赖模块.(参考网络资料设置,结果无法启动,注释掉就没事了T_T)
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-tomcat</artifactId>
       <scope>provided</scope>
   </dependency>-->
   <!-- 使用jsp引擎,springBoot内置tomcat没有此依赖 -->
   <dependency>
       <groupId>org.apache.tomcat.embed</groupId>
       <artifactId>tomcat-embed-jasper</artifactId>
       <scope>provided</scope>
   </dependency>

   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-test</artifactId>
       <scope>test</scope>
   </dependency>
</dependencies>

application配置文件

server:
  port: 8080 #配置 Web 容器的端口号
  tomcat:
    uri-encoding: UTF-8 #配置 Tomcat 请求编码
    max-threads: 500 #配置 Tomcat 的最大线程数
    basedir: #配置 Tomcat 运行日志和临时文件的目录。若不配置,则默认使用系统的临时目录
      ./logs/jsp
  servlet:
    context-path: /demo-jsp #配置项目名称(默认为 /),如果配置了项目名称,那么在访问路径中要加上配置的路径
    session: #配置 session 失效时间。30m 表示 30 分钟,如果不写单位则默认单位是秒。
      timeout: 30m # Tomcat中配置 session 过期时间是以分钟为单位,设置是秒的话,会自动转换为一个不超过所配置秒数的最大分钟数。如 119 秒,实际 session 过期时间是 1 分钟)
debug: false #是否启用debug

spring:
  mvc:
    view:
      prefix: /WEB-INF/jsp/ #jsp页面路径
      suffix: .jsp #页面资源后缀

启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * DemoJspApplication类说明: 启动类
 * <p><a href="https://blog.csdn.net/qq_34657993/article/details/79070084">springBoot 在idea多模块下 子模块的web项目用内置tomcat启动访问jsp报404</a></p>
 * <p><a href="https://www.hangge.com/blog/cache/detail_2457.html">SpringBoot - 内置的Tomcat服务器配置详解(附:启用HTTPS服务)</a></p>
 * <p>pom文件配置war打包方式,启动【编辑配置 Run/Debug Configurations】,【工作目录 Working directory】指向子目录路径</p>
 * <p>✓ Include dependencies with ”Provided“ scope(在“已提供”范围内包含依赖项).</p>
 * @author z.y.l
 * @version v1.0
 * @date 2020-09-01
 */
@SpringBootApplication
public class DemoJspApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoJspApplication.class, args);
    }
}

IndexController视图控制器

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * JspIndexController类说明: 视图控制器
 *
 * @author z.y.l
 * @version v1.0
 * @date 2020-09-01
 */
@Controller
@RequestMapping("/")
public class JspIndexController {
    @RequestMapping("")
    public String index(){
        return "index";
    }
    @RequestMapping("hello")
    public ModelAndView hello(){
        //设置对应JSP的模板文件
        ModelAndView modelAndView = new ModelAndView("hello");
        //设置${info}标签的值
        modelAndView.addObject("info", "Hello,JSP.");
        return modelAndView;
    }
}

jsp页面

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
	<head>
	  <meta charset="utf-8">
	  <title>index-jsp</title>
	</head>
	<body >
		<h1>Hello demo-jsp</h1>
	</body>
</html>

hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
	<head>
	  <meta charset="utf-8">
	  <title>index-jsp</title>
	</head>
	<body >
		<h1>${info}</h1>
	</body>
</html>

标题浏览器访问

在这里插入图片描述
在这里插入图片描述


springboot使内嵌式jetty服务器支持jsp

目录结构

在这里插入图片描述

pom文件

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- Jetty适合长连接应用,就是聊天类的长连接 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>

    <!--jetty容器支持jsp start-->
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlet</artifactId>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-webapp</artifactId>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>apache-jsp</artifactId>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>apache-jstl</artifactId>
    </dependency>
    <!--jetty容器支持jsp end-->

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

application配置文件

server:
  port: 8080
  servlet:
    context-path: /demo-jetty
    session:
      timeout: 30m
  jetty:
    acceptors: 2 #非阻塞线程池acceptors,接受请求连接
    selectors: 4 #非阻塞线程池selectors,处理HTTP消息协议的解包
    max-http-form-post-size:
      200000B # put或post方法最大字节数
debug: false
spring:
  mvc:
    view:
      prefix: /WEB-INF/jsp/
      suffix: .jsp

启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * DemoJettyApplication类说明: 启动类
 * <p>可以直接替代tomcat,解析jsp页面</p>
 *
 * @author z.y.l
 * @version v1.0
 * @date 2020-09-01
 */
@SpringBootApplication
public class DemoJettyApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoJettyApplication.class, args);
    }
}

IndexController视图控制器

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * JettyIndexController类说明: 视图控制器
 *
 * @author z.y.l
 * @version v1.0
 * @date 2020-09-01
 */
@Controller
@RequestMapping("/")
public class JettyIndexController{
    @RequestMapping("")
    public String index(){
        return "index";
    }
    @RequestMapping("hello")
    public ModelAndView hello(){
        //设置对应JSP的模板文件
        ModelAndView modelAndView = new ModelAndView("hello");
        //设置${info}标签的值
        modelAndView.addObject("info", "Hello,JETTY.");
        return modelAndView;
    }
}

jsp页面

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
	<head>
	  <meta charset="utf-8">
	  <title>index-jetty</title>
	</head>
	<body >
		<h1>Hello demo-jetty</h1>
	</body>
</html>

hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
	<head>
	  <meta charset="utf-8">
	  <title>index-jetty</title>
	</head>
	<body >
		<h1>${info}</h1>
	</body>
</html>

浏览器访问

在这里插入图片描述
在这里插入图片描述


end

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值