严重: Servlet.service() for servlet [jsp] in context with path [/bookstore] threw exception
[java.lang.LinkageError: loader constraint violation: loader org.apache.jasper.servlet.JasperLoader @46ac2af (instance of org.apache.jasper.servlet.JasperLoader,
child of org.apache.catalina.loader.WebappClassLoader @4badd439 org.apache.catalina.loader.WebappClassLoader) wants to load interface javax.servlet.http.HttpServletRequest.
A different interface with the same name was previously loaded by org.codehaus.plexus.classworlds.realm.ClassRealm @2cd388f5 (instance of org.codehaus.plexus.classworlds.realm.ClassRealm,
child of 'bootstrap').] with root cause
java.lang.LinkageError: loader constraint violation: loader org.apache.jasper.servlet.JasperLoader @46ac2af (instance of org.apache.jasper.servlet.JasperLoader,
child of org.apache.catalina.loader.WebappClassLoader @4badd439 org.apache.catalina.loader.WebappClassLoader) wants to load interface javax.servlet.http.HttpServletRequest.
A different interface with the same name was previously loaded by org.codehaus.plexus.classworlds.realm.ClassRealm @2cd388f5 (instance of org.codehaus.plexus.classworlds.realm.ClassRealm,
child of 'bootstrap').
at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3167)
引起问题的原因:
maven骨架默认使用servlet 2.5的版本
可以从项目的web.xml文件中看出:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
而项目中希望使用servlet3以上的版本,项目的配置文件中包含了多个版本的servlet,所以才报了这个错误:
pom.xml文件:
<!--此sevlet版本低于3的时候使用-->
<!-- <dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
这两个jar包的坐标,一个是servlet2.5版本,一个是servlet3.1版本。现在只需要3.1版本的坐标,其次我们需要更改maven项目的默认servlet版本,在webapp/WEB-INF/web.xml文件中,但是不能直接修改web.xml的版本,由于不同版本的约束头不一样,所以我们可以先在创建一个web,然后直接复制WEB-INF/web.xml文件中的约束头和版本。