0

I am trying to have hello.jsp page as an output but it goes to index.jsp page all the time. I tried searching various other things but failed, so finally decided to post here so any one of you can help. Below I have attached my project structure and all the files...and also the output what I am seeing when I run on tomcat server.

Project Structure

pom.xml

<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.gami</groupId>
  <artifactId>healthTracker</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>healthTracker 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-webmvc</artifactId>
        <version>3.2.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>healthTracker</finalName>
  </build>
</project>

Web.xml

<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <servlet>
        <servlet-name>healthTrackerServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/servlet-config.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>healthTrackerServlet</servlet-name>
        <url-pattern>*.html</url-pattern>

    </servlet-mapping>

    <display-name>Archetype Created Web Application</display-name>
</web-app>

servlet-config

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

    <mvc:annotation-driven/>
    <context:component-scan base-package="com.gami.controller"/>


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


<!--     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
     p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> -->

</beans>

HelloController.java

package com.gami.controller;

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

@Controller
public class HelloController {

    @RequestMapping(value ="greeting")
    public String sayHello (Model model) {

        model.addAttribute("greeting", "Hello World");

        return "hello";
    }

hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>${greeting}</h1>
</body>
</html>

index.jsp

<html>
<body>
<h2>This is index - Hello World!</h2>
</body>
</html>

This is the output I get when I run the project on server

These are logs, server was started properly

Grant Foster
  • 722
  • 2
  • 11
  • 21

4 Answers4

0

healthTracker is your root context.

Your controller path is accessible at /greeting:

@RequestMapping(value ="greeting")

So if you want to access the hello page, your url should be:

http://localhost:1615/healthTracker/greeting
Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51
  • Hello, I tried accessing using http://localhost:1615/healthTracker/greeting But it still showed me error.. Type Exception Report Message Servlet.init() for servlet [healthTrackerServlet] threw exception Description The server encountered an unexpected condition that prevented it from fulfilling the request. Exception javax.servlet.ServletException: Servlet.init() for servlet [healthTrackerServlet] threw exception Root Cause java.lang.IllegalArgumentException – Hardik Gami May 12 '18 at 17:29
0

Update your servlet-mapping in the deployment descriptor(web.xml)

<servlet-mapping>
        <servlet-name>healthTrackerServlet</servlet-name>
        <url-pattern>/*</url-pattern>

</servlet-mapping>

First, check your server is running on 1615 or not

If yes, then

Basic URL for accessing a resource is of the following form

https://<IP>:<Listener-PORT>/<ROOT-CONTEXT-NAME>/<URL-SPECIFIED-FOR-RESOURCE>

So use: http://localhost:1615/healthTracker/greeting

Also, recommend you to specify the request method in the @RequestMapping by default it will be GET

@RequestMapping(value ="greeting" , method = RequestMethod.POST)
    public String sayHello (Model model) {

        model.addAttribute("greeting", "Hello World");

        return "hello";
    }

If you want to make a page a default page of your application. There is another way of doing that just specify the page in web.xml

<web-app>  
 ....  

  <welcome-file-list>  
   <welcome-file>hello.html</welcome-file>  
  </welcome-file-list>  
</web-app>  
Yati Sawhney
  • 1,372
  • 1
  • 12
  • 19
0

If you want hello.jsp as default one you have to keep some snippet in index.jsp keep <jsp:forword page="greeting">

and in web.xml change to

<servlet-mapping>
        <servlet-name>healthTrackerServlet</servlet-name>
        <url-pattern>/*</url-pattern>

</servlet-mapping>
0

You can try the below to fix this issue.

Briefly your web application will look into welcome page and /redirect is invoked which is captured by controller class and execute the logic you have implemented.

Add this in web.xml

 <welcome-file-list>
        <welcome-file>hello.jsp</welcome-file>
    </welcome-file-list>

In the JSP itself, add below coding:

<c:redirect url="/directMe"/>

Lastly, in the controller:

@RequestMapping(value = "/directMe", method = RequestMethod.GET)
        public void myMethod(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException { 
// add your logic to execute
}
Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51