美文网首页
tomcat7以上版本使用maven引进jstl错误

tomcat7以上版本使用maven引进jstl错误

作者: carway | 来源:发表于2017-12-18 23:26 被阅读0次

    现在我已经不怎么用jsp了,但是有些比较基础的还是用jsp比较方便。我的tomcat版本是8.5,所以对应servlet应该是servlet3.x
    如果版本tomcat版本和servlet版本不对应的话有可能会出错


    Servlet/JSP各规范与Web服务器Tomcat各版本的对应关系.png

    相应的web.xml要对应过来
    web.xml 3.1

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
        id="WebApp_ID" version="3.1">
    </web-app>
    

    即便这样做,我还是报错了


    image.png

    控制台报错信息

    十二月 18, 2017 10:37:04 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin
    警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:demoJ4' did not find a matching property.
    十二月 18, 2017 10:37:04 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin
    警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Excle' did not find a matching property.
    十二月 18, 2017 10:37:04 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin
    警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:ShiroWeb' did not find a matching property.
    十二月 18, 2017 10:37:04 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: Server version:        Apache Tomcat/8.5.11
    十二月 18, 2017 10:37:04 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: Server built:          Jan 10 2017 21:02:52 UTC
    十二月 18, 2017 10:37:04 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: Server number:         8.5.11.0
    十二月 18, 2017 10:37:04 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: OS Name:               Windows 8.1
    十二月 18, 2017 10:37:04 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: OS Version:            6.3
    十二月 18, 2017 10:37:04 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: Architecture:          amd64
    十二月 18, 2017 10:37:04 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: Java Home:             D:\javasoft\jdk1.8.0_121\jre
    十二月 18, 2017 10:37:04 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: JVM Version:           1.8.0_121-b13
    十二月 18, 2017 10:37:04 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: JVM Vendor:            Oracle Corporation
    十二月 18, 2017 10:37:04 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: CATALINA_BASE:         D:\javasoft\eclipse工作区\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
    十二月 18, 2017 10:37:04 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: CATALINA_HOME:         D:\javasoft\apache-tomcat-8.5.11
    十二月 18, 2017 10:37:04 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: Command line argument: -Dcatalina.base=D:\javasoft\eclipse工作区\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
    十二月 18, 2017 10:37:04 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: Command line argument: -Dcatalina.home=D:\javasoft\apache-tomcat-8.5.11
    十二月 18, 2017 10:37:04 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: Command line argument: -Dwtp.deploy=D:\javasoft\eclipse工作区\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
    十二月 18, 2017 10:37:04 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: Command line argument: -Djava.endorsed.dirs=D:\javasoft\apache-tomcat-8.5.11\endorsed
    十二月 18, 2017 10:37:04 下午 org.apache.catalina.startup.VersionLoggerListener log
    信息: Command line argument: -Dfile.encoding=UTF-8
    十二月 18, 2017 10:37:04 下午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
    信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: D:\javasoft\jdk1.8.0_121\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:/javasoft/jdk1.8/bin/server;D:/javasoft/jdk1.8/bin;D:/javasoft/jdk1.8/lib/amd64;D:\javasoft\MySQL\mysql-5.7.17-winx64\bin;D:\javasoft\maven\apache-maven-3.3.9\bin;C:\ProgramData\Oracle\Java\javapath;D:\javasoft\jdk1.7.0_80\jdk1.7.0_80\bin;C:\Windows\System32;D:\javasoft\redis\;C:\Users\嘉炜\.dnx\bin;C:\Program Files\Microsoft DNX\Dnvm\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;D:\javasoft\插件\linux插件\WinSCP\WinSCP\;%CA;ALINA_HOME%\lib;%CATALINA_HOME%\bin;D:\javasoft\git\Git\cmd;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\;C:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;D:\javasoft\SVN\TortoiseSVN\bin;C:\Program Files\IBM\SPSS\Modeler\18.0\ext\bin\spss.TMWBServer\bin;C:\Program Files\IBM\SPSS\Modeler\18.0\MPICH2\bin;C:\Program Files\IBM\SPSS\Modeler\18.0\TABI;D:\pythonsoft\python3.6.3\Scripts\;D:\pythonsoft\python3.6.3\;D:\javasoft\eclipse\eclipse-jee-neon-3-RC3-win32-x86_64\eclipse;;.
    十二月 18, 2017 10:37:04 下午 org.apache.coyote.AbstractProtocol init
    信息: Initializing ProtocolHandler ["http-nio-8080"]
    十二月 18, 2017 10:37:05 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
    信息: Using a shared selector for servlet write/read
    十二月 18, 2017 10:37:05 下午 org.apache.coyote.AbstractProtocol init
    信息: Initializing ProtocolHandler ["ajp-nio-8009"]
    十二月 18, 2017 10:37:05 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
    信息: Using a shared selector for servlet write/read
    十二月 18, 2017 10:37:05 下午 org.apache.catalina.startup.Catalina load
    信息: Initialization processed in 1211 ms
    十二月 18, 2017 10:37:05 下午 org.apache.catalina.core.StandardService startInternal
    信息: Starting service Catalina
    十二月 18, 2017 10:37:05 下午 org.apache.catalina.core.StandardEngine startInternal
    信息: Starting Servlet Engine: Apache Tomcat/8.5.11
    十二月 18, 2017 10:37:06 下午 org.apache.jasper.servlet.TldScanner scanJars
    信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    十二月 18, 2017 10:37:06 下午 org.apache.catalina.core.ContainerBase startInternal
    严重: A child container failed during start
    java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/ShiroWeb]]
        at java.util.concurrent.FutureTask.report(FutureTask.java:122)
        at java.util.concurrent.FutureTask.get(FutureTask.java:192)
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:939)
        at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:872)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
    Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/ShiroWeb]]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)
        ... 6 more
    Caused by: org.apache.catalina.LifecycleException: Failed to start component [org.apache.catalina.webresources.StandardRoot@6e588728]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)
        at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:4842)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4974)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        ... 6 more
    Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [org.apache.catalina.webresources.JarResourceSet@3800327f]
        at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:112)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:140)
        at org.apache.catalina.webresources.StandardRoot.startInternal(StandardRoot.java:708)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        ... 9 more
    Caused by: java.lang.IllegalArgumentException: java.util.zip.ZipException: invalid LOC header (bad signature)
        at org.apache.catalina.webresources.AbstractSingleArchiveResourceSet.initInternal(AbstractSingleArchiveResourceSet.java:113)
        at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:107)
        ... 12 more
    Caused by: java.util.zip.ZipException: invalid LOC header (bad signature)
        at java.util.zip.ZipFile.read(Native Method)
        at java.util.zip.ZipFile.access$1400(ZipFile.java:60)
        at java.util.zip.ZipFile$ZipFileInputStream.read(ZipFile.java:717)
        at java.util.zip.ZipFile$ZipFileInflaterInputStream.fill(ZipFile.java:419)
        at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
        at sun.misc.IOUtils.readFully(IOUtils.java:65)
        at java.util.jar.JarFile.getBytes(JarFile.java:425)
        at java.util.jar.JarFile.getManifestFromReference(JarFile.java:193)
        at java.util.jar.JarFile.getManifest(JarFile.java:180)
        at org.apache.catalina.webresources.AbstractSingleArchiveResourceSet.initInternal(AbstractSingleArchiveResourceSet.java:111)
        ... 13 more
    
    十二月 18, 2017 10:37:07 下午 org.apache.jasper.servlet.TldScanner scanJars
    信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    十二月 18, 2017 10:37:07 下午 org.apache.catalina.core.ContainerBase startInternal
    严重: A child container failed during start
    java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
        at java.util.concurrent.FutureTask.report(FutureTask.java:122)
        at java.util.concurrent.FutureTask.get(FutureTask.java:192)
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:939)
        at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.StandardService.startInternal(StandardService.java:422)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:793)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:655)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:355)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:495)
    Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
    Caused by: org.apache.catalina.LifecycleException: A child container failed during start
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:947)
        at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:872)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        ... 6 more
    
    十二月 18, 2017 10:37:07 下午 org.apache.catalina.startup.Catalina start
    严重: The required Server component failed to start so Tomcat is unable to start.
    org.apache.catalina.LifecycleException: Failed to start component [StandardServer[8005]]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:655)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:355)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:495)
    Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardService[Catalina]]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)
        at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:793)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        ... 7 more
    Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina]]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)
        at org.apache.catalina.core.StandardService.startInternal(StandardService.java:422)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        ... 9 more
    Caused by: org.apache.catalina.LifecycleException: A child container failed during start
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:947)
        at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        ... 11 more
    
    十二月 18, 2017 10:37:07 下午 org.apache.coyote.AbstractProtocol pause
    信息: Pausing ProtocolHandler ["http-nio-8080"]
    十二月 18, 2017 10:37:07 下午 org.apache.coyote.AbstractProtocol pause
    信息: Pausing ProtocolHandler ["ajp-nio-8009"]
    十二月 18, 2017 10:37:07 下午 org.apache.catalina.core.StandardService stopInternal
    信息: Stopping service Catalina
    十二月 18, 2017 10:37:07 下午 org.apache.coyote.AbstractProtocol destroy
    信息: Destroying ProtocolHandler ["http-nio-8080"]
    十二月 18, 2017 10:37:07 下午 org.apache.coyote.AbstractProtocol destroy
    信息: Destroying ProtocolHandler ["ajp-nio-8009"]
    
    

    为了解决这个问题,
    先看一下我的pom.xml配置

            <!-- 添加servlet支持 servlet3.1对应tomcat8.5.x -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <scope>provided</scope>
            </dependency>
            <!-- 添加jsp支持 -->
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>javax.servlet.jsp-api</artifactId>
                <version>2.3.1</version>
                <scope>provided</scope>
            </dependency>
            <!-- 添加jstl支持 -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>    
    

    当我把jstl的依赖去掉的时候,却意外发现可以启动tomcat,所以我就Google一下

    在maven中, scope provided 意味着,该jar只在编译时提供支持,在运行时,由web容器提供支持。在tomcat 7(包含7) 以上版本不再提供对jstl的支持。所以,当你使用tomcat7(包含7)以上版本时,会报说 XXXX cannot be resolved。而tomca7 以下版本,在运行时会提供对 jstl 的支持。
    也就是说,要将 scope 范围改成 runtime,但是改了还是解决不了问题。

    我在StackOverflow上看到了解决方案,https://stackoverflow.com/questions/6094329/tomcat-7-and-jstl/25374024#25374024

    1、如果你在使用Tomcat 7,这是一个符合Servlet 3.0的服务器。
    2、如果你想使用Servlet 3.0规范,你必须有你的web.xml如下

    <web-app 
      xmlns="http://java.sun.com/xml/ns/javaee" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> 
    

    3、如果你使用Maven,你的pom.xml应该有这些行

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    
    <dependency>
        <groupId>org.glassfish.web</groupId>
        <artifactId>jstl-impl</artifactId>
        <version>1.2</version>
        <exclusions>
            <exclusion>
                <artifactId>servlet-api</artifactId>
                <groupId>javax.servlet</groupId>
            </exclusion>
            <exclusion>
                <artifactId>jsp-api</artifactId>
                <groupId>javax.servlet.jsp</groupId>
            </exclusion>
            <exclusion>
                <artifactId>jstl-api</artifactId>
                <groupId>javax.servlet.jsp.jstl</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    

    用某个答主的原话讲:
    These dependencies are very important. JSTL 2.1 + Tomcat 7 + Servlet 3.0 is very broken unless you fix it by using these lines, especially the exclusion part. What is happening is the JSTL 2.1 Jars are actually pulling in the wrong versions of the Servlet spec--2.5. Unless you stop that from happening, you will be in a whole world of pain.
    Finally, in case Maven can't find those JARS, you can make Eclipse happy by including three JARS with your project and doing the usual Project--> Properties--> Java Build Path and include them that way--though Maven should take care of it.

    javax.servlet-api-3.0.1.jar
    javax.servlet.jsp.jstl-1.2.1.jar
    javax.servlet.jsp.jstl-api-1.2.1.jar
    

    他的意思是JSTL 1.2.1 + Tomcat 7 + Servlet 3.0是非常糟糕的。
    假如你用maben找不到这些jar包,你可以用eclipse直接导入这些包算了,更加省事- -

    image.png

    最后那几段就是答主吐槽的话,就是说假如你是用JSTL 1.2.1 + Tomcat 7 + Servlet 3.0的这种组合的话,首先你的web.xml得满足servlet3.0的规范,你的jars的类路径只能有上面那三个jars包,确认不要把他们放在WEB-INF / lib目录下。

    不过我也看到更简单的解决方案
    把jstl换成这个就好

    <dependency>
        <groupId>org.glassfish.web</groupId>
        <artifactId>javax.servlet.jsp.jstl</artifactId>
        <version>1.2.1</version>
        <scope>runtime</scope>
    </dependency>
    
    另外一个答主的答案.png

    意思说新版本1.2.1将javax.servlet,javax.jsp等依赖项标记为提供的范围,以便不需要排除它们,所以直接用这个版本就行,不用像1.2.0那样得去掉依赖项。
    换了之后,完美解决问题。

    相关文章

      网友评论

          本文标题:tomcat7以上版本使用maven引进jstl错误

          本文链接:https://www.haomeiwen.com/subject/dofjvxtx.html