Java 尝试将小程序与servlet通信时出现协议错误

Java 尝试将小程序与servlet通信时出现协议错误,java,servlets,applet,client-server,Java,Servlets,Applet,Client Server,当我将我的名字从applet发送到servlet时,我面临“java.net.UnknownServiceException:协议不支持输出”问题。 我试过了,但没有解决我的问题。 我的小程序代码是: import java.io.*; import javax.servlet.ServletException; import javax.servlet.http.*; public class AppletToServlet extends HttpServlet { // Getting

当我将我的名字从applet发送到servlet时,我面临“java.net.UnknownServiceException:协议不支持输出”问题。 我试过了,但没有解决我的问题。 我的小程序代码是:

import java.io.*;

import javax.servlet.ServletException;
import javax.servlet.http.*;

public class AppletToServlet extends HttpServlet {
// Getting 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");
            InputStream inputStream = request.getInputStream();
            ObjectInputStream inputFromApplet = new ObjectInputStream(inputStream);
            String string = (String) inputFromApplet.readObject();
// getting string value and passing to applet
OutputStream outputStream = response.getOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(string);
objectOutputStream.flush();
objectOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class AppletToServletExample extends Applet {
  private TextField inputField = new TextField(10);
  private TextField resultField = new TextField(10);

  public void init() {
    // add input label, field and send button
    add(new Label("Input Your Name", Label.RIGHT));
    add(inputField);
    Button sendButton = new Button("Send");
    add(sendButton);
    sendButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        SendData();
      }
    });
    // add output label as a non editable field
    add(new Label("Output:", Label.RIGHT));
    add(resultField);
    resultField.setEditable(false);
  }

  // Get a connection to the servlet.
  private URLConnection getServletConnection() throws MalformedURLException,
      IOException {
    URL urlServlet = new URL(getCodeBase(), "applettoservlet");
    URLConnection con = urlServlet.openConnection();
    con.setDoInput(true);
    con.setDoOutput(true);
    con.setUseCaches(false);
    con.setRequestProperty("Content-Type",
        "application/x-java-serialized-object");
    return con;
  }

  // Send the inputField data to the servlet and show the result in the
  // outputField.
  private void SendData() {
    try {
      String input = inputField.getText();
      // send data to the servlet
      URLConnection con = getServletConnection();
      OutputStream outputStream = con.getOutputStream();
      ObjectOutputStream oos = new ObjectOutputStream(outputStream);
      oos.writeObject(input);
      oos.flush();
      oos.close();
      // receive result from servlet
      InputStream inputStream = con.getInputStream();
      ObjectInputStream inputFromServlet = new ObjectInputStream(
          inputStream);
      String result = (String) inputFromServlet.readObject();
      inputFromServlet.close();
      inputStream.close();
      // show result
      resultField.setText(result);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}
servlet代码是:

import java.io.*;

import javax.servlet.ServletException;
import javax.servlet.http.*;

public class AppletToServlet extends HttpServlet {
// Getting 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");
            InputStream inputStream = request.getInputStream();
            ObjectInputStream inputFromApplet = new ObjectInputStream(inputStream);
            String string = (String) inputFromApplet.readObject();
// getting string value and passing to applet
OutputStream outputStream = response.getOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(string);
objectOutputStream.flush();
objectOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class AppletToServletExample extends Applet {
  private TextField inputField = new TextField(10);
  private TextField resultField = new TextField(10);

  public void init() {
    // add input label, field and send button
    add(new Label("Input Your Name", Label.RIGHT));
    add(inputField);
    Button sendButton = new Button("Send");
    add(sendButton);
    sendButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        SendData();
      }
    });
    // add output label as a non editable field
    add(new Label("Output:", Label.RIGHT));
    add(resultField);
    resultField.setEditable(false);
  }

  // Get a connection to the servlet.
  private URLConnection getServletConnection() throws MalformedURLException,
      IOException {
    URL urlServlet = new URL(getCodeBase(), "applettoservlet");
    URLConnection con = urlServlet.openConnection();
    con.setDoInput(true);
    con.setDoOutput(true);
    con.setUseCaches(false);
    con.setRequestProperty("Content-Type",
        "application/x-java-serialized-object");
    return con;
  }

  // Send the inputField data to the servlet and show the result in the
  // outputField.
  private void SendData() {
    try {
      String input = inputField.getText();
      // send data to the servlet
      URLConnection con = getServletConnection();
      OutputStream outputStream = con.getOutputStream();
      ObjectOutputStream oos = new ObjectOutputStream(outputStream);
      oos.writeObject(input);
      oos.flush();
      oos.close();
      // receive result from servlet
      InputStream inputStream = con.getInputStream();
      ObjectInputStream inputFromServlet = new ObjectInputStream(
          inputStream);
      String result = (String) inputFromServlet.readObject();
      inputFromServlet.close();
      inputStream.close();
      // show result
      resultField.setText(result);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

我几乎找遍了所有地方,但都没找到答案

在哪里运行小程序?您是否真的将其嵌入到页面并通过某种Web服务器请求它

代码

 URL urlServlet = new URL(getCodeBase(), "applettoservlet");
仅当您从web/应用程序服务器提供的页面运行小程序时,才会按预期工作。 尝试添加

System.out.println(urlServlet);

检查正在使用的url。

否,我正在尝试在neatbeans小程序查看器上运行它。不在浏览器中。那么该怎么办呢?我想有更优雅的方法可以做到这一点,但我会将url硬编码到服务器进行测试。替换
URL urlServlet=newurl(getCodeBase(),“applettoservlet”)带有
URL urlServlet=新URL(“http://myappserver/myServletPath/applettoserver");它删除了协议问题,但仍然存在错误。我想这是因为我走错了路。我的web项目名为“applet”,这两个类在其中的“”下。我将您的代码重写为“URL urlServlet=newURL(”htp://applet/AppletToServlet)但它仍然给出一个错误“java.net.UnknownHostException:AppletToServlet”我缺少什么?如果有人能解决我的问题。我会非常感激的是,越来越明显的是,你不知道自己在做什么。您正在尝试创建一个servlet和一个applet。因此,您需要一个应用服务器(用于servlet),即ApacheTomcat。安装应用服务器并部署servlet(扩展HTTPServlet的类)后,您将拥有一个类似internet的url“”,可以在小程序中使用。