Spring MVC WebApp Issue - multithreading

We are using Spring MVC 3 for our WebApplication. I see a strange behavior from the application. When a user perform a action (ex: button click) , i see the controllers logs are logged twice and so are the subsequent calls in the controller .Controllers we used are default,singleton.
So,
I would like to print the thread information in the controller(like threadName. tried Thread.getName()..was Unique).. any help?
How do i make sure that there is only one Spring container
loaded? -- any Suggestions?
log4j Prop:
`log4j.appender.ROLL_FILE=org.apache.log4j.RollingFileAppender
log4j.appender.ROLL_FILE.File=/ws/was/dept/logs/${module.jvm.instance}_module.log
log4j.appender.ROLLING_FILE.Append=true
log4j.additivity.ROLLING_FILE.Append=false
log4j.appender.ROLLING_FILE.MaxFileSize=10MB
log4j.appender.ROLLING_FILE.MaxBackupIndex=20
log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ROLLING_FILE.layout.ConversionPattern=[module] %d - %c -%-4r [%t] %-5p %c %x - %m
Web.xml :
<display-name>module</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring/int/root-int-context.xml,/WEB-INF/spring/root- context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>`
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
..........
.........
.........
Thanks

Use Thread.getId() to get the unique thread ID.
Check your logger configuration - I'm guessing you are using log4j or slfj - you might have more than one logger that intercepts the message adds appenders on-top of the appenders defined in the parent logger this might cause two messages to be printed - so check if you have that and define additivity to be false.

I would like to print the thread information in the controller(like threadName. tried Thread.getName()..was Unique)
Use a logging framework. You can find several examples. Check here for configuring logback
How do i make sure that there is only one Spring container loaded?
I really don't understand what you mean here, but if have defined contextConfigLocation appropriately in your web.xml, the Spring container would be loaded correctly
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/META-INF/spring/*.xml
</param-value>

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?

My js/css caching is not working even after using project stage and filters, header date always getting update

I am trying to implement caching for my JS/CSS files but it is not working, I am using Mojarra JSF-2.2.12 and PrimeFaces 6.
I have tried using javax.faces.PROJECT_STAGE as Production and created a custom filter but nothing worked.
Need your help... below is web.xml code snippet
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.defaultResourceMaxAge</param-name>
<param-value>3628800000</param-value> <!-- 8 hours -->
</context-param>
In xhtml we have written like below.
<h:outputScript name="js/ProcessSpecific-min.js"></h:outputScript>
It looks like response server header is updating date and time on every hit, may be that is the reason it is not able to pick it from cache.
header response for 1 file
you use Mojarra or Myfaces ?
In Mojarra you can control the expiration time by the following context parameter (the value is in millis):
<context-param>
<param-name>com.sun.faces.defaultResourceMaxAge</param-name>
<param-value>3628800000</param-value> <!-- 6 weeks. -->
</context-param>
And in MyFaces:
<context-param>
<param-name>org.apache.myfaces.RESOURCE_MAX_TIME_EXPIRES</param-name>
<param-value>3628800000</param-value> <!-- 6 weeks. -->
</context-param>
Set HTTP headers properly to force caching on JS, CSS and PNG files

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

JSF works only with the .xhtml ending

i start with the programming of a JSF Website. At the moment all files have the .xhtml ending. When i go to http://localhost:8080/myProject/start.jsf everything is all right. But when i rename the file from start.xhtml to start.jsf i became a NoClassDefFound Error.
What is my mistake?
<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>*.jsf</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
You have to change the javax.faces.DEFAULT_SUFFIX parameter (in web.xml)
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.jsf</param-value>
</context-param>
However, this is not advisable - either use .xhtml or .jsp for your files.
Note that you can use .jsp with facelets with no problems (if, for example the auto-complete of your IDE doesn't work for .xhtml).
Also note that:
the faces servlet mapping determines how the jsf pages are referred to from http perspective
the DEFAULT_SUFFIX parameter indicates what's the extension of the files.
Why do you want to rename the file start.jsf? The correct extension of the JSF files are .xhtml (but you can modify this default extension, as stated by Bozho).
In fact, to be precise, this extension is defined by Facelets (or JSF 2.0, as it natively integrate Facelets), which is different if you use "basic" JSP files.
It's best to stay with .xhtml because that's the right way to do it, but you can configure it with the javax.faces.DEFAULT_SUFFIX context-param in web.xml.

Resources