Proper activeweb configuration for pages with foreign language characters - activeweb

I have just created new web application based on activeweb framework and by default it shows me "?" for all foreign language characters. Something like this:
? ????????? ??????
???? ????????? ??
?????? ??????????
? ??? ???? ??????????
??????????? ????????
English characters are displayed ok.
Here is my web.xml config:
<display-name>activeweb</display-name>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<filter>
<filter-name>dispatcher</filter-name>
<filter-class>org.javalite.activeweb.RequestDispatcher</filter-class>
<init-param>
<param-name>exclusions</param-name>
<param-value>css,img,fonts,images,js,html,GwtExample,ico</param-value>
</init-param>
<init-param>
<param-name>root_controller</param-name>
<param-value>home</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>dispatcher</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
I expect that this problem might be related to encoding in activeweb internals or I have to apply special setting. Do you know how to fix it?

This happens because ActiveWeb does nothing about encoding by default, and what you get comes from the servlet container.In order to force a specific encoding (UTF-8, I assume), you need to add a line of configuration to the web.xml file:
<filter>
<filter-name>dispatcher</filter-name>
<filter-class>org.javalite.activeweb.RequestDispatcher</filter-class>
<init-param>
<param-name>exclusions</param-name>
<param-value>css,images,js,ico</param-value>
</init-param>
<init-param>
<param-name>root_controller</param-name>
<param-value>home</param-value>
</init-param>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
This will force UTF-8 encoding on request as well as response.

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?

Filter for a single file in JSF application

I have one file (FileUpload.xhtml) which needs to protected from unauthorized access. So whenever someone tries to access FileUpload.xhtml, he has to go through the filter. This file is located as /web/sign/FileUpload.xhtml. There are some other files also that are present in the same directory as FileUpload.xhtml which should not go though this filter. I tries url patterns like
/web/sign/FileUpload.xhtml
/web/sign/FileUpload.xhtml*
/web/sign/FileUpload*
But none on them seems to work. But the url pattern :
/web/sign/*
works. But it also includes other files that are present with FileUpload.xhtml that I do not want to go through this filter. How to go about this problem?
Just to be more clear. I will be checking the request parameter to authorize the page. If someone accessing the page like
http://localhost:8888/CodesignWebApp/web/sign/FileUpload.xhtml?sign=Driver
I will check value of 'sign' parameter against the user, to see if he has the privilege to use Driver as sign value. Is this the right way to go about it?
Filter entries from web.xml :
<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>
</filter-mapping>
<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>

Conflict between use of hx:fileupload and tomahawk tree2 component in the application

I'm using hx:fileupload component which allows the user to open local file system and browse to the file. In the same application I have used tomahawk tree2 component to display hierarchical directory structure.
Earlier to adding tree2 component, fileupload was working perfect but when I added the filter tag corresponding to tomahawk tree2 in web.xml file, getFileupload1().getFilename() method of hx:fileupload component (where getFileupload1() is getter method of hx:fileupload component) return null exception.
Filter tag code:
<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>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<url-pattern>*.jsf</url-pattern>
</filter-mapping>
If i remove filter tag from web.xml file, fileupload component resumes to work properly but Tree2 component won't work.
Please help to make both components work together in the application.
Thank you
I have done the following updations:
Implemented ExtensionsFilter filter servlet by removing if (ServletFileUpload.isMultipartContent(httpRequest)) {} block from the original ExtensionsFilter source code file.
updated web.xml code
<filter>
<filter-name>MyFacesExtensionsFilter</filter-name>
<filter-class>pagecode.ExtensionsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<url-pattern>/faces/myFacesExtensionResource/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<url-pattern>*.jsf</url-pattern>
</filter-mapping>
Please help. Thanks.
The ExtensionsFilter also automatically parses multipart/form-data requests as part of its job for Tomahawk's <t:inputFileUpload>. This is not disableable by configuration.
You have basically 2 options:
Copy the source code of ExtensionsFilter under the same license and remove the whole if (ServletFileUpload.isMultipartContent(httpRequest)) {} block (lines 347-350) so that it doesn't parse multipart/form-data requests anymore and use this filter instead.
Replace <hx:fileUpload> by <t:inputFileUpload>.

Setting Vaadin session-timeout parameter

I am using Vaadin 7.1.7 and I can't figure out how to set session-timeout
parameter (to, say, 1min).
As far as I can tell, Vaadin 7.x.x does not produce web.xml, it uses #VaadinServletConfiguration annotation but there doesn't seem to be a session-timeout parameter.
As far as I know there are 2 ways to set session-timeout in Vaadin 7.
In the web.xml:
<session-config>
<session-timeout>1</session-timeout> <!-- 1 minute -->
</session-config>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.xyz.web.MyServlet</servlet-class>
<init-param>
<description>My Main Page</description>
<param-name>UI</param-name>
<param-value>com.xyz.web.MyUI</param-value>
</init-param>
<init-param>
<description>Enable Session Timeout (heartbeat can't keep alive)</description>
<param-name>closeIdleSessions</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Or we can set it programmatically (current session only):
VaadinSession.getCurrent().getSession().setMaxInactiveInterval(60); // 1 minute
It seems the servlet 3.0 annotations do not help: link
More help here: link

IP Filtering in Tomcat 6

I know to filter using ip in tomcat 6,I have to use he bleow code
Valve className=”org.apache.catalina.valves.RemoteAddrValve” allow=”10.10.1[12]..*”/>
this works for me.But,How can I restrict access to a specific servlet.The above blocks for the entire application,I want to balock access for specifi servlet.I tried the below in web.xml,but its display tomcat error page 404
<filter>
<filter-name>R</filter-name>
<filter-class>org.apache.catalina.valves.RemoteAddrValve</filter-class>
<init-param>
<param-name>allow</param-name>
<param-value>172\.16\.18\..*</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>R</filter-name>
<url-pattern>*/json</url-pattern>
</filter-mapping>
you can not use star(*) int the start of url pattern.

Resources