Site Search :
Standard Enterprise XML Methodology Pattern Setting Tunning Other
Article Contributors
GuestBook
Javapattern Maven
XSourceGen Dev
JetSpeed Test
JLook Image
jLook Family Site


Servlet°ú Applet°£ÀÇ HTTP Åë½Å
 
Servlet°ú Applet°£ÀÇ HTTP Åë½ÅÀ» ÀÌ¿ëÇÏ¿© Database ³»¿ëÀ» Applet¿¡ DisplayÇÏ´Â ¹æ¹ý ( 2003/04/02 ) 816
Written by ienvyou - ÃÖÁö¿õ
3 of 4
 

 

2. source ºÐ¼®

ItemEntity.java

import java.io.*;
public class ItemEntity implements Serializable
{

public String item_name;
public double item_price;
public ItemEntity(String item_name,double item_price){
this.item_name=item_name;
this.item_price=item_price;
}
public void setItem_name(String item_name){
this.item_name=item_name;
}
public void setItem_price(double item_address){
this.item_price=item_price;
}

public String getItem_name(){
return item_name;
}
public double getItem_price(){
return item_price;
}


}

UsersEntity.java

import java.io.*;
public class UsersEntity implements Serializable
{


public String user_id;
public String user_name;
public String user_address;
public UsersEntity(String user_id,String user_name,String user_address){
this.user_id=user_id;
this.user_name=user_name;
this.user_address=user_address;
}
public void setUser_id(String user_id){
this.user_id=user_id;
}
public void setUser_name(String user_name){
this.user_name=user_name;
}
public void setUser_address(String user_address){
this.user_address=user_address;
}
public String getUser_id(){
return user_id;
}
public String getUser_name(){
return user_name;
}
public String getUser_address(){
return user_address;
}


}

À§ÀÇ µÎ°¡Áö class´Â ServerÂÊ¿¡¼­ÀÇ °á°ú°ªÀ» client¿¡°Ô Àü´ÞÇÒ¶§ DBÀÇ dataµéÀ» java obejct typeÀÇ ¹è¿­À» ÀÌ¿ëÇÏ¿© Àü´Þ½ÃÅ°´Â wrapper classÀÌ´Ù.

À̶§ °¡Àå Áß¿äÇÑ°ÍÀº µÎ°³ÀÇ object¿¡´Â °´Ã¼ Á÷·ÄÈ­°¡ µÇ¾î ÀÖ¾î¾ß ÇÑ´Ù. Áï object streamÀ» ÅëÇØ °á°ú°ªÀÌ Àü´ÞµÇ±â ¶§¹®¿¡ Ç×»ó java.io.* package°¡ import µÇ¾î¾ß Çϸç implements SerializableÀÌ µÇ¾î¾ß ÇÑ´Ù.

MyApplet.java

public void actionPerformed(ActionEvent evt) {


if (evt.getSource()==users_b){
callServlet("Users");
}else if(evt.getSource()==item_b) {
callServlet("Item");
}


}
private void callServlet(String command) {


try {
URL phoneServlet = new URL(getDocumentBase(), "servlet/MyServlet");
ServletMessage msg = new ServletMessage(phoneServlet);
Properties args = new Properties();

if (command != null && command.length() > 0) {
args.put("name", command);
}
InputStream result = msg.sendMessage(args, ServletMessage.POST);
ObjectInputStream in = new ObjectInputStream(result);
list.removeAll();
if(command.equals("Users")){
UsersEntity users[]=(UsersEntity [])in.readObject();
display(users);
}else if(command.equals("Item")){
ItemEntity item[]=(ItemEntity [])in.readObject();
display(item);
}
} catch (IOException exc) {
showStatus("Cannot read Data: 1" + exc.toString());
}catch(Exception ee){
showStatus("Cannot read Data: 2" + ee.toString());
}


}


public void display(Object o[]){


if(o instanceof UsersEntity[]){
UsersEntity users[]=(UsersEntity[])o;
for (int i=0;i<users.length ;i++ )
{
list.add(users[i].user_id+" "+users[i].getUser_name()+" "+users[i].getUser_address());
}
}else if (o instanceof ItemEntity[]){
ItemEntity item[]=(ItemEntity[])o;
for (int i=0;i<item.length ;i++ )
{
list.add(item[i].getItem_name()+" "+item[i].getItem_price());
}
}


}

Applet¿¡¼­´Â button event¸¦ ¹Þ¾Æ¼­ »ç¿ëÀÚ°¡ UsersÀÇ Á¤º¸¸¦ ¿øÇÏ´ÂÁö ItemÀÇ Á¤º¸¸¦ ¿øÇÏ´ÂÁö¿¡ ´ëÇØ ¾Ë°Ô µÈ´Ù. ÀÌÁ¤º¸´Â ServletMessage.class ¸¦ ÀÌ¿ëÇÏ¿© POST ȤÀº GET¹æ½ÄÀ» ÅëÇØ MyServlet¿¡°Ô Àü´ÞµÈ´Ù. Áï ¿ì¸®°¡ browser¸¦ ÅëÇØ ServletÀ» È£ÃâÇÏ´Â°Í Ã³·³ code»ó¿¡¼­ ServletÀ» È£ÃâÇÏ´Â °ÍÀÌ´Ù.

´ÙÀ½ÀÇ code¿Í °°ÀÌ Servlet¿¡°Ô Àü´ÞµÉ º¯¼ö¿Í °ªÀ» ¼ÂÆÃÇÏ°í ÀÌ°ªÀ» POST ¹æ½ÄÀ̳ª GET¹æ½ÄÀ¸·Î Àü´ÞÇÑ´Ù. ¸¶Ä¡ html¿¡¼­ form tag¸¦ ÀÌ¿ëÇÏ¿© Àü´ÞµÇ´Â ¹æ½ÄÀ̶ó°í »ý°¢ÇÏ¸é µÈ´Ù.

Properties args = new Properties();

if (command != null && command.length() > 0) {
args.put("name", command);
}

ServletÀÇ È£ÃâÀÌ ³¡À̳ª¸é °á°ú°ª¿¡ ´ëÇÑÁ¤º¸°¡ output Stream À» ÀÌ¿äÇÏ¿© ¹Þ°ÔµÈ´Ù.À̶§ Server¿¡¼­´Â °á°ú°ª¿¡ ´ëÇÑ°ÍÀ» ObjectOutputStreamÀ» ÀÌ¿ëÇÏ¿© ItemEntity °´Ã¼³ª UsersEntity °´Ã¼ÀÇ ¹è¿­ ÇüÅ·Πclient ¿¡°Ô Àü´ÞÇÏ°Ô µÈ´Ù.

±×°á°ú °ªÀº displayÀÌ ¶ó´Â method¸¦ ÀÌ¿ëÇÏ¿© È­¸é¿¡ °á°ú¸¦ displayÀÌ ½ÃŲ´Ù.

ServletMessage.java

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

public class ServletMessage {


public static int GET = 0;
public static int POST = 1;
private URL servlet;

public ServletMessage(URL servlet) {

this.servlet = servlet;
System.out.println(servlet.toString());

}
public InputStream sendMessage(Properties args) throws IOException {

return sendMessage(args, POST);

}
public InputStream sendMessage(Properties args, int method)
throws IOException {

if (method == GET) {
URL url = new URL(servlet.toExternalForm() + "?" +
toEncodedString(args));
return url.openStream();

} else {
URLConnection conn = servlet.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);

// POST the request data (html form encoded)
PrintStream out = new PrintStream(conn.getOutputStream());
if (args != null && args.size() > 0) {
System.out.println("POST "+toEncodedString(args));
out.print(toEncodedString(args));
}
out.close(); // ESSENTIAL for this to work!

// Read the POST response data
return conn.getInputStream();
}

}
public String toEncodedString(Properties args) {

StringBuffer sb = new StringBuffer();
if (args != null) {
String sep = "";
Enumeration names = args.propertyNames();
while (names.hasMoreElements()) {
String name = (String)names.nextElement();
System.out.println(name);
sb.append(sep + URLEncoder.encode(name) + "=" +
URLEncoder.encode(args.getProperty(name)));
System.out.println(sep + URLEncoder.encode(name) + "=" +
URLEncoder.encode(args.getProperty(name)));
sep = "&";
}
}
System.out.println(sb.toString());
return sb.toString();

}


}

ÀÌ Class¿¡¼­´Â applet¿¡¼­ÀÇ event°¡ ¹ß»ýµÇ¾îÁö¸é POST ¹æ½ÄÀÏ°æ¿ì¿Í GET¹æ½ÄÀÏ°æ¿ì¿¡ µû¶ó Porperties ¿¡ server·Î Àü´ÞµÇ´Â Á¤º¸¿Í ÇÔ²² µ¿ÀÛ µÇ¾îÁø´Ù.

GET¹æ½ÄÀÏ°æ¿ì¿¡´Â URL À» ÀÌ¿ëÇÏ¿© ServletÀ» È£ÃâÇÏ°Ô µÇ°í POST¹æ½ÄÀÏ °æ¿ì¿¡´Â PrintStream out = new PrintStream(conn.getOutputStream()) À» ÀÌ¿ëÇÏ¿© ServletÀ» È£ÃâÇÏ°Ô µÈ´Ù. ServletÂÊ¿¡¼­´Â µÎ°¡Áö ¹æ½Ä¿¡ µû¶ó doGet()ÀÌ È£Ã⠵ǾîÁö±âµµ ÇÏ°í doPost()°¡ È£Ã⠵ǾîÁö±âµµ ÇÑ´Ù.

 

 
1 2 3 4
References
 
Copyright ¨Ï 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD