This is my first time working with Spring and I am following the tutorial on PluralSight. Running the first example, I run into the error that is listed in the title. In my console, I get errors like these. I am using older versions of the dependencies to go through this course. I've made sure to follow the tutorial closely so not sure if I messed up somewhere or something is deprecated. I am using http://localhost:8080/FitnessTracker/greeting.html
SEVERE: Context initialization failed
java.lang.IllegalArgumentException
SEVERE: StandardWrapper.Throwable
java.lang.IllegalArgumentException
SEVERE: Allocate exception for servlet fitTrackerServlet
java.lang.IllegalArgumentException
HelloController.java
package com.pluralsight.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";
}
}
servlet-config.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: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.pluralsight.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
</beans>
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>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>fitTrackerServlet</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>fitTrackerServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<display-name>Archetype Created Web Application</display-name>
</web-app>
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.pluralsight</groupId>
<artifactId>FitnessTracker</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>FitnessTracker 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>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>FitnessTracker</finalName>
</build>
</project>
I had the same problem with the same PluralSight tutorial. I had both Java 1.7 and Java 1.8 installed. The project apparently throws an exception with Java 1.8. To rectify this situation you need to ensure that your Spring STS environment is set to use Java 1.7.
Right click on the project and select Properties.
In the Properties dialog, select Java Compiler.
At the bottom of the dialog, select the link to configure the Installed JREs.
Add a definition to point to JRE 1.7 and select this as the default.
The PluralSight tutorial now works for me. I am beginner to Java and Spring MVC myself so I am not sure if this was the best way to solve the problem.
Well, spring 3.2 needs java 7 or earlier. Instead of changing project properties, one another solution is just change the version number of spring mvc in pom.xml to 4.2.4.RELEASE instead of 3.2.0.RELEASE that you have used earlier.
Now, you can proceed normally with java 8.
Below, I show the whole pom.xml with only change that I mentioned above.
<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.pluralsight</groupId>
<artifactId>FitnessTracker</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>FitnessTracker 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>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>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>FitnessTracker</finalName>
</build>
</project>
Try with 4.1.5. It works for me.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
Try to use 3.2.3.RELEASE version wherever you have spring based dependency
I faced the same issue.
So what you have to do is to use Tomcat v7.0,
It is because I was using Tomcat v9.0 and it does not work with JRE7 but JRE8.
So
Set your application with Tomcat v7.0 and set your STS or Eclipse default build path with JRE7 .
Restart your Tomcat server.
Go to your browser and paste the above link and Here you Go !!! :)
To fix this, simply change your servlet container's runtime environment to Java 7. In Eclipse:
Preferences -> Server -> Runtime Environments
Select the server you are running the project under and click Edit....
From the JRE section, select the Java 7 JRE. Note that in order to be able to do this, you first need to download and install Java 7, and make the Java 7 JRE available in Eclipse. To make Java 7 JRE available in Eclipse:
Download Java 7 and install it.
In Eclipse, select Preferences -> Java -> Installed JREs and click Search.... If you have installed Java 7 to its default path, Eclipse should be able to discover Java 7 automatically.
After adding Java 7 to Eclipse, you can select JRE 7 as the runtime environment of your servlet container.
I would also advise indicating the runtime environment being used in the server's name. For example, in Eclipse, instead of letting the server name simply be "Apache Tomcat v7.0", make it "Apache Tomcat v7.0 - JRE 7". Of course, you should update this name if you change the runtime environment of this server later.
Source: http://www.codejava.net/ides/eclipse/how-to-change-java-runtime-environment-for-tomcat-in-eclipse
NOTE: This answer applies to any other IDE as well. You just need to make sure that your servlet container runs under JRE 7.
Related
After debugging for a few hours, I am seeking help to set up a REST API using Jersey in Java. I am using a Tomcat 6 server and the following is my file structure of the Mavin project:
On the right, the resource which I want to expose can be seen.
For the web.xml I have a file as follows:
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details https://github.com/jax-rs -->
<web-app version="2.5">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.howtodoinjava.demo</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
and for the pom.xml as follows:
<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.howtodoinjava.demo</groupId>
<artifactId>JerseyArcheTypeDemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>JerseyDemo</name>
<build>
<finalName>JerseyArcheTypeDemo</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<!-- uncomment this to get JSON support
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
-->
</dependencies>
<properties>
<jersey.version>2.20</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
I can build the Maven project and start the server. However if I want to add resources to the Server by right-clicking on the server name and clicking on "Add or Remove..." I get a message that I cannot select any resources. I followed the adivce in this question: There are No resources that can be added or removed from the server but it did not help and I also do not get any error messages which could lead me. Any recommendation what I am doing wrong?
When I want to run the Resource on the server by selecting "Run As" and then "Run on Server" I get the following message: The selection cannot be run on the server.
You can't select this project because Tomcat 6 does not support Servlet 4.0 projects. Servlet 4.0 requires Tomcat 9.x, which itself requires Java 1.8. See the Tomcat documentation.
You can lower the facet version, but it makes far more sense to simply use the newer Tomcat version. Tomcat 6 is far past its end-of-life.
I go over the internet to figure it out why I receive 404 I tried almost all solutions and didn't help. I have:
Eclipse Photom
Tomcat 9
Java version 1.8
Maven project using Jersey 2.27
When I hit http://localhost:8080/Test/rest/testservice I got 404 "Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists."
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>Test</groupId>
<artifactId>com.test</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>com.test 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.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<finalName>com.test</finalName>
</build>
</project>
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>Test Jersey Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Test Jersey Service </servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
PROJECT STRUCTURE AND JAVA CLASS
I Have added in Deployment Assembly -> Maven Dependencies also
I solved it. So if anyone experience problems with 404 even the fact that you did everything right then double check these steps:
Always save (CTR + S) pom.xml and web.xml when you make any kind of
modifications
Replace index.jsp with index.html
Right click the project go to Maven -> Update Project
Right click the project go to Run as -> Run on Server (if I run it
manually it doesn't work, but when I go from run as it is working
correctly)
I have created a tutorial with step by step. If anyone wanted please contact me.
What I Have
Tomcat 9.0.7
WildFly 12.0.0
JDK 10
Maven 3.5.3
Relevant Files
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.example.training</groupId>
<artifactId>BigVoiceWebApp</artifactId>
<name>BigVoiceWebApp Maven Webapp</name>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>1.10</maven.compiler.source>
<maven.compiler.target>1.10</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<!-- <build>
<finalName>${project.artifactId}</finalName>
</build>-->
<profiles>
<profile>
<id>release-profile</id>
<build>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<build>
<finalName>BigVoiceWebApp</finalName>
</build>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>Archetype Created Web Application</display-name>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/PostgresDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
My Problem
I have servlets like this:
#WebServlet(name = "MemberController", urlPatterns = {"/MemberController"})
#MultipartConfig
public class MemberController extends HttpServlet {
The problem now, WildFly somehow does not obey annotations. When I access the URL, this is what I got in my browser (copied by viewing source HTML):
<html>
<head><title>Error</title></head>
<body>Not Found</body>
</html>
I have to declare everything in web.xml (adding to previous file) for what I've declared as annotations.
<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>com.example.training.controller.MemberController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/MemberController</url-pattern>
</servlet-mapping>
When I reupload the exact same WAR file to Tomcat, the annotations are read correctly.
My question is, why? And how to make WildFly recognize those annotations?
Thanks for the file. It's Java 10. I noticed during the build I get:
[INFO] --- maven-war-plugin:2.2:war (default-war) # SampleForError ---
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/home/scott/.m2/repository/com/thoughtworks/xstream/xstream/1.3.1/xstream-1.3.1.jar) to field java.util.Properties.defaults
WARNING: Please consider reporting this to the maintainers of com.thoughtworks.xstream.core.util.Fields
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
A similar warning is produced when Wildfly starts up. Some combination of Wildfly 12 and Java 10 doesn't work right in JEE7 mode.
However, there are multiple workarounds. If you run Wildfly in EE8 Preview Mode then it works. Do this by running either bin/standalone.sh -Dee8.preview.mode=true or bin/standalone.sh -c standalone-ee8.xml
Otherwise, this works without any changes in Java 8.
As an aside, you're trying very hard to confuse any container you're running in. You specify servlet spec 4 in your pom.xml but then your web.xml is:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
So your web.xml tells the container that you're using Servlet spec 2.3. If you really need servlet spec 4.0 then your web.xml should be something like:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app>
I am creating simple spring-mvc application referring this url. I created maven web project by referring url.
After creating this project some default file named 'index.jpg' is generated in this project. And when I build and run this project it was shpoing content of 'index.jsp' file.
Now I edited content of pom.xml file and it looks like this:
<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.leader.unisys</groupId>
<artifactId>sample-application</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>sample-application Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<java.version>1.6</java.version>
<spring.version>4.0.3.RELEASE</spring.version>
<cglib.version>2.2.2</cglib.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring core & mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<!-- CGLib for #Configuration -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>${cglib.version}</version>
<scope>runtime</scope>
</dependency>
<!-- Servlet Spec -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>sample-application</finalName>
</build>
</project>
The project structure looks like as shown in the image:
Now, How can I make this project to work as per my mapping in the spring-servlet.xml file.
view resolver:
public ViewResolver getViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
content from controller:
#RequestMapping(value="/")
public ModelAndView getHomePage(){
return new ModelAndView("home");
}
web xml default content:
<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>
I want to display home.jsp after hitting http://localhost:8080/sample-application. Can someone give me the instructions to do this. I am working with maven for first time.
Move your home.jsp under WEB-INF. And then add this line in your web.xml
<welcome-file-list>
<welcome-file>home.jsp</welcome-file>
</welcome-file-list>
This <welcome-file-list> in web.xml tells the application that it has to be loaded first.
what you are asking for has nothing to do with Spring or Maven but Java EE / Web container configuration - via web.xml
Here is a link that should help ..
http://docs.oracle.com/cd/E14571_01/web.1111/e13712/configurejsp.htm#WBAPP183
Relevant parts copied over:
Welcome files are defined at the Web application level. If your server is hosting multiple Web applications, you need to define welcome files separately for each Web application. You define Welcome files using the welcome-file-list element in web.xml. (The web.xml file is located in the WEB-INF directory of your Web application.) The following is an example Welcome file configuration:
Welcome File Example
<servlet>
<servlet-name>WelcomeServlet</servlet-name>
<servlet-class>foo.bar.WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WelcomeServlet</servlet-name>
<url-pattern>*.foo</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/welcome.foo</welcome-file>
</welcome-file-list>
hope this helps.
When we start the web application, by default web container looks for the welcome-file-list present in web.xml, if the welcome-file-list does not exist in web.xml then the container will look for index.html,index.htm, and index.jsp under the web-content folder in case of a normal dynamic web project, but incase of maven project container looks under the web-app folder. Container follows the order and if the first file exists it will execute that file and if it doesn't exist, the container follows the order until the third file. If any one of the files doesn't exist container will throw a 404 error.
For reference https://www.javatpoint.com/welcome-file-list
The above flow is the same for both dynamic and maven web projects, the only thing that differs from each other is that we have a web-content folder in case of dynamic WP and a web-app folder incase of maven WP
I want to deploy my JSF2 app on tomcat using mvn tomcat:run. I did compile it correctly previously using the mvn clean install command.
Tomcat is saying to me INFO: Starting Coyote HTTP/1.1 on http-8080
I wonder if I did configure my welcome page correctly.
This is my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="2.5">
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>home.xhtml</welcome-file>
</welcome-file-list>
</web-app>
And this is my pom.xml just to double check that the artifactid is correct:
<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>org.bogus</groupId>
<artifactId>jsf2_tutorial</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>jsf2_tutorial Maven Webapp</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.1.0-b03</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.1.0-b03</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
Just i am going to paste the last bit, the home.xhtml to make sure it is also correct:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Hello JSF 2!</title>
</h:head>
<h:body>
#{welcome.message}
</h:body>
</html>
The error when I navigate to the welcome page is:
HTTP status 500
In the url I notice I had a typo. Now I enter the correct URL but the problem is:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Class javax.faces.webapp.FacesServlet is not a Servlet
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
java.lang.Thread.run(Thread.java:662)
root cause
java.lang.ClassCastException: javax.faces.webapp.FacesServlet cannot be cast to javax.servlet.Servlet
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
java.lang.Thread.run(Thread.java:662)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.29 logs.
Update
I got rid of the exception by removing the servlet-api dependency from the pom.xml
<dependency> <groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId> <version>2.5</version>
</dependency>
The page now loads but the message is not being displayed.
This is how the java part of this app looks like:
package org.bogus;
import javax.faces.bean.ManagedBean;
#ManagedBean(name = "welcome", eager = true)
public class WelcomeBean {
public WelcomeBean() {
System.out.println("WelcomeBean instantiated");
}
public String getMessage() {
return "I'm alive!";
}
}
This example is almost the same as in the official JSF tutorial at oracle: http://docs.oracle.com/javaee/6/tutorial/doc/gjaam.html
I included the <load-on-startup>1</load-on-startup> but still I don't see the message in the console or in the page.
Maybe there is still something wrong with the configurations.
Any idea?
Update
I just included the faces-config.xml file under WEB-INF but still does not work:
<?xml version="1.0" encoding="UTF-8"?>
<!-- This file is not required if you don't need any extra configuration. -->
<faces-config version="2.1"
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-facesconfig_2_1.xsd">
<!-- Write your navigation rules here. -->
<application>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>en</supported-locale>
</locale-config>
</application>
</faces-config>
You've basically 2 problems:
JSF2 requires a minimum of Servlet 2.5 (Tomcat 6 compatible), yet your web.xml is declared conform Servlet 2.3 (Tomcat 4 compatible). You should declare your web.xml to highest version compatible with the servletcontainer. Given that it's Tomcat 6, which is a Servlet 2.5 container, declare it as Servlet 2.5 instead.
<?xml version="1.0" encoding="UTF-8"?>
<web-app
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"
version="2.5">
<!-- Config here. -->
</web-app>
(yes, without the doctype!)
#ManagedBean of Mojarra 2.1.0 doesn't work on Tomcat and Jetty due to accidently inserted Glassfish-specific code in the annotation scanning code. See also issue 1937. You need to upgrade to at least Mojarra 2.1.1 (or just the currently available 2.1.17). An alternative is to manually register the managed bean in faces-config.xml, but that's plain clumsy.
Unrelated to the concrete problem, the #ManagedBean(eager=true) works on #ApplicationScoped only. Just omit it.