hy_zhym
2011-03-18 14:00
浏览 191
已采纳

在applet中用URLConnection访问servlet为什么没反应?同样的代码在jsp中就可以。

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">



here



  

上面是页面代码。

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就没反应,这是怎么回事呢?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • enet_java 2011-03-18 14:29
    已采纳

    AppletTest.class 和html页面必须在同一目录下。

    点赞 评论
  • enet_java 2011-03-18 14:33

    去掉codebase="." 试试

    点赞 评论

相关推荐 更多相似问题