前台代码: import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; class Frame2 extends JFrame { JPanel contentPane; JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); /**Construct the frame*/ public Frame2() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit();
} catch(Exception e) { e.printStackTrace(); } } /**Component initialization*/ private void jbInit() throws Exception { jButton1.setText("happy"); jButton1.setBounds(new Rectangle(135, 86, 79, 29)); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); contentPane = (JPanel) this.getContentPane(); contentPane.setLayout(null); this.setSize(new Dimension(400, 300)); this.setTitle("Frame Title"); jButton2.setText("ok"); jButton2.setBounds(new Rectangle(137, 152, 79, 29)); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton2_actionPerformed(e); } }); contentPane.add(jButton1, null); contentPane.add(jButton2, null); } /**Overridden so we can exit when window is closed*/ protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } void jButton1_actionPerformed(ActionEvent e) { try { java.net.URL url = new java.net.URL( "http://localhost:8080/examples/servlet/DataStreamEcho"); java.net.URLConnection con = url.openConnection(); con.setUseCaches(false); con.setDoOutput(true); con.setDoInput(true); ObjectOutputStream out = new ObjectOutputStream(con.getOutputStream()); System.out.println("Writing test data"); out.writeObject(jButton1.getText()); out.flush(); out.close(); ObjectInputStream in = new ObjectInputStream(con.getInputStream()); Vector stringValue = (Vector)in.readObject(); in.close(); for(int i=0;i<stringValue.size();i++) System.out.println((String)stringValue.elementAt(i)); } catch(Exception ex) { ex.printStackTrace(); } } void jButton2_actionPerformed(ActionEvent e) { try { java.net.URL url = new java.net.URL( "http://localhost:8080/examples/servlet/DataStreamEcho"); java.net.URLConnection con = url.openConnection(); con.setUseCaches(false); con.setDoOutput(true); con.setDoInput(true); ObjectOutputStream out = new ObjectOutputStream(con.getOutputStream()); System.out.println("Writing test data"); out.writeObject(jButton2.getText()); out.flush(); out.close(); ObjectInputStream in = new ObjectInputStream(con.getInputStream()); Vector stringValue = (Vector)in.readObject(); in.close(); for(int i=0;i<stringValue.size();i++) System.out.println((String)stringValue.elementAt(i)); } catch(Exception ex) { ex.printStackTrace(); } } } public class Frame1 { public static void main(String[] args) { Frame frame = new Frame2(); frame.setSize(400,300); frame.show(); } } 后台代码: import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*;public class DataStreamEcho extends HttpServlet{
public void service(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException { Vector vec = new Vector(); try{
ObjectInputStream in = new ObjectInputStream(req.getInputStream()); ObjectOutputStream out = new ObjectOutputStream(resp.getOutputStream()); resp.setContentType("application/octet-stream"); //HttpSession session = req.getSession(true);
String aa = (String)in.readObject(); //session.setAttribute(aa,"hello hello");
[email protected]
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
class Frame2 extends JFrame {
JPanel contentPane;
JButton jButton1 = new JButton();
JButton jButton2 = new JButton(); /**Construct the frame*/
public Frame2() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
jButton1.setText("happy");
jButton1.setBounds(new Rectangle(135, 86, 79, 29));
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
jButton2.setText("ok");
jButton2.setBounds(new Rectangle(137, 152, 79, 29));
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
contentPane.add(jButton1, null);
contentPane.add(jButton2, null);
}
/**Overridden so we can exit when window is closed*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
} void jButton1_actionPerformed(ActionEvent e) {
try
{
java.net.URL url = new java.net.URL(
"http://localhost:8080/examples/servlet/DataStreamEcho");
java.net.URLConnection con = url.openConnection();
con.setUseCaches(false);
con.setDoOutput(true);
con.setDoInput(true);
ObjectOutputStream out = new ObjectOutputStream(con.getOutputStream());
System.out.println("Writing test data");
out.writeObject(jButton1.getText());
out.flush();
out.close();
ObjectInputStream in = new ObjectInputStream(con.getInputStream());
Vector stringValue = (Vector)in.readObject();
in.close();
for(int i=0;i<stringValue.size();i++)
System.out.println((String)stringValue.elementAt(i));
}
catch(Exception ex)
{
ex.printStackTrace();
} } void jButton2_actionPerformed(ActionEvent e) {
try
{
java.net.URL url = new java.net.URL(
"http://localhost:8080/examples/servlet/DataStreamEcho");
java.net.URLConnection con = url.openConnection();
con.setUseCaches(false);
con.setDoOutput(true);
con.setDoInput(true);
ObjectOutputStream out = new ObjectOutputStream(con.getOutputStream());
System.out.println("Writing test data");
out.writeObject(jButton2.getText());
out.flush();
out.close();
ObjectInputStream in = new ObjectInputStream(con.getInputStream());
Vector stringValue = (Vector)in.readObject();
in.close();
for(int i=0;i<stringValue.size();i++)
System.out.println((String)stringValue.elementAt(i));
}
catch(Exception ex)
{
ex.printStackTrace();
} }
}
public class Frame1
{
public static void main(String[] args)
{
Frame frame = new Frame2();
frame.setSize(400,300);
frame.show();
}
}
后台代码:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;public class DataStreamEcho extends HttpServlet{
public void service(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException
{ Vector vec = new Vector();
try{
ObjectInputStream in = new ObjectInputStream(req.getInputStream());
ObjectOutputStream out = new ObjectOutputStream(resp.getOutputStream());
resp.setContentType("application/octet-stream"); //HttpSession session = req.getSession(true);
String aa = (String)in.readObject();
//session.setAttribute(aa,"hello hello");
if(aa.equals("happy"))
{
vec.addElement("cc");
vec.addElement("aa");
String qqqq=(String)getServletContext().getAttribute(aa);
if(qqqq == null)
getServletContext().setAttribute(aa,"change is ok");
String kkkk = (String)getServletContext().getAttribute(aa);
vec.addElement(kkkk);
out.writeObject(vec);
out.flush();
out.close();
}
else if(aa.equals("ok"))
{
vec.addElement("kk");
vec.addElement("ss");
String pppp = (String)getServletContext().getAttribute(aa);
vec.addElement(pppp);
out.writeObject(vec);
out.flush();
out.close();
} }catch(Exception e)
{
}
}
}
点击OK按钮时,取不出来点HAPPY按钮时存在SESSION中的值。
java.net.ProtocolException: Cannot write output after reading input.
http://www.innovation.ch/java/HTTPClient/