-2

These are the files of my Spring MVC project:

Java controller: AddController.java

    package com.mycompany;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class AddController {
        @RequestMapping("/sum")
        public String add(){
            return "display.jsp";
        }
    }

War configuration file: web.xml

    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app>
      <display-name>Archetype Created Web Application</display-name>
    
      <servlet>
        <servlet-name>telusko</servlet-name>
        <servlet-class>
          org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>telusko</servlet-name>
        <url-pattern>*.htm</url-pattern>
      </servlet-mapping>
    
    </web-app>

Index view page: index.jsp

    <html>
    <body>
    <form action="sum">
        <input type="text" name="t1"><br>
        <input type="text" name="t2"><br>
        <input type="submit">
    </form>
    </body>
    </html>

Display result view page: display.jsp

    <%--
      Created by IntelliJ IDEA.
      User: yrbar
      Date: 2022-08-11
      Time: 2:59 p.m.
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    I am here
    </body>
    </html>

This is the pom.xml file

    <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.mycompany</groupId>
      <artifactId>DemoMVC</artifactId>
      <packaging>war</packaging>
      <version>1.0-SNAPSHOT</version>
        <properties>
            <maven.compiler.source>17</maven.compiler.source>
            <maven.compiler.target>17</maven.compiler.target>
        </properties>
        <name>DemoMVC 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-context</artifactId>
          <version>5.3.22</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.3.22</version>
        </dependency>
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>8.0.30</version>
        </dependency>
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
        </dependency>
    
    
      </dependencies>
      <build>
        <finalName>DemoMVC</finalName>
      </build>
    </project>

This is my servlet configuration file: mycompany-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:ctx="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
    
    
        <ctx:annotation-config></ctx:annotation-config>
        <ctx:component-scan base-package="com.mycompany"></ctx:component-scan>
</beans>

And my project structure looks like this: Project Structure

I'm getting the following error: The requested resource [/DemoMVC_war/sum] is not available How can i fix this?

slindenau
  • 1,091
  • 2
  • 11
  • 18
  • 1
    What is your question? – Scott Hunter Aug 11 '22 at 19:21
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – bezbos. Aug 11 '22 at 20:13
  • When I run this code on my tomcat server, it gives me this error: "The requested resource [/DemoMVC_war/sum] is not available". So I am wondering if I am doing something wrong. – yash baria Aug 12 '22 at 12:23

1 Answers1

0

With your provided files i have reproduced the WAR file and deployed it in tomcat.

When you browse to http://localhost:8080/DemoMVC/ your index.jsp is loaded, and when you enter form data you're posting to http://localhost:8080/DemoMVC/sum?t1=1&t2=2

This means the URL your spring servlet should listen to must include /sum. Currently your servlet is setup to listen to the following endpoints:

      <servlet-mapping>
        <servlet-name>telusko</servlet-name>
        <url-pattern>*.htm</url-pattern>
      </servlet-mapping>

But the requested URL /sum?t1=1&t2=2 does not include .htm, so this request is not forwarded to your Spring MVC servlet. Hence tomcat responds with a http 404 not found error.

You can solve this by updating your servlet configuration file to something like this:

    <servlet-mapping>
        <servlet-name>telusko</servlet-name>
        <url-pattern>/sum/*</url-pattern>
    </servlet-mapping>

You can read more on the working of the org.springframework.web.servlet.DispatcherServlet on the following page: https://docs.spring.io/spring-framework/docs/3.0.0.RC2/reference/html/ch15s02.html

slindenau
  • 1,091
  • 2
  • 11
  • 18
  • Upon doing that I am getting this error: " HTTP Status 500 – Internal Server Error" - Message Error instantiating servlet class [org.springframework.web.servlet.DispatcherServlet] Root cause: java.lang.NoClassDefFoundError: javax/servlet/http/HttpServlet – yash baria Aug 12 '22 at 13:18
  • I had the same; then you need to make sure your dependencies are correct: https://stackoverflow.com/questions/28325329/java-lang-noclassdeffounderror-javax-servlet-http-httpservlet Also check that you're running on a supported tomcat version: https://stackoverflow.com/questions/66741683/dispatcherservlet-cannot-be-cast-to-class-jakarta-servlet-servlet-classcastexce – slindenau Aug 12 '22 at 13:25
  • @yashbaria i've installed in on tomcat 9 now, that is supported. There was one more small error in the example code. If you name the servlet via `telusko` then the xml configuration filename must also match. So either change `mycompany-servlet.xml` to `telusko-servlet.xml`, or update the servlet and mapping name to `mycompany`. If this helped you solve your problem, please remember to upvote :). – slindenau Aug 13 '22 at 12:06