public class HelloServlet extends HttpServlet
{
/* only for test purposes !
public void doGet(HttpServletRequest request, HttpServletResponse response)
{
try
{
FileWriter fw = new FileWriter(new File(“test.txt”));
fw.write(“doGet”);
fw.close();
}
catch (IOException e)
{
}
}
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
try {
response.setContentType("application/x-java-serialized-object");
InputStream in = request.getInputStream();
ObjectInputStream inputFromApplet = new ObjectInputStream(in);
String echo = (String) inputFromApplet.readObject();
FileWriter fw = new FileWriter(new File("test.txt"));
fw.write(echo);
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
now the applet, which submits a string:
public class HelloApplet extends Applet
{
public void init()
{
onSendData();
}
/**
* Get a connection to the servlet.
*/
private URLConnection getServletConnection()
throws MalformedURLException, IOException {
// Connection zum Servlet ýffnen
URL urlServlet = new URL(getCodeBase(), "hello");
URLConnection con = urlServlet.openConnection();
// konfigurieren
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty(
"Content-Type",
"application/x-java-serialized-object");
// und zurýckliefern
return con;
}
private void onSendData() {
try {
// get input data for sending
String input = "User 1 1:30";
// send data to the servlet
URLConnection con = getServletConnection();
OutputStream outstream = con.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject(input);
oos.flush();
oos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
now i have several problems and questions.
first i get an exception:
java.net.UnknownServiceException: protocol doesn’t support output
at java.net.URLConnection.getOutputStream(Unknown Source)
at HelloApplet.onSendData(HelloApplet.java:54)
at HelloApplet.init(HelloApplet.java:19)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)