Re: So boring issues about character encoding in action ......

2005-10-18 Thread Joe Germuska

At 1:44 PM +0800 10/18/05, Tony Lu wrote:

I need to create an internationalized Web application by struts,mysql and
hibernate.
Each component Character Encoding is utf8.
 It runs well when I run a pure servlet to save 'Chinese Character' to
database and load it from database.
But when I implement it with struts action, the application can not save
Chinese correctly.
 I really don't know why struts action can not work well. I am sure there is
no difference between them.


By the time that your action class is executing, Struts has already 
read from the request input stream (to populate the ActionForm).


In any web application, if you want to set the request character 
encoding, you must arrange to do it before any code will read from 
the request input stream.  The best way to do this in a Servlet 2.3 
or later web environment is to use a ServletFilter.  In fact, most of 
the worked examples of writing ServletFilters on the web are to solve 
this problem.


You could also do it by extending RequestProcessor (or 
TilesRequestProcessor, if you use Tiles) and overriding the 
processPreprocess method.  However, in Struts 1.3 the 
processPreprocess method is no longer used.  Using a ServletFilter 
will apply to any application you write in any Struts version, or 
with a non-Struts approach.


Joe




 Is there anywhere to set character encoding for action? Please help!
  --Pure Servlet ( It runs well)
-
public class UtfTest extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {

request.setCharacterEncoding("UTF-8");
String description = request.getParameter("description");
description = (description == null?"":description.trim());

try{
ItemDAO itemDao = ItemDAO.getInstance();
Item item = new Item();
item.setDescription(description);
itemDao.save(item);
}catch(Exception e){
e.printStackTrace();
}
response.sendRedirect("test.jsp");
}
}

--Action for struts 

public class TestAction extends Action{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
HttpServletResponse res) throws Exception {

req.setCharacterEncoding("UTF-8");
String description = req.getParameter("description");
description = (description == null?"":description.trim());
try{
ItemDAO itemDao = ItemDAO.getInstance();
Item item = new Item();

item.setDescription(description);
itemDao.save(item);
// Determine which action forward should be returned
return mapping.findForward("success");
}catch(Exception e){
}
}
}



--
Joe Germuska
[EMAIL PROTECTED]  
http://blog.germuska.com
"Narrow minds are weapons made for mass destruction"  -The Ex


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: So boring issues about character encoding in action ......

2005-10-17 Thread Tony Lu
I don't think so.
I have set '' in
struts-config.xml
So the action don't need to specially add statement '
res.setCharacterEncoding("UTF-8")'.
I also have tested it like this. But it seems it's no use.

 On 10/18/05, Deepesh Nandal <[EMAIL PROTECTED]> wrote:
>
> Hi,
> In the struts action you are setting character encoding on 'request'
> object req ,it however needs to be on 'response' ie res.
>
> Deepesh.
>
>
> public class TestAction extends Action{
> public ActionForward execute(ActionMapping mapping,
> ActionForm form,
> HttpServletRequest req,
> HttpServletResponse res) throws Exception {
>
> req.setCharacterEncoding("UTF-8"); // this should be
> res.setCharacterEncoding("UTF-8");
>
> String description = req.getParameter("description");
> description = (description == null?"":description.trim());
> try{
> ItemDAO itemDao = ItemDAO.getInstance();
> Item item = new Item();
>
>
>
> - Original Message -
> From: "Tony Lu" <[EMAIL PROTECTED]>
> To: 
> Sent: Tuesday, October 18, 2005 11:14 AM
> Subject: So boring issues about character encoding in action ..
>
>
> I need to create an internationalized Web application by struts,mysql and
> hibernate.
> Each component Character Encoding is utf8.
> It runs well when I run a pure servlet to save 'Chinese Character' to
> database and load it from database.
> But when I implement it with struts action, the application can not save
> Chinese correctly.
> I really don't know why struts action can not work well. I am sure there
> is
> no difference between them.
> Is there anywhere to set character encoding for action? Please help!
> --Pure Servlet ( It runs well)
> -
> public class UtfTest extends HttpServlet {
> protected void doPost(HttpServletRequest request, HttpServletResponse
> response)
> throws ServletException, IOException {
>
> request.setCharacterEncoding("UTF-8");
> String description = request.getParameter("description");
> description = (description == null?"":description.trim());
>
> try{
> ItemDAO itemDao = ItemDAO.getInstance();
> Item item = new Item();
> item.setDescription(description);
> itemDao.save(item);
> }catch(Exception e){
> e.printStackTrace();
> }
> response.sendRedirect("test.jsp");
> }
> }
>
> --Action for struts
> 
>
> public class TestAction extends Action{
> public ActionForward execute(ActionMapping mapping,
> ActionForm form,
> HttpServletRequest req,
> HttpServletResponse res) throws Exception {
>
> req.setCharacterEncoding("UTF-8");
> String description = req.getParameter("description");
> description = (description == null?"":description.trim());
> try{
> ItemDAO itemDao = ItemDAO.getInstance();
> Item item = new Item();
>
> item.setDescription(description);
> itemDao.save(item);
> // Determine which action forward should be returned
> return mapping.findForward("success");
> }catch(Exception e){
> }
> }
> }
>
>
>
>
> 
>
>
> No virus found in this incoming message.
> Checked by AVG Anti-Virus.
> Version: 7.0.344 / Virus Database: 267.12.2/139 - Release Date: 10/17/2005
>
>
> -
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


Re: So boring issues about character encoding in action ......

2005-10-17 Thread Deepesh Nandal

Hi,
   In the struts action you are setting character encoding on 'request' 
object req ,it however needs to be on 'response'  ie res.


Deepesh.


public class TestAction extends Action{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
HttpServletResponse res) throws Exception {

req.setCharacterEncoding("UTF-8");   // this should be 
res.setCharacterEncoding("UTF-8");


String description = req.getParameter("description");
description = (description == null?"":description.trim());
try{
ItemDAO itemDao = ItemDAO.getInstance();
Item item = new Item();



- Original Message - 
From: "Tony Lu" <[EMAIL PROTECTED]>

To: 
Sent: Tuesday, October 18, 2005 11:14 AM
Subject: So boring issues about character encoding in action ..


I need to create an internationalized Web application by struts,mysql and
hibernate.
Each component Character Encoding is utf8.
It runs well when I run a pure servlet to save 'Chinese Character' to
database and load it from database.
But when I implement it with struts action, the application can not save
Chinese correctly.
I really don't know why struts action can not work well. I am sure there is
no difference between them.
Is there anywhere to set character encoding for action? Please help!
 --Pure Servlet ( It runs well)
-
public class UtfTest extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {

request.setCharacterEncoding("UTF-8");
String description = request.getParameter("description");
description = (description == null?"":description.trim());

try{
ItemDAO itemDao = ItemDAO.getInstance();
Item item = new Item();
item.setDescription(description);
itemDao.save(item);
}catch(Exception e){
e.printStackTrace();
}
response.sendRedirect("test.jsp");
}
}

--Action for struts 

public class TestAction extends Action{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
HttpServletResponse res) throws Exception {

req.setCharacterEncoding("UTF-8");
String description = req.getParameter("description");
description = (description == null?"":description.trim());
try{
ItemDAO itemDao = ItemDAO.getInstance();
Item item = new Item();

item.setDescription(description);
itemDao.save(item);
// Determine which action forward should be returned
return mapping.findForward("success");
}catch(Exception e){
}
}
}






No virus found in this incoming message.
Checked by AVG Anti-Virus.
Version: 7.0.344 / Virus Database: 267.12.2/139 - Release Date: 10/17/2005


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



So boring issues about character encoding in action ......

2005-10-17 Thread Tony Lu
I need to create an internationalized Web application by struts,mysql and
hibernate.
Each component Character Encoding is utf8.
 It runs well when I run a pure servlet to save 'Chinese Character' to
database and load it from database.
But when I implement it with struts action, the application can not save
Chinese correctly.
 I really don't know why struts action can not work well. I am sure there is
no difference between them.
 Is there anywhere to set character encoding for action? Please help!
  --Pure Servlet ( It runs well)
-
public class UtfTest extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {

request.setCharacterEncoding("UTF-8");
String description = request.getParameter("description");
description = (description == null?"":description.trim());

try{
ItemDAO itemDao = ItemDAO.getInstance();
Item item = new Item();
item.setDescription(description);
itemDao.save(item);
}catch(Exception e){
e.printStackTrace();
}
response.sendRedirect("test.jsp");
}
}

--Action for struts 

public class TestAction extends Action{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
HttpServletResponse res) throws Exception {

req.setCharacterEncoding("UTF-8");
String description = req.getParameter("description");
description = (description == null?"":description.trim());
try{
ItemDAO itemDao = ItemDAO.getInstance();
Item item = new Item();

item.setDescription(description);
itemDao.save(item);
// Determine which action forward should be returned
return mapping.findForward("success");
}catch(Exception e){
}
}
}