1

My code is in github

https://github.com/shiblybcc/blog-aggregator

I have just created a spring framework project. This is my web.xml file code

<?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"
    version="3.0">
    <display-name>Course Rating</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

After I run my jetty server this works perfect and show the content of index.jsp file. But if I add following code in the web.xml file

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.html</url-pattern>
    <url-pattern>*.htm</url-pattern>
    <url-pattern>*.json</url-pattern>
    <url-pattern>*.xml</url-pattern>
</servlet-mapping>

I get the error

HTTP ERROR: 503    
Problem accessing /. Reason:    
    Service Unavailable

I am following a tutorial and they are doing the same. I don't understand what is wrong with my code. Thanks in advance.

asdfkjasdfjk
  • 3,784
  • 18
  • 64
  • 104

2 Answers2

0

Ok, understood, what is happening when you are calling localhost:8080/index.htm, servlet name "dispatcher" got executed and as there is no mapping for index.html exist, you will not be able to see the index.html page.

To make it work, you have to put little more efforts.

1). Put your index.html under WEB-INF folder.

2). Create Controller:

package com.test.controller;

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

@Controller
@RequestMapping("/index")
public class IndexPageController{

    @RequestMapping(method = RequestMethod.GET)
public String showIndexPage(ModelMap model) {
  return "index";
 }
}

3). Create mvc-dispatcher-servlet.xml as below and put it under WEB-INF folder :

 <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"   
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">
       <mvc:annotation-driven></mvc:annotation-driven>
       <context:component-scan base-package="com.test.controller" />

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

    </beans>

4). Edit your web.xml as:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
      </context-param>

      <listener>
            <listener-class>
            org.springframework.web.context.ContextLoaderListener
            </listener-class>
      </listener>
Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
0

After whole day research I found the solution here Getting Error scanning file when running jetty 9 on java 8 using the maven jetty plugin

Also I updated jetty, maven and tomcat. My current pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <dependencies>
                <dependency>
                    <groupId>org.ow2.asm</groupId>
                    <artifactId>asm</artifactId>
                    <version>5.0.3</version>
                </dependency>
                <dependency>
                    <groupId>org.ow2.asm</groupId>
                    <artifactId>asm-commons</artifactId>
                    <version>5.0.3</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.3</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

After updating everything, it is working fine now.

Community
  • 1
  • 1
asdfkjasdfjk
  • 3,784
  • 18
  • 64
  • 104