Tomcat8 - default error page not working - web.xml

Tomcat 8
Servlet 3.1
I'm trying to get default error page to work. This post, for example, is what I'm trying to do.
My web.xml
<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_3_1.xsd"
version="3.1">
...
<!--
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
-->
<error-page>
<location>/error.jsp</location>
</error-page>
When I remove the comments I see my error page, when trying to access a non-existent URL. With the comments in, trying the same URLs, my browser just returns a 404.
I can't find anything that says this behaviour changed between 3.0 and 3.1 and I can't see any errors in the tomcat logs.

Related

JSF with Prettyfaces returning cached error page

I'm developing a web application using JSF 2.2 and tomcat 8, but I'm facing a weird problem...
For example, I have a home page which will call a managedbean, connect to databse and return response. Everything is working, but, if some error happens, it's going to redirect to my error page (500.xhtml, which I set up in my web.xml file).
After the error happens, I fixed the error, but I can't access the home page anymore, it always redirect me for my 500 page. If, i change the the home page for something like home?anything it call my managebean again and works. Also, if i change the server, from localhost to my ip or 127.1.0.0, it also makes the request again.
Following, my web.xml. Also, I'm using prettyfaces 3.4, don't know if it's related but...
Tomcat server.xml and web.xml is default config.
web.xml
<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_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>JSF Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JSF Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>home.xhtml</welcome-file>
</welcome-file-list>
<context-param>
<param-name>com.sun.faces.defaultResourceMaxAge</param-name>
<param-value>3628800000</param-value> <!-- 6 weeks. -->
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<mime-mapping>
<extension>xhtml</extension>
<mime-type>application/xml</mime-type>
</mime-mapping>
<mime-mapping>
<extension>jsp</extension>
<mime-type>application/xml</mime-type>
</mime-mapping>
<error-page>
<error-code>403</error-code>
<location>/error/403.xhtml</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/error/404.xhtml</location>
</error-page>
<error-page>
<error-code>405</error-code>
<location>/error/405.xhtml</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error/500.xhtml</location>
</error-page>
<error-page>
<error-code>503</error-code>
<location>/error/503.xhtml</location>
</error-page>
Prettyfaces:
#RewriteConfiguration
public class ConfigurationProvider extends HttpConfigurationProvider {
#Override
public Configuration getConfiguration(final ServletContext context) {
return ConfigurationBuilder.begin()
.addRule(Join.path("/home").to("/"))
.addRule(Join.path("/home").to("/home.xhtml").withInboundCorrection())
.addRule(Join.path("/company").to("/company.xhtml").withInboundCorrection())
.addRule(Join.path("/portfolio").to("/portfolio.xhtml").withInboundCorrection())
.addRule(Join.path("/contact").to("/contact.xhtml").withInboundCorrection())
.addRule(Join.path("/admin/login").to("/admin/login.xhtml").withInboundCorrection())
.addRule(Join.path("/admin/recovery").to("/admin/recovery.xhtml").withInboundCorrection())
.addRule(Join.path("/403").to("/error/403.xhtml").withInboundCorrection())
.addRule(Join.path("/404").to("/error/404.xhtml").withInboundCorrection())
.addRule(Join.path("/405").to("/error/405.xhtml").withInboundCorrection())
.addRule(Join.path("/500").to("/error/500.xhtml").withInboundCorrection())
.addRule(Join.path("/503").to("/error/503.xhtml").withInboundCorrection());
}
#Override
public int priority() {
return 10;
}
}
Looking at firefox debugger, network tab shows there was a request to home page (which was cached) and the a redirect with 301 code to 500.
#EDIT
It seems to be something with prettyfaces.
I just remove it, and tested with chrome browser (without cache), after the error, i can access the page again. Then, I activate prettyfaces again, and the problem returned.
Any idea why that?

welcome page after running dynamic web project [duplicate]

This question already has answers here:
Set default home page via <welcome-file> in JSF project
(3 answers)
Closed 6 years ago.
I'm trying to let a welcome page displaying after running a dynamic web project.
When googling I found a lot of tutorials but I can't found the solution.
I share:
The structure of my project (I want that the page welcome.xhtml will displayed by default).
The file web.xml is:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID"
version="3.0">
<display-name>HiSpring</display-name>
<welcome-file-list>
<welcome-file>welcome.xhtml</welcome-file>
</welcome-file-list>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<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>/faces/*</url-pattern>
</servlet-mapping>
</web-app>
Once clicking on HiSpring > Run As > Run on Server, I should have
This url: http://localhost:8080/HiSpring/faces/welcome.xhtml. However, I got http://localhost:8080/HiSpring/.
Could you please tell me what I missed; Thanks in advance.
According to your web.xml tomcat would look for /welcome.xhtml and has no clue that this might be in /faces/welcome.xhtml. The URL is fine (if welcome.xhtml is in /)
There might be a more elegant JSF version (I'm not much of a JSF guy) - some possible solutions that come to my mind would be
map the faces servlet to *.xhtml do remove the /faces/ part from path (you judge if this is proper JSF - comment if it's not and I'll remove this part)
create a separate explicit redirection, e.g. through a (stupidly simple) index.html (of course, declare index.html as another welcome file) like the following sample:
<html>
<head><title>Redirection</title>
<meta http-equiv="refresh" content="2;URL=/faces/">
<!-- 2 means 2 seconds delay. Change as you like -->
</head>
<body>
<p>redirecting to application. Click here if it doesn't work</p>
</body>
</html>

Why does my Spring MVC application not work when changing the server mapping?

I'm working on the Spring MVC "FitnessTracker" application outlined on Pluralsight. Below is my "web.xml" file:
<?xml version="1.0" encoding="UTF-8"?>
<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>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>/FitnessTracker/*.html</url-pattern>
</servlet-mapping>
<display-name>Archetype Created Web Application</display-name>
</web-app>
The above makes Tomcat generate a bunch of exceptions, starting with
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:899)
But when I change what's between the <url-pattern> tag to *.html, it works fine. Why is that?
Note: My goal is to try to make my app's controller run when I type /FitnessTracker/greeting.html, instead of /greeting.html. I am using Intellij IDEA, and doing Maven project with Tomcat 7.0 as my server.
Application runs # http://localhost:9090/FitnessTracker/greeting.html URL . FitnessTracker is application root context and greeting.html is mapped to Hello controller method. Please see below.
Could you please post the web.xml and controller mapping .
The original code runs at the URL - http://localhost:8080/FitnessTracker/greeting.html. SO I am not sure why you need to change web.xml for that.
Also the URL pattern you are trying to use "/FitnessTracker/*.html" is not valid.
More details here.
https://stackoverflow.com/a/5441862/6352160

Spring Websocket, 404 error while connecting. App not using Spring MVC

I'm trying to get working the Spring Example tutorial:
https://spring.io/guides/gs/messaging-stomp-websocket/
But I need it working on an existing Application that I need to add WebSocket support. The idea is once I get the basic working, start building from there.
The differences I have with the example in the URL is:
I'm not using SpringBootApplication, but Tomcat instead (7.0.69)
That implies I do have a web.xml (included below)
I skipped the Application.java with the main... I'm building a WAR and deploying it manually into Tomcat.
Whem I start Tomcat I read the following which seems relevant:
03:06:52,908 INFO SimpleBrokerMessageHandler:157 - Starting...
03:06:52,908 INFO SimpleBrokerMessageHandler:260 - BrokerAvailabilityEvent[available=true, SimpleBrokerMessageHandler [DefaultSubscriptionRegistry[cache[0 destination(s)], registry[0 sessions]]]]
03:06:52,913 INFO SimpleBrokerMessageHandler:166 - Started.
The problem is the following:
When I click on connect on the index.html, I get a 404 when I'm trying to reach the server at the mapping url "/hello" (not sure why info is there, I guess is part of the protocol..) http://localhost:8080/hello/info
I was googling this, even found some answers in stackoverflow. Some have fixed this addding as prefix the DispatcherServlet mapping to the websocket url...
However my app isn't using Spring MVC, so I don't have a DispatcherServlet configured... and I looks like an overkill to include it just for the WebSockets.
I tried adding the annotation #EnableWebMvc to the WebSocketConfig class (in the link is the code, I have the same lines)
Any suggestion will be much appreciated !
WEB.XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>App</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
I give up trying to avoid having an MVC Dispatcher for an APP witch doesn't use Spring MVC.
This solved it:
web.xml
<servlet>
<!-- Only used by websockets -->
<servlet-name>SpringMVCDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/websockets-application-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVCDispatcherServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
And then, in the index.html I only added the prefix /ws/ when connecting (only there, sinding messages without the prefix is ok).

<async-supported>true</async-supported> in web.xml

Someone help me plss, i got this error when i put async-supported tag in web.xml:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'async-supported'. One of '{"http://java.sun.com/xml/ns/javaee":run-as, "http://java.sun.com/xml/ns/javaee":security-role-ref}' is expected.
this is my 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>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.yeditepeim.messenger.resources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
The web.xml goes of an XML schema. If you're not familiar with XML schemas, they describe what elements and attributes an XML document can contain in order to be a valid instance of that schema.
That being said, you can see in the schema location the version of the schema file being used, i.e. ...web-app_2_5.xsd. This means that your web.xml is going to be based on that version of the schema, which maps to that version of the servlet specification, which in your case is 2.5. The problem with this is that async is not introduced to the servlet spec until 3.0. So there is no element specification in the 2.5 schema. So when the xml is being validated, it's saying that not such element <async-supported> is allowed in the document, as it doesn't comply to the schema.
To fix it, just change the version to 3.0 and the schema file to 3_0
<!-- change to 3.0 -->
<web-app version="3.0"
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_3_0.xsd">
<!-- change to 3_0 -->

Resources