Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 235
LinhSaysHi Avatar asked Sep 05 '15 18:09

LinhSaysHi


2 Answers

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.

like image 51
Jus Avatar answered Sep 28 '22 08:09

Jus


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>
like image 43
Number945 Avatar answered Sep 28 '22 08:09

Number945