my dog learned polymorphism*
The moose likes Servlets and the fly likes Japanese character from the request stored in question mark in the DB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Japanese character from the request stored in question mark in the DB" Watch "Japanese character from the request stored in question mark in the DB" New topic
Author

Japanese character from the request stored in question mark in the DB

saravanan kanda swamy
Ranch Hand

Joined: Apr 21, 2004
Posts: 33
Hi,

I have a problem . From a web application I am sending the japanese characters in the text box when I try to print that it is coming in corrupted format, but I have set
contentType="text/html; charset=SHIFT_JIS" pageEncoding="Shift_JIS"

in the page attribute of the JSP

and in the Servlet also I have set the reqeust in SHIFT_JIS encoding. the problem now is when I try to print the the japanese data it is coming in different values.

To debug I printed the reponse.getCharacterEncoding() it is coming as ISO-8859-1 cant we change that to SHIFT_JIS?

How to change the reponse encoding to SHIFT_JIS?

When I try to store that Data in DB it is coming in question mark. What need to be done to avoid that problem.

But When I try same thing in japanese machine i.e. deploying the web application in that. That characters coming fine. I need to depoly that in Windows machine EN_US.

I am using weblogic 6.1 Application Server

Thanks in advance.

Saravanan.K
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

About the encoding of the response :
response.setCharacterEncoding("Shift_JIS");
You can also use :
response.setContentType("text/html;charset = Shift_JIS");

About the DB, what is its default encoding ?
I think that you need to set it to an encoding supporting Japanese characters (SJIS, EUC, or even Unicode). If you write the data in SJIS, then you'll need to set the DB to SJIS too.


[My Blog]
All roads lead to JavaRanch
saravanan kanda swamy
Ranch Hand

Joined: Apr 21, 2004
Posts: 33
Hi,

I did
response.setContentType("text/html;charset = Shift_JIS"); because there is no such method like response.setCharacterEncoding.

Now I able to see the japanese characters in the output but when it stored in the DB it stored in the question mark.

To explain in detail I have two different applications. One is the normal web application there in which I sending the request to another enterprise application (that is struts and EJB in that I am able to store the japanese chracters perfectly fine using that application). But when the request that is sent from web application (that has japanese chracters) to the struts application.It is stored as question mark. I am using UrlConnection to send the reqeust from the normal web application to Entrprise application.

I am using POST Method. But still the problem persists.

Here is the piece of code that Used to send the request

static public String getURLPostString(URL url, String body,HttpServletRequest hreqObject) throws Exception{
StringBuffer sb = new StringBuffer();

// find the newline character(s) on the current system
String newline = null;
try {
newline = System.getProperty("line.separator");
} catch (Exception e) {
newline = "\n";
}

//try {
// URL must use the http protocol!
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setAllowUserInteraction(false); // you may not ask the user
conn.setDoOutput(true); // we want to send things
// the Content-type should be default, but we set it anyway
//conn.setRequestProperty( "Accept-Charset", "SHIFT_JIS" );

System.out.println("CONTENT TYPE "+hreqObject.getContentType());

System.out.println("CONTENT Encoding"+hreqObject.getCharacterEncoding());


conn.setRequestProperty( "Content-type", "application/x-www-form-urlencoded;charset=Shift_JIS" );

conn.setRequestProperty("Content-Encoding","SHIFT_JIS");

// conn.setRequestProperty( "Content-type", "text/html" );

// the content-length should not be necessary, but we're cautious
conn.setRequestProperty( "Content-length", Integer.toString(body.length()));

// get the output stream to POST our form data
OutputStream rawOutStream = conn.getOutputStream();

PrintWriter pw = new PrintWriter(rawOutStream);

pw.print(body); // here we "send" our body!
pw.flush();
pw.close();



System.out.println("Character encoding before getting the response :::"+conn.getRequestProperty("Content-Encoding"));
// get the input stream for reading the reply
// IMPORTANT! Your body will not get transmitted if you get the
// InputStream before completely writing out your output first!
InputStream rawInStream = conn.getInputStream();

//ByteArrayInputStream rawInStream = (ByteArrayInputStream)rawInStream1;



// get response
BufferedReader rdr = new BufferedReader(new InputStreamReader(rawInStream,"SHIFT_JIS"));
String line;

while ((line = rdr.readLine()) != null) {
sb.append(line);
sb.append(newline);
}


/*int c;


char chvalue[] =new char[10000];
int itemp =0;
while ((c = rawInStream.read()) != -1) {

chvalue[itemp]=(char)c;
//strTmp = strTmp + (char)c;
itemp++;

}


String strTmp = new String(chvalue);

return strTmp;*/
return sb.toString();
/* } catch (Exception e) {
System.out.println("Exception "+e.toString());
e.printStackTrace();
}*/
// return ""; // an exception occurred
}



What else need to be changed to store the japanese chracters in the DB

Because from other applcation we are able to store the japanese characters so there is no need to change any settings in the DB.

But if we install both the application in Japanese machine it is working fine.
But when I try that in the normal Windows (EN_US) machine it is giving this problem
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

One thing I still don't understand (sorry) is :
Is the data you received correctly encoded.
I mean, output the data into a file instead of a DB, and open the file in SJIS mode. Does it display properly ?
saravanan kanda swamy
Ranch Hand

Joined: Apr 21, 2004
Posts: 33
Hi,

No, I am not sure if the data is coming correct or not but whatever data is sent from the normal web application (japanese chracters is fine because we are able to see the output) but once the request is reached the other application I am not able to find the data is coming fine or not. Once the data is stored in the DB it is comes as question mark i.e I opened isqlplus to find the data it is coming as question mark.

Thanks and Regards,
Saravanan.K
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I see. It would help to know if the data is already altered before writing to the DB. To undersand if the problem is "receiving" or "writing"
saravanan kanda swamy
Ranch Hand

Joined: Apr 21, 2004
Posts: 33
Hi,

How to check if the data is corrupted before the updation? And one more I am suspecting the function that I am using to send the request. I am using OutputStream from HttpUrlconnection and I am writing the data in that and then I am sending to the other web application. Will that cause any problem?

Thanks and Regards,
Saravanan.K
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Maybe.

Try with
saravanan kanda swamy
Ranch Hand

Joined: Apr 21, 2004
Posts: 33
Hi,

I tried with

OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "Shift_JIS");

but it gave a problem in the content length so I commented that code

conn.setRequestProperty( "Content-length", Integer.toString(body.length()));

but still it is giving the same problem. I have even tried with the

Writer out
= new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "Shift_JIS"));

Is there any problem with using method print instead of write? In the Printwriter.

I even tried the same thing with by printing the body in the reponse.getPrintWriter() Method. It is also coming perfectly fine.

Can I use getBytes() method to find if the data is corrupted or not?

Thanks and Regards,
Saravanan.K
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

PrintWriter is for text output stream, so you should probably avoid using it, and send byte[] data instead.
saravanan kanda swamy
Ranch Hand

Joined: Apr 21, 2004
Posts: 33
Hi,

I tried with the following in the Normal web application I set to SHIFT_JIS



OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "Shift_JIS");

In the enterprise application I did

StringstrUserName= new String(strUserName1.getBytes"SHIFT_JIS"), "SHIFT_JIS");

where strUserName1 is the one that has the japanese characters. Now I able to store the Japanese data in the DB Perfectly.

Thanks a lot for your valuable suggestion.

Saravanan.K
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Japanese character from the request stored in question mark in the DB
 
Similar Threads
Help needed in internationalization of J2EE application
problem with foreign characters
multilingual JSP/Servlets
XML -> SAX -> MYSQL conversion losing character encoding...
Unable to read the Japanese character from HTTP Response