Mailing List Archive

Support open source code!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

tlug: Java and Japanese



--------------------------------------------------------
tlug note from Craig Oda <craig@example.com>
--------------------------------------------------------
Hi,
thanks to Tsurui-san from Toshiba CAE Systems, I tested out 
getBytes("8859_1") and now have a working servlet that mails
me the contents of a Japanese HTML form and writes to a file
in JIS, ShiftJIS, and EUC.  The code is purely for testing.  I'm
posting it because I'm excited about the getBytes("EUC"),
getBytes("SJIS"), and getBytes("JIS"), think it's mondo cool.

I also have about 25 pages of Japanese mailing list archives dealing
with Java, Japanese and SQL databases that I may edit and
put on my home page one day.  Evidently, the database JDBC 
driver people had the same problem with Strings that I had
with the servlet.  Fascinating stuff.  

I'm about to watch my Japanese drama, so I'll post my test
here for the benefit of others.  I'll probably have a couple
of clean objects in a couple of days that can be used to
handle generic Japanese forms, so let me know if anyone
wants it.  Though, the weekend is coming up and I've been 
wanting to go outside.   

I'm not sure what the "8859_1" means.  Does anyone know?


Regards,
Craig



 ------------------------- begin test code  -------------

/* Test program that shows use of Strings and the new JDK 1.1 encoding
    conversion classes.  It also uses Sun's servlet development kit 1.0.
    I've tested input into the form with EUC encoding under Linux and
    ShiftJIS under Apple Macintosh Kanji Talk.  

This is the HTML test form.

 <h1>Servlet Test</h1>
 <FORM METHOD=POST 
 ACTION= "http://cow.farm.twics.com/village-bin/servlet.sh/test2">
 ĚžÁ°: <INPUT TYPE="TEXT" NAME="name" SIZE=20><BR>
 <INPUT TYPE="submit" NAME="request" VALUE="Send">
 </FORM>

    */

import java.io.*;
import java.util.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;


public
class test2 extends HttpServlet {

    public void doPost(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException
    {
	res.setContentType("text/html");

	// get name_ from HTTP apache server.  This is taken from
	// the HTML form
	String name_ = req.getParameter("name");

	// the String should be a unicode encoded string
	String uniString;
	// sends the output stream back to the HTTP server to send
	// to the browser
	ServletOutputStream out = res.getOutputStream();

	try {
	  uniString = new String(name_.getBytes("8859_1"),"EUCJIS");
	  byte[] eucByteArray = uniString.getBytes("EUCJIS");
	  byte[] sjisByteArray = uniString.getBytes("SJIS");
	  byte[] jisByteArray = uniString.getBytes("JIS");


	  File eucOut = new File("/tmp/name_of_person.euc");
	  File sjisOut = new File("/tmp/name_of_person.sjis");
	  File jisOut = new File("/tmp/name_of_person.jis");

 	  FileOutputStream writesJis = new FileOutputStream(sjisOut);
 	  FileOutputStream writeEuc = new FileOutputStream(eucOut);
	  FileOutputStream writeJIS = new FileOutputStream(jisOut);


 	  for (int j = 0; j < jisByteArray.length; j ++) {
                   writeJIS.write(jisByteArray[j]); 
 	  }
 	  writeJIS.flush();
 	  writeJIS.close();
 	  out.println("Wrote File");

        try { 
          // open a network socket on the SMTP port
          Socket server = new Socket("mail.twics.com", 25);     

          // set up network print stream
          PrintWriter sendmailout =
            new PrintWriter(
                            new BufferedWriter(
                                             new OutputStreamWriter(
                                                                    server.getOutputStream())), true);
          // send mail greetings and message
          sendmailout.println("HELO my_box.twics.com\n" 
                               + "MAIL FROM:<me@example.com>\n"
                               + "RCPT TO:<you@example.com>\n"
                               + "DATA\n");

          // send the body of the e-mail here

         sendmailout.println("Hi, this is an automated message"
                              + " with the results of the "
                              + "questionaire form." );
          
         Date date_written = new Date();
         sendmailout.println("This note was sent on " + date_written);

 	  for (int j = 0; j < jisByteArray.length; j ++) {
                   sendmailout.write(jisByteArray[j]); 
 	  }
	  sendmailout.println("\n");

         // the mail program requires a single dot on a line
          // by itself
          sendmailout.println(".");
          sendmailout.println("QUIT");
          sendmailout.close();
        }  
        catch ( UnknownHostException e ) { 
          System.out.println("Can't find host."); 
        }  
        catch ( IOException e ) { 
          System.out.println("Error connecting to host."); 
        } 

	  
 	  for (int j = 0; j < sjisByteArray.length; j ++) {
                   writesJis.write(sjisByteArray[j]); 
 	  }
 	  writesJis.flush();
 	  writesJis.close();
 	  out.println("Wrote File");

	  for (int j = 0; j < eucByteArray.length; j ++) {
                  writeEuc.write(eucByteArray[j]); 
		  out.write(eucByteArray[j]);
	  }
	  writeEuc.flush();
	  writeEuc.close();


	}
	catch (UnsupportedEncodingException encodErr) {
	  System.out.println(encodErr);
	  }

    }
}


--------------------------------------------------------
"Knowledge is the air and light of civilization.  Transform it and you
transform all else."
Craig Oda 	craig@example.com	
   TWICS - IEC Building, 1-21 Yotsuya, Shinjuku-ku, Tokyo 160 JAPAN
Main Reception: 03-3351-5977	Fax: +81-3-3353-6096

Next TLUG meeting is Saturday October 11, 1997
-----------------------------------------------------------------
a word from the sponsor will appear below
TWICS - Japan's First Public-Access Internet System.
www.twics.com  info@example.com  Tel:03-3351-5977  Fax:03-3353-6096

Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links