Requests not being routed to JSF Servlet - jsf

I have a JSF webapp which is exhibiting the following behaviour:
http://localhost/myapp/ returns the raw contents of index.xhtml
http://localhost/myapp/web/ returns a blank page
http://localhost/myapp/web/index.xhtml returns the error /index.xhtml Not Found in ExternalContext as a Resource
The directory structure of the webapp is shown below:
The web.xml file looks as follows:
<?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_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>myapp</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>/web/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<!-- <welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
-->
</web-app>
I have a breakpoint in the first line of the method javax.faces.webapp.FacesServlet.service
public void service(ServletRequest req,
ServletResponse resp)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
requestStart(request.getRequestURI()); // V3 Probe hook
This breakpoint is never hit. Is anyone able to shed some light on what may be wrong here or some pointers on where I can start my investigations.

JSF will trim off own URL pattern from the request URL before finding the resource. You need to put /index.xhtml file exactly there where JSF expects it as per the error message: in /index.xhtml. So, outside the /web folder. Note that you can just keep using /web in request URL.
An alternative is to just map the FacesServlet on *.xhtml. This way you don't need to worry about virtual URLs.
See also:
JSF Facelets: Sometimes I see the URL is .jsf and sometimes .xhtml. Why?

Related

Arabic is showing as ???? ???? in console and in MySQL [duplicate]

I am helping to one of my friend. He is creating web-application using JSF 2.0 & mysql.
While creating database he have used below query.
CREATE DATABASE dbName DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
Till date website is working fine. Today client tried entering Arabic text and they said that the output is coming weird. What my friend do is after entering the data to DB, he also prints the same data on another page saying Congratulations, XYZ ABC is added successfully. However he see output as Congratulations, Ù?ظاÙ? تÙ?Ù?Ù?Ø© Ù?تÙ?Ù?Ù? صدÙ?Ù? Ù?Ù?بÙ?ئة is added successfully. I don't understand why he get like that when Database characters are set properly.
web.xml content is as below.
<?xml version="1.0" encoding="UTF-8"?>
<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">
<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>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
600
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<filter>
<filter-name>restrict</filter-name>
<filter-class>com.sac.filter.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>restrict</filter-name>
<url-pattern>*.xhtml</url-pattern>
</filter-mapping>
<filter>
<filter-name>MyFacesExtensionsFilter</filter-name>
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>DisplayImage</servlet-name>
<servlet-class>com.sac.databean.DisplayImage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayImage</servlet-name>
<url-pattern>/DisplayImage</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>SaveMyImage</servlet-name>
<servlet-class>com.sac.databean.SaveMyImage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SaveMyImage</servlet-name>
<url-pattern>/SaveMyImage</url-pattern>
</servlet-mapping>
<!-- for not using css and js of default richfaces -->
<context-param>
<param-name>org.richfaces.SKIN</param-name>
<param-value>plain</param-value>
</context-param>
<context-param>
<param-name>org.richfaces.LoadStyleStrategy</param-name>
<param-value>None</param-value>
</context-param>
<context-param>
<param-name>org.richfaces.enableControlSkinning</param-name>
<param-value>false</param-value>
</context-param>
<!-- for not using css and js of default richfaces -->
</web-app>
On each .xhtml page, he have <?xml version='1.0' encoding='UTF-8' ?>
Please let me know if you need anything else.
Edit 1
In my JSF filter, I also added req.setCharacterEncoding("UTF-8"); in doFilter(). Still in Database I see ???????????
Edit 2
In JSF page I have <h:inputText value="#{PersonalInformationDataBean.fullName}"> and when I print the fullName value in Java bean as System.out.println("my name while entering is " + fullName);m I get output as my name while entering is ???????????? ????.
This means there is problem while entering data
Can someone help what is going ODD?
However he see output as Congratulations, Ù?ظاÙ? تÙ?Ù?Ù?Ø© Ù?تÙ?Ù?Ù? صدÙ?Ù? Ù?Ù?بÙ?ئة is added successfully. I don't understand why he get like that when Database characters are set properly.
This is known as Mojibake. This is not a DB encoding problem, but a HTTP encoding problem. Setting the POST request character encoding as you did is indeed the proper solution.
Edit 1: In my JSF filter, I also added req.setCharacterEncoding("UTF-8"); in doFilter(). Still in Database I see ???????????
Question marks occur when the both sides of the connection are aware of their own encoding. Sent/retrieved characters which are not covered by the encoding of one side will be replaced by question marks. Arabic characters doesn't occur in ISO-8859-1 and hence they're replaced by question marks. That's the difference with Mojibake whereby characters are been sent without checking if the encoding used by the other side really supports the character. You'll end up incorrectly encoded characters which presents itself as an unintelligible sequence of characters.
In this particular case, the JDBC driver is by itself aware that it's using ISO-8859-1 by default to transmit the characters to DB, while the retrieved characters are in UTF-8 (the MySQL JDBC driver doesn't look at the DB table encoding, even though it's properly been set to UTF-8 in your case). You need to explicitly tell the JDBC driver to use UTF-8 to decode characters before transmitting data to DB. This is to be done as JDBC connection properties which are definied as query string parameters in the JDBC URL like so:
jdbc:mysql://localhost:3306/db_name?useUnicode=yes&characterEncoding=UTF-8
If you're using a container-managed datasource, then just specify those properties separately the same way as you did for the username and password
useUnicode=yes
characterEncoding=UTF-8
See also:
Unicode - How to get the characters right?

JSF, primefaces and websphere can't find the enter page [duplicate]

This question already has answers here:
Can we use regular expressions in web.xml URL patterns?
(6 answers)
Sometimes I see JSF URL is *.jsf, sometimes *.xhtml and sometimes /faces/*. Why?
(1 answer)
Closed 4 years ago.
I use JSF, Primefaces and websphere.
I made a web project in maven with two maven modules.
But when i start the websphere server and the url appears and i click on it, does the webbrowser show that he doesn't find the page.
Is there something wrong in 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_3_0.xsd" version="3.0">
<display-name>Tinnenhoek-BackOffice</display-name>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<enabled>true</enabled>
<async-supported>false</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>
/faces/*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
The page that should be seen is main.xhtml

IDE creates xhtml instead of .jsf as extension [duplicate]

This question already has an answer here:
Sometimes I see JSF URL is *.jsf, sometimes *.xhtml and sometimes /faces/*. Why?
(1 answer)
Closed 6 years ago.
I'm so new for JavaServer Faces. I'm trying to create a project in Netbeans. (New Project >Java Web>Web Application). While creating I changed JSF Servlet URL Pattern.
It was like this:
And I changed it as "*.jsf" then created. Netbeans edited web.xml file.
<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>*.jsf</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsf</welcome-file>
</welcome-file-list>
</web-app>
But my index page is still xHTML.
Lastly, when I run to file, IDE trying to open index.html and browser can't find index.HTML so I go index.jsf and page opens.
I've been searching for a while, I just find about people suggests editing to web.xml but its already edited. So do you have any suggestion?
Then you can use below servlet-mapping
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
you can use more than one pattern as well please check below
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
<url-pattern>*.jsf</url-pattern>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
For more and detail information you can check BalusC's evergreen answers for following questions.
What is the difference between creating .xhtml or .jsp .or .jsf for JSF pages
JSF Facelets: Sometimes I see the URL is .jsf and sometimes .xhtml. Why?
JSF 2 with HTML pages instead of XHTML
For your question why its created index.html rather than index.xhtml you can check following link
Creating a new JSF project
it will tell you step by step process to create a JSF project and at last it creating index.xhtml page rather than index.html. So from this link you can get which step you missed in your case.
Please check below two screens
1. Step 1
2. Step 2
You can check highlighted(Yellow Color) text its extension is .XHTML and not .HTML

Listener method in p:fileUpload is never invoked in primefaces [duplicate]

This question already has answers here:
How to use PrimeFaces p:fileUpload? Listener method is never invoked or UploadedFile is null / throws an error / not usable
(11 answers)
Closed 7 years ago.
I am trying to upload an image with primefaces and the fileUploadListener is not invoked.
<h:form enctype="multipart/form-data">
<p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}"
mode="advanced"
update="messages"
sizeLimit="100000"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>
<p:growl id="messages" showDetail="true"/>
here is the bean:
#ManagedBean
#RequestScoped
public class FileUploadController {
public void handleFileUpload(FileUploadEvent event) throws Exception {
System.out.println("OOOOOOOOOOOOOOOOOOK");
File targetFolder = new File("C:/Uploads");
InputStream inputStream = event.getFile().getInputstream();
OutputStream out = new FileOutputStream(new File(targetFolder,
event.getFile().getFileName()));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
inputStream.close();
out.flush();
out.close();
}
}
And here is the web.xml configuration:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<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>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
I have also added the commons-fileupload and Commons-io jars in the classpath.
I am not understanding why the the handleFileUpload is not invoked.
You've explicitly configured the file upload filter to listen on FORWARD dispatcher only.
You need either to remove the following entry from the filter mapping so that it listens by default on the REQUEST dispatcher only:
<dispatcher>FORWARD</dispatcher>
Or, to add the REQUEST dispatcher to the filter mapping so that it would run on normal requests as well:
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
The FORWARD dispatcher is only mandatory when RequestDispatcher#forward() is been called before the filter is invoked. For example, by some URL rewriting solution such as PrettyFaces. The information provided so far in the question does however not seem to indicate in any way that you're using that.
Unrelated to the concrete problem, as the PrimeFaces file upload requires Apache Commons IO, you may want to consider to IOUtils#copy() instead of that verbose input/output stream loop. See also: How to save uploaded file in JSF.

JSF facelet app not working in jboss AS 7

I am trying to set up environment for Java/JSF app in my windows 7 machine. I am using eclipse juno with Jboss AS 7 downloaded from the eclipse market. Also I am using JDK 1.7.
I created a very simple app containing just a h:outputLabel tag. Everything looks fine until deployed and run, but the tag doesn't render. What I mean to say is that I am getting a blank page.
The jars I included for JSF are : jsf-api-2.1, jsf-impl-2.1.0-b03.jar,jsf-facelets-1.1.14.jar.
And the usual commons and jstl jars.
There are no errors or any exceptions. Am I missing anything here? Please pardon me if its too simple but this is just taking too much time.
EDIT:
My auto-created faces-config xml:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
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_0.xsd"
version="2.0">
</faces-config>
My web xml:
<?xml version="1.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">
<display-name>test</display-name>
<welcome-file-list>
<welcome-file>NewFile.xhtml</welcome-file>
</welcome-file-list>
<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>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
</web-app>
Thanks in advance.
The jars I included for JSF are : jsf-api-2.1, jsf-impl-2.1.0-b03.jar,jsf-facelets-1.1.14.jar. And the usual commons and jstl jars.
There are quite a lot of mistakes right here. First of all, you don't need and even should not include the JSF jars. Those are already part of Java EE, which is implemented by JBoss AS 7.
Secondly, you definitely don't need and absolutely should not use the separate Facelets jar in combination with JSF 2.x. Facelets is already part of JSF 2.x.
You also should not include the JSTL jar. That one too is provided by Java EE/Jboss AS. If with commons jars you mean Apache commons, then those are fine but they are NOT needed for JSF. Include them only if you want to use them directly in your application code.
If you would need any of those jars (you don't, but suppose), it's also best practice to take the latest ones if you're just starting. From the version numbers it kinda looks like you just took a random old version. But again, you don't need any of those jars. They are provided by Java EE/JBoss AS 7.
My auto-created faces-config xml:
You don't need an empty faces-config.xml. If you're just starting, it might be better to remove everything you don't need. If there's later something you need to configure, you can always add it.
My web xml:
For JSF you don't really need to map the FacesServlet to the extensions you used. Those are already the default. If you leave out the entire web.xml, your Facelets (.xhtml) page can be requested by changing the .xhtml extension to .jsf or .faces. E.g. if your page is 'NewFile.xhtml' you can request it using localhost:8080/NewFile.jsf or 'localhost:8080/NewFile.faces` or 'localhost:8080/faces/NewFile.xhtml'.
Unfortunately, the only (IMHO) useful mapping is NOT provided as a default by JSF 2.1 and for that one you do need to add a mapping entry in web.xml:
<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>*.xhtml</url-pattern>
</servlet-mapping>
Because your welcome page is NewFile.xhtml, you need either this *.xhtml mapping, OR you can remove the mapping entirely and change the welcome-file content to e.g. NewFile.jsf.
Update:
If the welcome page still doesn't show, there must be something else in your project that you either don't know about yourself or aren't showing us.
Try to start over with a very simple project and see if it works there:
In Eclipse, create a new Dynamic Web Project
Use project name: welcome and Target runtime: JBoss 7.1 Runtime
Delete WebContent/META-INF and WebContent/WEB-INF/lib
Copy the 3 files from http://arjan-tijms.omnifaces.org/2011/08/minimal-3-tier-java-ee-app-without-any.html to your project. Afterwards your workspace should look exactly like the one in the picture.
Add a WebContent/WEB-INF/web.xml with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<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>*.xhtml</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>page.xhtml</welcome-file>
</welcome-file-list>
</web-app>
Double check you have exactly 4 files in your entire 'welcome' project, not more and not less.
Deploy your project to JBoss AS 7.1. To be sure, right click on the runtime server in the Servers view and click on "Clean..." Start your server and request localhost:8080/welcome or localhost:8080/welcome/.
I just tested this locally using the exact same steps I outlined, and it worked.

Resources