给你个例子,看看吧
servlet端
/**
* type class' function
*
* @author Likestar
*/
public class ServletToApplet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("GBK");
try {
PrintWriter out = response.getWriter();
out.println(" ");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/octest-stream");
try {
ObjectInputStream in = new ObjectInputStream(request.getInputStream());
Object obj = in.readObject();
in.close();
ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());
out.writeObject("成功");
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println(obj);
} public static void main(String[] args) {
}
}
servlet端
/**
* type class' function
*
* @author Likestar
*/
public class ServletToApplet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("GBK");
try {
PrintWriter out = response.getWriter();
out.println(" ");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/octest-stream");
try {
ObjectInputStream in = new ObjectInputStream(request.getInputStream());
Object obj = in.readObject();
in.close();
ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());
out.writeObject("成功");
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println(obj);
} public static void main(String[] args) {
}
}
解决方案 »
- onchange(this)在jsp里,怎么把这个this传到另1个js文件里去?
- servlet的生命周期和jsp的生命周期的一点迷惑?
- 关于struts中ActionMessages的问题
- 朋友:帮我解释下这段代码 做web分页时候碰到的
- ruby和rails有什么不同
- JSP中如何使用已经建立好的存储过程
- jsp中关于使用session控制用户登录的问题
- java中对浮点数如何设定小数点位数?
- 我无法用JBuilder7建立JSP程序!
- 跪求各位大神:各位大神为什么我的在java里面查的数为3 把代码放到数据库中查为0啊
- 关于用java实现web下的语音聊天
- 调用ActiveX文档,出现乱码
/**
* type class' function
*
* @author Likestar
*/
public class AppletToServlet extends Applet { private String msg = "你好"; public void init() {
Button but = new Button("测试");
but.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try {
URL url = new URL(getDocumentBase(),
"servlet/ServletToApplet");
System.out.println(url);
URLConnection con = url.openConnection();
con.setUseCaches(true);
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-type",
"application/octest-stream");
sendServletObject(con,"真实的世界");
System.out.println("发送信息");
msg=(String) getServletObject(con);
repaint();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
} });
add(but);
} public void paint(Graphics g) {
g.drawString(msg, 20, 20);
} /**
* 给Servlet传送对象
*
* @param output
* @param obj
* @throws IOException
*/
public void sendServletObject(URLConnection con, Object obj)
throws IOException {
OutputStream output = con.getOutputStream();
ObjectOutputStream dataout = new ObjectOutputStream(output);
dataout.writeObject(obj);
dataout.flush();
dataout.close();
}
/**
* 获得Servlet的传送对象
*
* @param input
* @return @throws
* IOException
*/
public Object getServletObject(URLConnection con) throws IOException {
InputStream input = con.getInputStream();
ObjectInputStream datain = new ObjectInputStream(input);
Object obj = null;
try {
obj = datain.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("对象读取错误!");
}
datain.close();
return obj;
}
}