2

I am getting this error after typing url localhost:8080/HelloWeb

HTTP Status 404 - /HelloWeb

type Status report

message /HelloWeb

description The requested resource is not available.
Apache Tomcat/7.0.30

I am not able to solve it please help someone here are my files which are needed to do execute this SPRING MVC hello program

HelloController.java

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

@Controller
@RequestMapping("/hello")
public class HelloController {
    @RequestMapping(method = RequestMethod.GET)
    public String printHello(ModelMap model) {
        model.addAttribute("message", "Hello Spring MVC Framework!");

        return "hello";
    }
}

hello.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
   <h2>${message}</h2>
</body>
</html>

web.xml

<web-app id="WebApp_ID" version="2.4"
   xmlns="http://java.sun.com/xml/ns/j2ee" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

   <display-name>Spring MVC Application</display-name>

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

   <servlet-mapping>
      <servlet-name>HelloWeb</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>

</web-app>

HelloWeb-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
   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.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:component-scan base-package="com.tutorialspoint" />

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

</beans>

and here is my project hierarchy

HelloWeb
 src
   com.tutorialspoint
 WebContent
   jsp
     hello.jsp
   META-INF
     MANIFEST.MF
   WEB_INF
     lib
     HelloWeb-servlet.xml
     index.jsp
     web.xml

sorry, i forgot to include that i following this Spring MVC example from

tutorialspoint.com/spring/spring_mvc_hello_world_example.htm

Reeinku
  • 180
  • 3
  • 6
  • 19

9 Answers9

0

You're using the wrong URL. The correct URL is localhost:8080/hello or localhost:8080/HelloWeb/hello, depending on how you're deploying.

atamanroman
  • 11,607
  • 7
  • 57
  • 81
  • no sir @atamanroman i am using URL localhost:8080/HelloWeb/ but as soon as i enter Http status 404 error comes. – Reeinku Aug 31 '14 at 08:35
0

when you hit the url localhost:8080/HelloWeb/ it needs a landing page to display something which you can specify in the web.xml. Add this to your web.xml under the

<welcome-file-list>
  <welcome-file>/WEB-INF/index.jsp</welcome-file>
</welcome-file-list>

but if you need to access the servlet you need to specify the url as localhost:8080/HelloWeb/hello

SparkOn
  • 8,806
  • 4
  • 29
  • 34
  • I want to display servlet but as soon as i enter localhost:8080/HelloWeb/hello but then again error Http Status 404 comes – Reeinku Aug 31 '14 at 09:01
  • what do you mean by display servlet and your url must be localhost:8080/HelloWeb/hello HelloWeb with uppercase H – SparkOn Aug 31 '14 at 09:05
  • moreover you are missing `` in your HelloWeb-servlet.xml – SparkOn Aug 31 '14 at 09:09
  • display mean to access servlet in browser and i am typing same URL localhost:8080/HelloWeb/hello but getting error – Reeinku Aug 31 '14 at 09:11
  • include the thing i told you in the above comment – SparkOn Aug 31 '14 at 09:14
  • ya i included it HelloWeb-servlet.xml but again i am getting same error. – Reeinku Aug 31 '14 at 09:21
  • one more thing remove the RequestMapping from the class level and add this `@RequestMapping(value="/hello", method = RequestMethod.GET)` above your method printHello – SparkOn Aug 31 '14 at 09:39
  • sorry for the late response, i have added RequestMapping suggested by you above method printHello but i am getting that error AGAIN........... – Reeinku Sep 02 '14 at 00:09
  • did u remove it from the class level? – SparkOn Sep 02 '14 at 03:55
0

This is late, but i too had same problem, because i also took code from tutorialspoint.com

try adding this in your HelloWeb-servlet.xml

<context:annotation-config />

This will enable annotations

and as well include this in your web.xml

 <welcome-file-list>
 <welcome-file>/WEB-INF/jsp/login.jsp</welcome-file>
 </welcome-file-list>
Saha
  • 307
  • 2
  • 5
  • 16
0

I had the same problem and discovered that my jsp name had a space. The example from tutorialspoint works perfect as is.

0

I know that this post is very old, this is for the sake of other that may face the same challenge. Your url mapping is not properly mapped. Use this as your controller. notice the change is just 'hello' to 'helloWeb' the name in your Controoler (@RequestMapping("/hello")) should be the same as the name in your web-app ( HelloWeb / )

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

    @Controller
    @RequestMapping("/HelloWeb")
    public class HelloController {
        @RequestMapping(method = RequestMethod.GET)
        public String printHello(ModelMap model) {
            model.addAttribute("message", "Hello Spring MVC Framework!");

            return "hello";
        }
    }
0

This is your code...

`<property name="prefix" value="/WEB-INF/jsp/" />`

it shows that you have folder 'jsp' inside the 'WEB-INF' . 

But you don't have a folder there as by looking at your project hierarchy.

Hence, change your code into below:

`<property name="prefix" value="/WEB-INF/" />.`
0

As @SparkOn pointed out, that the demo is missing <mvc:annotation-driven/>

I make a working version on github, so anyone refered to this page can checkout it and run it with maven, as following:

build with maven

mvn clean package jetty:run-war

and go to browser, input http://localhost:8080/hello

you will see

 Hello Spring MVC Framework!
Yu Jiaao
  • 4,444
  • 5
  • 44
  • 57
0

I got into similar problem and followed few steps to resolve the issue. Though its very old post the solution may help others :-)

The steps I followed to solve the problem

1.Check the server configuration you should get Tomcat Homepage for http://localhost:8080/ else follow the below link
Tomcat started in Eclipse but unable to connect to http://localhost:8085/

2.Why tomcat server location property is greyed in Eclipse

3.Dont forget to set the Deploypath to webapps

4.Right clich project->RunAs->RunOnServer

5.go to browser, input http://localhost:8080/yourApplicationName/yourPath

Nithya
  • 1
  • 2
0

You have not given the Request Mapping here

@RequestMapping(method = RequestMethod.GET)
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103