I have a website to get survey from employee of a company using Struts2 and Hibernate, MySQL, using Tomcat 6 server. This is method to get question of an event, the question of event is using UTF-8 encoding. This is my code:
public String getEventQuestion() {
try {
event = surveyEventController.getEvent(this.getId());
surveyQuestions = surveyQuestionController.getQuestion(this.getId());
if (surveyQuestions != null) {
return Constants.SUCCESS;
} else {
return Constants.FAIL;
}
} catch (Exception e) {
logger.error("[Exception]getSurvey: " + e.getMessage(), e);
return Constants.FAIL;
}
}
getQuestion method from controller:
@SuppressWarnings("unchecked")
public List<SurveyQuestion> getQuestion(Long eventID)
{
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
List<SurveyQuestion> listQuestion = null;
try
{
session.getTransaction().begin();
listQuestion = (List<SurveyQuestion>)session.createQuery("from SurveyQuestion where event_id="+eventID.toString()).list();
session.getTransaction().commit();
}
catch(Exception e)
{
logger.error("[Exception] getQuestion SurveyQuestion: ",e);
session.getTransaction().rollback();
}
if(listQuestion!=null && !listQuestion.isEmpty())
{
return listQuestion;
}
else
{
return null;
}
}
All the jsp page encoding is UTF-8 also.
On the first time I access website, the content is successfully loaded with UTF-8 content as I expect.
The problem is after I press F5 to refresh the page, the content is automatically changed to non UTF-8 encoding. I debug and recognize that when the line session.getTransaction().commit() is run, the database automatically update with the listQuestion in non-UTF-8 encoding content, maybe ISO or something like that, all special character is changed to ?. How can I solve this issue?