Solved

applet error

Posted on 2010-11-29
11
425 Views
Last Modified: 2013-11-24
Hi all,

Im getting the following error when trying to run the attached applet and servlet:

java.net.UnknownServiceException: protocol doesn't support output
      at java.net.URLConnection.getOutputStream(URLConnection.java:792)
      at EchoApplet.onSendData(EchoApplet.java:105)
      at EchoApplet.access$0(EchoApplet.java:98)
      at EchoApplet$1.actionPerformed(EchoApplet.java:46)
      at java.awt.Button.processActionEvent(Button.java:392)
      at java.awt.Button.processEvent(Button.java:360)
      at java.awt.Component.dispatchEventImpl(Component.java:4630)
      at java.awt.Component.dispatchEvent(Component.java:4460)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Can someone help me out here please..
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

/**
 * Simple demonstration for an Applet <-> Servlet communication.
 */
public class EchoApplet extends Applet {
	private TextField inputField = new TextField();
	private TextField outputField = new TextField();
	private TextArea exceptionArea = new TextArea();

	/**
	 * Setup the GUI.
	 */
	public void init() {
		// set new layout
		setLayout(new GridBagLayout());

		// add title		
		Label title = new Label("Echo Applet", Label.CENTER);
		title.setFont(new Font("SansSerif", Font.BOLD, 14));
		GridBagConstraints c = new GridBagConstraints();
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.weightx = 1.0;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(5, 5, 5, 5);
		add(title, c);

		// add input label, field and send button
		c = new GridBagConstraints();
		c.anchor = GridBagConstraints.EAST;
		add(new Label("Input:", Label.RIGHT), c);
		c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 1.0;
		add(inputField, c);
		Button sendButton = new Button("Send");
		c = new GridBagConstraints();
		c.gridwidth = GridBagConstraints.REMAINDER;
		add(sendButton, c);
		sendButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				onSendData();
			}
		});

		// add output label and non-editable field
		c = new GridBagConstraints();
		c.anchor = GridBagConstraints.EAST;
		add(new Label("Output:", Label.RIGHT), c);
		c = new GridBagConstraints();
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 1.0;
		add(outputField, c);
		outputField.setEditable(false);

		// add exception label and non-editable textarea
		c = new GridBagConstraints();
		c.anchor = GridBagConstraints.EAST;
		add(new Label("Exception:", Label.RIGHT), c);
		c = new GridBagConstraints();
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.weighty = 1;
		c.fill = GridBagConstraints.BOTH;
		add(exceptionArea, c);
		exceptionArea.setEditable(false);
	}

	/**
	 * Get a connection to the servlet.
	 */
	private URLConnection getServletConnection()
		throws MalformedURLException, IOException {

		// Connection zum Servlet öffnen
		URL urlServlet = new URL(getCodeBase(), "echo");
		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;
	}

	/**
	 * Send the inputField data to the servlet and show the result in the outputField.
	 */
	private void onSendData() {
		try {
			// get input data for sending
			String input = inputField.getText();

			// send data to the servlet
			URLConnection con = getServletConnection();
			OutputStream outstream = con.getOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(outstream);
			oos.writeObject(input);
			oos.flush();
			oos.close();

			// receive result from servlet
			InputStream instr = con.getInputStream();
			ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
			String result = (String) inputFromServlet.readObject();
			inputFromServlet.close();
			instr.close();

			// show result
			outputField.setText(result);

		} catch (Exception ex) {
			ex.printStackTrace();
			exceptionArea.setText(ex.toString());
		}
	}
}

Open in new window

import java.io.*;

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

/**
 * Simple demonstration for an Applet <-> Servlet communication.
 */
public class EchoServlet extends HttpServlet {
	/**
	 * Get 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");

			// read a String-object from applet
			// instead of a String-object, you can transmit any object, which
			// is known to the servlet and to the applet
			InputStream in = request.getInputStream();
			ObjectInputStream inputFromApplet = new ObjectInputStream(in);
			String echo = (String) inputFromApplet.readObject();

			// echo it to the applet
			OutputStream outstr = response.getOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(outstr);
			oos.writeObject(echo);
			oos.flush();
			oos.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

Open in new window

0
Comment
Question by:oggiemc
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 34235614
use firebug to check the request being sent from the applet
0
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 34235623
Are you running the applet from the web server? (you need to be)
0
 

Author Comment

by:oggiemc
ID: 34235714
im using eclipse..tomcat is running there..how exactly should i be running it??where in the applet is the connection to the servlet?? ive never used applet servlet communication before, i found this example on the site.

Thanks
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 34235746
you should be loading the page from tomcat
0
 

Author Comment

by:oggiemc
ID: 34235842
0
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 34235887
no the url of the html page that contains the applet
0
 

Author Comment

by:oggiemc
ID: 34235963
please see attached screen shot..when i click for details nothing is coming up..

i have also tried a simple applet that just prints a string to the screen but nothing is coming up on the html page either.
0
 
LVL 92

Expert Comment

by:objects
ID: 34236324
no screenshot
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34237579
How are you running and hosting the applet? It should be in a web page on a web server. Otherwise you could get a file protocol in the url, which doesn't support output, as your stack trace is telling you...
0
 
LVL 3

Expert Comment

by:indigo23
ID: 34241402
i think this error happens because you have defined only the dopost method and you are send the request by GET. try override doGet method on your servlet
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34241645
Incidentally, you can easily verify what i mentioned by printing the value of 'urlServlet'
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Opening PDF on button click and fill new document 2 47
web application structure 18 100
jsp insert to database example 2 47
jmss example java 2 23
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question