简体   繁体   中英

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

I am learning Spring MVC and when I am trying to run the html file it gives the error HTTP Status 500 - Servlet.init() for servlet spring-dispatcher threw exception

This is my 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_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>FirstSpringMVC</display-name>

    <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    </servlet>


    <servlet-mapping>
        <servlet-name>spring-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

This is my spring-dispatcher-servlet

<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


    <bean id="HandlerMapping" class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

    <bean name = "/welcome.html" class = "com.ankitud.hellocontroller.HelloController"/>

    <bean id = "viewResolver"
     class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name = "prefix">
            <value>/WEB-INF/</value>
        </property>

        <property name = "suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

This is my jsp page

<html>
    <head>
        <title>First MVC Application</title>
    </head>
    <body>

    <h1>First MVC Application</h1>
    <h2>${welcomemessage}</h2>      

    </body>
</html>

This is my HelloController class

package com.ankitud.hellocontroller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloController extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception{

        ModelAndView modelandview = new ModelAndView("HelloPage");
        modelandview.addObject("welcomemessage", "Hi User, welcome to the first Spring MVC application");

        return modelandview;

    }

}

And this is the exception that I am getting

javax.servlet.ServletException: Servlet.init() for servlet spring-dispatcher threw exception
 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)

I am working with Eclipse 4.5.1 , Tomcat 8.0.30 and Spring 4.2.3.

You missed this block from your web.xml try to add it:

 <web-app...>

  <!-------- DispatcherServlet definition goes here----->
   ....
   <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/spring-dispatcher-servlet.xml</param-value>
   </context-param>

   <listener>
   <listener-class>
     org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>
 </web-app>

In the spring-dispatcher-servlet.xml instead of the below code

   <?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

try using this code

<?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"
       xsi:schemaLocation="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.xsd">

I tried and it worked for me. Happy coding.

This might be too late but I he's the solution for any help seeker in future. There are 2 blocks which are missing in the code:

  1. In your web.xml, inside you need to add

 <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-dispatcher-servlet.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>

  1. The above change will help you get rid of HTTP Status error 500. But you would now end up getting HTTP Status error 404 and this is because in your class, ie HelloController.java you need to import add @Controller annotation just above your class definition like this

 @Controller public class HelloController extends AbstractController { //Your class content goes here }

This will solve your routing issues and now you can visit /welcome.html. Though you would still see ${welcomemessage} as it is when you route to /welcome.html. Finally in order to see the actual object data on the jsp page, simply add

 <%@ page contentType="text/html; charset=UTF-8" isELIgnored="false" %>

That's it! This would help you access your welcomemessage object in your Jsp page. Hope this helps :)

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