嚴重: 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版本。現(xiàn)在只需要3.1版本的坐標缸夹,其次我們需要更改maven項目的默認servlet版本燎字,在webapp/WEB-INF/web.xml文件中,但是不能直接修改web.xml的版本勾笆,由于不同版本的約束頭不一樣敌蚜,所以我們可以先在創(chuàng)建一個web,然后直接復制WEB-INF/web.xml文件中的約束頭和版本。