tomcat拦截jsp页面
时间: 2023-11-17 20:57:45 浏览: 42
Tomcat在处理JSP页面时,会使用默认的JspServlet来处理请求。JspServlet会根据请求的URL路径来查找对应的JSP文件,并将其编译成Java代码,最终生成对应的Servlet类。在Tomcat的配置文件web.xml中,可以通过配置servlet-mapping来指定JspServlet拦截的URL路径,一般是以*.jsp或者*.jspx为后缀的文件。同时,Tomcat也提供了DefaultServlet
相关问题
为什么基于ssm的项目使用Tomcat时,jsp中引入的css文件以及图片等会被拦截
在使用 SSM 框架时,通常会将静态资源文件(如 CSS 文件、图片等)放置在 Web 应用程序的根目录下的 `WEB-INF` 目录之外的某个位置,例如 `webapp/static`。但是,在访问这些静态资源文件时,可能会出现被拦截的情况。
这是因为在使用 Tomcat 时,Tomcat 默认会对请求进行安全检查,而默认情况下,Tomcat 是不允许直接访问 `WEB-INF` 目录之外的文件的。因此,当 JSP 页面中引用的静态资源文件不在 `WEB-INF` 目录下时,Tomcat 会拦截这些请求,导致无法访问。
要解决这个问题,可以在 `web.xml` 文件中配置 Tomcat 的安全过滤规则,允许访问 `WEB-INF` 目录之外的文件。可以在 `web.xml` 文件中添加以下代码:
```
<!-- 设置Tomcat允许访问WEB-INF目录之外的文件 -->
<security-constraint>
<web-resource-collection>
<web-resource-name>Allowed Resources</web-resource-name>
<url-pattern>/static/*</url-pattern>
</web-resource-collection>
</security-constraint>
```
这段代码的作用是设置 Tomcat 允许访问 `/static/` 目录下的文件。这样,当 JSP 页面中引用的静态资源文件位于 `webapp/static` 目录下时,就可以正常访问了。
springboot工程上使用jsp实现配置拦截器登录
要在Spring Boot工程上使用JSP实现配置拦截器登录,您可以按照以下步骤操作:
1. 在pom.xml文件中添加JSP支持依赖:
```
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.20</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
```
2. 在application.properties文件中添加JSP配置:
```
spring.mvc.view.prefix:/WEB-INF/jsp/
spring.mvc.view.suffix:.jsp
```
3. 创建一个拦截器类,实现HandlerInterceptor接口,并实现其preHandle、postHandle和afterCompletion方法,用于在请求处理前、处理后和完成后执行相应的操作,比如验证登录信息、记录日志等。
4. 在WebMvcConfigurerAdapter中注册拦截器:
```
@Configuration
public class WebConfigurer extends WebMvcConfigurerAdapter {
@Autowired
private LoginInterceptor loginInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginInterceptor)
.addPathPatterns("/admin/**") // 拦截/admin/下的请求
.excludePathPatterns("/login"); // 不拦截/login请求
}
}
```
5. 创建一个Controller类,用于处理用户登录请求。在该类中,可以使用@SessionAttribute注解将用户登录信息存储到Session中,以便在其他页面中进行验证。
6. 在JSP页面中,使用JSTL标签库或EL表达式获取Session中的用户信息,以便进行权限验证。
以上就是在Spring Boot工程上使用JSP实现配置拦截器登录的基本步骤,您可以根据实际需求进行相应的修改和扩展。
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)