-1

I am working on the this example. I was receiving the same error message. I have modified the code as mentioned the answer, I am not receiving the below error message anymore:

Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]

However, I am not able to redirect to the jsp. Please find code details below:

Project Structure:

enter image description here

Error Message: enter image description here

Dependencies: enter image description here web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    version="4.0">
    <display-name>exSpringJDBC</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>

    <servlet>
        <servlet-name>offers</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value> 
            classpath:org/exSpringJDBC/config/exSpringDao.xml
        </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
        <servlet-name>offers</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

Offers-servlet.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.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-4.3.xsd">


    <context:annotation-config></context:annotation-config>
    <context:component-scan
        base-package="org.exSpringJDBC.Controller">
    </context:component-scan>
    <mvc:annotation-driven></mvc:annotation-driven>
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsps/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

Hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    exSpringJDBC successful.
</body>
</html>

ExSpringController.java

package org.exSpringJDBC.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ExSpringController {
    
    @RequestMapping("/")
    public String Hello() {
        return "Hello";
    }
}

exSpringDao.xml -> I have to configure to Dao but it is not yet done as I am still working on the error.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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.xsd">


</beans>

Console Output:

Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version name:   Apache Tomcat/9.0.34
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Apr 3 2020 12:02:52 UTC
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version number: 9.0.34.0
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 10
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            10.0
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jdk-14.0.1
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           14.0.1+7
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         D:\new_Eclipse_Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         C:\Program Files\Apache Software Foundation\Tomcat 9.0
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=D:\new_Eclipse_Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\Program Files\Apache Software Foundation\Tomcat 9.0
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=D:\new_Eclipse_Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Jul 26, 2020 6:05:59 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Jul 26, 2020 6:05:59 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: Loaded APR based Apache Tomcat Native library [1.2.23] using APR version [1.7.0].
Jul 26, 2020 6:05:59 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
Jul 26, 2020 6:05:59 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
Jul 26, 2020 6:05:59 PM org.apache.catalina.core.AprLifecycleListener initializeSSL
INFO: OpenSSL successfully initialized [OpenSSL 1.1.1c  28 May 2019]
Jul 26, 2020 6:06:00 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-9591"]
Jul 26, 2020 6:06:00 PM org.apache.catalina.startup.Catalina load
INFO: Server initialization in [1,323] milliseconds
Jul 26, 2020 6:06:00 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Catalina]
Jul 26, 2020 6:06:00 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.34]
Jul 26, 2020 6:06:02 PM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
WARNING: Name = myoracle Property maxActive is not used in DBCP2, use maxTotal instead. maxTotal default value is 8. You have set value of "20" for "maxActive" property, which is being ignored.
Jul 26, 2020 6:06:02 PM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
WARNING: Name = myoracle Property maxWait is not used in DBCP2 , use maxWaitMillis instead. maxWaitMillis default value is -1. You have set value of "-1" for "maxWait" property, which is being ignored.
Jul 26, 2020 6:06:02 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Jul 26, 2020 6:06:02 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
WARNING: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [107] milliseconds.
Jul 26, 2020 6:06:06 PM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
WARNING: Name = myoracle Property maxActive is not used in DBCP2, use maxTotal instead. maxTotal default value is 8. You have set value of "20" for "maxActive" property, which is being ignored.
Jul 26, 2020 6:06:06 PM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
WARNING: Name = myoracle Property maxWait is not used in DBCP2 , use maxWaitMillis instead. maxWaitMillis default value is -1. You have set value of "-1" for "maxWait" property, which is being ignored.
Jul 26, 2020 6:06:06 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Jul 26, 2020 6:06:07 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Jul 26, 2020 6:06:07 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring DispatcherServlet 'offers'
Jul 26, 2020 6:06:07 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: Initializing Servlet 'offers'
Jul 26, 2020 6:06:07 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: Completed initialization in 447 ms
Jul 26, 2020 6:06:09 PM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
WARNING: Name = myoracle Property maxActive is not used in DBCP2, use maxTotal instead. maxTotal default value is 8. You have set value of "20" for "maxActive" property, which is being ignored.
Jul 26, 2020 6:06:09 PM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
WARNING: Name = myoracle Property maxWait is not used in DBCP2 , use maxWaitMillis instead. maxWaitMillis default value is -1. You have set value of "-1" for "maxWait" property, which is being ignored.
Jul 26, 2020 6:06:09 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Jul 26, 2020 6:06:09 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Jul 26, 2020 6:06:09 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-9591"]
Jul 26, 2020 6:06:09 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in [8,739] milliseconds
Jul 26, 2020 6:06:09 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
**WARNING: No mapping for GET /exSpringJDBC/**

Thanks,
Sanjay Chauhan

Solution: Issue was with the web.xml. Updated web.xml as mentioned in the answer and issue got resolved. I have paste final web.xml for reference.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    version="4.0">
    <display-name>exSpringJDBC</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>

    <servlet>
        <servlet-name>offers</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value> 
            /WEB-INF/offers-servlet.xml
            classpath:org/exSpringJDBC/config/exSpringDao.xml
            classpath:org/exSpringJDBC/config/OfferService-Context.xml
        </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
        <servlet-name>offers</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
Wolfgang
  • 515
  • 1
  • 11
  • 41

1 Answers1

1

You are trying to load a jsp with name "home", but you don't have this page. You have only "hello" jsp

Change this

@RequestMapping("/")
public String home() {
    return "Home";
}

On this

@RequestMapping("/")
public String home() {
    return "Hello";
}

Also change your path to the configuration file

Change this

<servlet>
    <servlet-name>offers</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value> 
        classpath:org/exSpringJDBC/config/exSpringDao.xml
    </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

On this

<servlet>
    <servlet-name>offers</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/offers-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
3asyPe
  • 254
  • 1
  • 6