日期:2014-05-16 浏览次数:20309 次
import java.applet.Applet; import java.io.BufferedInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; import java.net.URLConnection; import javax.swing.JApplet; public class AppletTest extends JApplet { public void init(){ try{ String addr = "http://192.168.100.162:8080/Payment/Dser"; URL url = new URL(addr); URLConnection conn = url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestProperty("Content-Type", "application/x-java-serialized-object"); InputStream inS = conn.getInputStream(); String header = conn.getHeaderField(0); inS = new BufferedInputStream(inS); Reader r = new InputStreamReader(inS); int c; System.out.println("===================输出结果======================"); while ( (c = r.read()) != -1) System.out.print( (char) c); inS.close(); }catch(Exception e){ } } }
?上面这是applet代码。
?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>here</title> </head> <body> <APPLET codebase="." code="AppletTest.class" WIDTH=200 HEIGHT=100 > </APPLET> </body> </html>
?
上面是页面代码。
?
package com.jhhz.test; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class Dser */ public class Dser extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Dser() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setHeader("pragma", "no-cache"); response.setHeader("cache-control", "no-cache"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.print("123456789"); System.out.println("in!"); out.flush(); out.close(); } }
?
上面是servlet代码。
?
把applet代码复制到jsp中测试完全成功,可是一用html调用applet,servlet就没反应,这是怎么回事呢?
?
问题解决了servlet应该这样写:
package com.jhhz.test; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class CallServlet */ public class Dser extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Dser(){ super(); } public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException { (req.getInputStream()); resp.setContentType("application/octest-stream"); ByteArray