Solved

applet error

Posted on 2010-11-29
11
427 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 46
ejb stateless example 2 68
Eclipse Java import and method not resolved 4 135
Code Coverage and Coding Standards 2 81
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

739 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