I would like to be able to receive a request on a servlet I made, then analyse the URL and configure the application database based on that, and after the DB is configure forward the request to the regular Faces Servlet.
How should I configure my web.xml? And how can I forward the request to the Faces Servlet?
Here is what I was thinking:
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>aws</servlet-name>
<servlet-class>servlets.aws</servlet-class>
</servlet>
<!-- <servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>-->
<servlet-mapping>
<servlet-name>aws</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
System.out.println("configure DB based on URL");
RequestDispatcher rd = request.getRequestDispatcher("FacesServlet");
rd.forward(request, response);
}
However that gives the following error:
Related
I am trying to configure a normal (non-ajax) request error page using Omnifaces library. I am able to use the FullAjaxExceptionHandler and with ajax request error and its page as shown in the demo. When I use the same error page with normal request the error page is shown, but the values are displayed as source code (for example Date/time: #{of:formatDate(now, 'yyyy-MM-dd HH:mm:ss')} User agent: #{header['user-agent']} .. are displayed in the browser as it is).
I am using Tomcat 7, JSF 2.2 (MyFaces), Weld 2.6 (for CDI), Omnifaces 2.0 and Primefaces 5.1. The following is the relevant code.
The page:
<h:commandButton value="Throw runtime exception on normal request"
action="#{appbean.throwRuntimeException}"/>
<p:commandButton value="Throw runtime exception on AJAX request"
action="#{appbean.throwRuntimeException}"/>
The bean:
public void throwRuntimeException() {
throw new RuntimeException("peek-a-boo");
}
faces-config:
<factory>
<exception-handler-factory>
org.omnifaces.exceptionhandler.FullAjaxExceptionHandlerFactory
</exception-handler-factory>
</factory>
web.xml:
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>facesExceptionFilter</filter-name>
<filter-class>org.omnifaces.filter.FacesExceptionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>facesExceptionFilter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<error-page>
<exception-type>java.lang.RuntimeException</exception-type>
<location>/WEB-INF/errorpages/error2.xhtml</location>
</error-page>
The <error-page><location> must match the FacesServlet mapping in order to get FacesServlet to run on the error page too during an exception on a synchronous request (which doesn't use ViewHandler#renderView(), but RequestDispatcher#forward()).
Alter the mapping accordingly:
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
Using /faces/* (and *.faces) is soo JSF 1.0/1.1. If you really need to keep /faces/* for some reason (e.g. existing webapp with already published URLs), then just use both (and migrate with 301s accordingly):
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
See also:
JSF returns blank/unparsed page with plain/raw XHTML/XML/EL source instead of rendered HTML output
I'm trying to implement a custom PDF file servlet (/fileprovider/name?param1=value¶m2=otherValue) on a JSF 2 web application, but when declaring it on web.xml the application always adds .xhtml extension to every request. I believe due to the DEFAULT_SUFFIX context-param.
<!-- ... -->
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>org.omnifaces.FACES_VIEWS_SCAN_PATHS</param-name>
<param-value>/*.xhtml</param-value>
</context-param>
<servlet>
<servlet-name>faces</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>MyPDFServlet</servlet-name>
<servlet-class>com.company.servlet.MyPDFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyPDFServlet</servlet-name>
<url-pattern>/fileprovider/name</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>faces</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<!-- ... -->
The exception thrown is:
FacesFileNotFoundException: /name.xhtml Not Found in ExternalContext as a Resource
So I believe that the path is not mapped correctly, maybe I'm missing something.
Is there any way to specify that I want specific paths to point to custom servlets without adding .xhtml extension?
Getting a strange error.
I have a few different files in WebContent.
WebContent/page1.xhtml
WebContent/page2.xhtml
WebContent/page3.xhtml
I have my web.xml set up like this:
<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>
...
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/01/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/02/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/03/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/04/*</url-pattern>
</servlet-mapping>
When running the web server (JBoss EAP 6.1), I can get to /myapp/01/page1.xhtml, /myapp/01/page2.xhtml, etc. This seems to be the case for everything except for /myapp/03/.
For /myapp/03/, I can get to page2.xhtml just fine, but when I try to get to page1.xhtml I am getting a java.io.FileNotFoundException. I can get to page1.xhtml just fine from all of the other combinations (/myapp/01/, /myapp/02/, /myapp/04/, etc. all the way to 10).
All of the /myapp/##/ stuff goes through the same filter, and the java.io.FileNotFoundException is happening when the chain.doFilter() method is called, but it works fine for all of the other sections, so I don't know if it could be in there or not.
Any ideas would be greatly appreciated.
The root cause of this issue was a path error. The site was set up to use multiple include files, some of the paths for which were set via code. I needed to review every path to ensure that the correct files were being found.
I am developing a jsf2 + spring3 application with the following filesystem:
webapp
-resources (folder)
-WEB-INF (folder)
--applicationContext.xml
--faces-config.xml
--web.xml
-buy.xhtml
-company.xhtml
-home.xhtml
-sell.xhtml
And I got this in my web.xml:
<welcome-file-list>
<welcome-file>home.jsf</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>facesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
My faces-config.xml got nothing about navigation.
When I go to the following URL "http://:8080/website/" I got the "home.jsf" view.
I need to go to the same "home.jsf" view "http://:8080/website/home.jsf" when I write the following URL "http://:8080/website/caminha/"
How can I achieve this? What I need to change in web.xml or in faces-config.xml? Thanks!
For my app I'm using Tomcat 6.0.x and Mojarra 1.2_04 JSF implementation.
It works fine, just I would like to switch now to MyFaces 1.2_10 impl of JSF.
During the deployment of my app a get the following error:
ERROR [org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/myApp]] StandardWrapper.Throwable
java.lang.IllegalStateException: No Factories configured for this Application. This happens if the faces-initialization does not work at all - make sure that you properly include all configuration settings necessary for a basic faces application and that all the necessary libs are included. Also check the logging output of your web application and your container for any exceptions!
If you did that and find nothing, the mistake might be due to the fact that you use some special web-containers which do not support registering context-listeners via TLD files and a context listener is not setup in your web.xml.
A typical config looks like this;
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
at javax.faces.FactoryFinder.getFactory(FactoryFinder.java:106)
at javax.faces.webapp.FacesServlet.init(FacesServlet.java:137)
at org.apache.myfaces.webapp.MyFacesServlet.init(MyFacesServlet.java:113)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1172)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:992)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4058)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4371)
...
Here is part of my web.xml configuration:
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<!-- <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> -->
<servlet-class>org.apache.myfaces.webapp.MyFacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
...
<listener>
<listener- class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
Has anyone experienced similar error, and what should I do i order to fix it? Thanx!
EDIT:
I was able to fix the problem. Since I am using delegator for FacesServlet, it turned out that this delegator was causing the problem.
All I needed to do is to make this class implement DelegatedFacesServlet, and I've removed org.apache.myfaces.webapp.StartupServletContextListener. Here is my web.xml now:
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<!-- <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> -->
<servlet-class>org.apache.myfaces.webapp.MyFacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>Faces Servlet Delegator</servlet-name>
<servlet-class>com.myapp.web.FacesServletDelegator</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet Delegator</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
and here is FacesServletDelegator
public class PLMFacesServlet extends HttpServlet implements DelegatedFacesServlet {
private MyFacesServlet delegate;
public final void init(ServletConfig servletConfig) throws ServletException {
delegate = new MyFacesServlet();
delegate.init(servletConfig);
}
public final void destroy() {
delegate.destroy();
}
public final ServletConfig getServletConfig() {
return delegate.getServletConfig();
}
public final String getServletInfo() {
return delegate.getServletInfo();
}
public final void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {
try {
delegate.service(request, response);
} catch(Exception e) {}
}
// some other code...
}
EDIT 2:
Following BalusC advice, I've edited my web.xml a bit, here is the final version:
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>Faces Servlet Delegator</servlet-name>
<servlet-class>com.myapp.web.FacesServletDelegator</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet Delegator</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
In my case, the removal of the following line from my web.xml:
<load-on-startup>1</load-on-startup>
...was what made the error disappear.
(Was fixing up a pure Tomcat 7 to work with JSF.)