Solved

applet error

Posted on 2010-11-29
11
418 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
Comment Utility
use firebug to check the request being sent from the applet
0
 
LVL 92

Assisted Solution

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

Author Comment

by:oggiemc
Comment Utility
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
 
LVL 92

Assisted Solution

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

Author Comment

by:oggiemc
Comment Utility
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 92

Assisted Solution

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

Author Comment

by:oggiemc
Comment Utility
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
Comment Utility
no screenshot
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
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
Comment Utility
Incidentally, you can easily verify what i mentioned by printing the value of 'urlServlet'
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now