简体   繁体   中英

HTTP Status 500 - Servlet.init() for servlet fitTrackerServlet threw exception

This is my first time working with Spring and I am following the tutorial on PluralSight. Running the first example, I run into the error that is listed in the title. In my console, I get errors like these. I am using older versions of the dependencies to go through this course. I've made sure to follow the tutorial closely so not sure if I messed up somewhere or something is deprecated. I am using http://localhost:8080/FitnessTracker/greeting.html

SEVERE: Context initialization failed
java.lang.IllegalArgumentException
SEVERE: StandardWrapper.Throwable
java.lang.IllegalArgumentException
SEVERE: Allocate exception for servlet fitTrackerServlet
java.lang.IllegalArgumentException

HelloController.java

package com.pluralsight.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

    @RequestMapping(value = "/greeting")
    public String sayHello(Model model) {
        model.addAttribute("greeting", "Hello World");
        return "hello";
    }
}

servlet-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.pluralsight.controller" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
    p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>

</beans>

hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <h1>${greeting}</h1>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>fitTrackerServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/servlet-config.xml</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>fitTrackerServlet</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>

  <display-name>Archetype Created Web Application</display-name>
</web-app>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.pluralsight</groupId>
  <artifactId>FitnessTracker</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>FitnessTracker Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.2.0.RELEASE</version>
    </dependency>


    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>FitnessTracker</finalName>
  </build>
</project>

I had the same problem with the same PluralSight tutorial. I had both Java 1.7 and Java 1.8 installed. The project apparently throws an exception with Java 1.8. To rectify this situation you need to ensure that your Spring STS environment is set to use Java 1.7.

  1. Right click on the project and select Properties .
  2. In the Properties dialog, select Java Compiler .
  3. At the bottom of the dialog, select the link to configure the Installed JREs .
  4. Add a definition to point to JRE 1.7 and select this as the default.

The PluralSight tutorial now works for me. I am beginner to Java and Spring MVC myself so I am not sure if this was the best way to solve the problem.

Well, spring 3.2 needs java 7 or earlier. Instead of changing project properties, one another solution is just change the version number of spring mvc in pom.xml to 4.2.4.RELEASE instead of 3.2.0.RELEASE that you have used earlier. Now, you can proceed normally with java 8.

Below, I show the whole pom.xml with only change that I mentioned above.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.pluralsight</groupId>
  <artifactId>FitnessTracker</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>FitnessTracker Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>FitnessTracker</finalName>
  </build>
</project>

Try with 4.1.5. It works for me.

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.5.RELEASE</version>
</dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.2.3.RELEASE</version>
    </dependency>        
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>3.2.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>3.2.3.RELEASE</version>
    </dependency>

Try to use 3.2.3.RELEASE version wherever you have spring based dependency

I faced the same issue. So what you have to do is to use Tomcat v7.0, It is because I was using Tomcat v9.0 and it does not work with JRE7 but JRE8 . So

  • Set your application with Tomcat v7.0 and set your STS or Eclipse default build path with JRE7 .
  • Restart your Tomcat server.
  • Go to your browser and paste the above link and Here you Go !!! :)

To fix this, simply change your servlet container's runtime environment to Java 7. In Eclipse:

  1. Preferences -> Server -> Runtime Environments
  2. Select the server you are running the project under and click Edit... .
  3. From the JRE section, select the Java 7 JRE. Note that in order to be able to do this, you first need to download and install Java 7, and make the Java 7 JRE available in Eclipse. To make Java 7 JRE available in Eclipse:
    1. Download Java 7 and install it.
    2. In Eclipse, select Preferences -> Java -> Installed JREs and click Search... . If you have installed Java 7 to its default path, Eclipse should be able to discover Java 7 automatically.
    3. After adding Java 7 to Eclipse, you can select JRE 7 as the runtime environment of your servlet container.

I would also advise indicating the runtime environment being used in the server's name. For example, in Eclipse, instead of letting the server name simply be "Apache Tomcat v7.0", make it "Apache Tomcat v7.0 - JRE 7". Of course, you should update this name if you change the runtime environment of this server later.

Source: http://www.codejava.net/ides/eclipse/how-to-change-java-runtime-environment-for-tomcat-in-eclipse

NOTE: This answer applies to any other IDE as well. You just need to make sure that your servlet container runs under JRE 7.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM