import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
/**
* Simple demonstration for an Applet <-> Servlet communication.
*/
public class EchoApplet extends Applet {
private TextField inputField = new TextField();
private TextField outputField = new TextField();
private TextArea exceptionArea = new TextArea();
/**
* Setup the GUI.
*/
public void init() {
// set new layout
setLayout(new GridBagLayout());
// add title
Label title = new Label("Echo Applet", Label.CENTER);
title.setFont(new Font("SansSerif", Font.BOLD, 14));
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.weightx = 1.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(5, 5, 5, 5);
add(title, c);
// add input label, field and send button
c = new GridBagConstraints();
c.anchor = GridBagConstraints.EAST;
add(new Label("Input:", Label.RIGHT), c);
c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
add(inputField, c);
Button sendButton = new Button("Send");
c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
add(sendButton, c);
sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onSendData();
}
});
// add output label and non-editable field
c = new GridBagConstraints();
c.anchor = GridBagConstraints.EAST;
add(new Label("Output:", Label.RIGHT), c);
c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
add(outputField, c);
outputField.setEditable(false);
// add exception label and non-editable textarea
c = new GridBagConstraints();
c.anchor = GridBagConstraints.EAST;
add(new Label("Exception:", Label.RIGHT), c);
c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
add(exceptionArea, c);
exceptionArea.setEditable(false);
}
/**
* Get a connection to the servlet.
*/
private URLConnection getServletConnection()
throws MalformedURLException, IOException {
// Connection zum Servlet ?fnen
URL urlServlet = new URL(getCodeBase(), "echo");
URLConnection con = urlServlet.openConnection();
// konfigurieren
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty(
"Content-Type",
"application/x-java-serialized-object");
// und zur?kliefern
return con;
}
/**
* Send the inputField data to the servlet and show the result in the outputField.
*/
private void onSendData() {
try {
// get input data for sending
String input = inputField.getText();
// send data to the servlet
URLConnection con = getServletConnection();
OutputStream outstream = con.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject(input);
oos.flush();
oos.close();
// receive result from servlet
InputStream instr = con.getInputStream();
ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
String result = (String) inputFromServlet.readObject();
inputFromServlet.close();
instr.close();
// show result
outputField.setText(result);
} catch (Exception ex) {
ex.printStackTrace();
exceptionArea.setText(ex.toString());
}
}
}
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
/**
* Simple demonstration for an Applet <-> Servlet communication.
*/
public class EchoServlet extends HttpServlet {
/**
* Get a String-object from the applet and send it back.
*/
public void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
try {
response.setContentType("application/x-java-serialized-object");
// read a String-object from applet
// instead of a String-object, you can transmit any object, which
// is known to the servlet and to the applet
InputStream in = request.getInputStream();
ObjectInputStream inputFromApplet = new ObjectInputStream(in);
String echo = (String) inputFromApplet.readObject();
// echo it to the applet
OutputStream outstr = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstr);
oos.writeObject(echo);
oos.flush();
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<title> Plugin example </title>
<body bgcolor="white">
<jsp:plugin type="applet" code="appletsrc.AppletToServer2.class"
codebase="appletArchive"
jreversion="1.5"
width="100%"
height="100%"
archive="jfreechart-1.0.9.jar,jcommon-1.0.12.jar,jwork.jar"
>
<jsp:params>
<jsp:param name="paramval" value="Hello!! jsp:param" />
<jsp:param name="servlet" value="http://203.245.121.43:8888/gsgroup/jwork/groupTest2.do" />
<jsp:param name="title" value="나의 차트 테스트" />
<jsp:param name="domainAxisLabel" value="LABEL 1" />
<jsp:param name="rangeAxisLabel" value="LABEL 2" />
<jsp:param name="orientation" value="H" />
</jsp:params>
</jsp:plugin>
</body>
</html>
댓글 없음:
댓글 쓰기