Servlet - Applet Kommunikation

  • Themenstarter Themenstarter mariella
  • Beginndatum Beginndatum
Status
Nicht offen für weitere Antworten.
M

mariella

Gast
Hallo Ich bin dabei mein erstes Servlet Applet zu schreiben. Aber ich habe den Problem das keiner Kommunikation möglich ist zwischen die beiden. villeicht kann mir jemanden ein Tip geben und den Problem erkennen?

Applet Seite:

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

			// Connection zum Servlet öffnen
			URL urlServlet = new URL(getCodeBase(), "AppletServlet");
			URLConnection con = urlServlet.openConnection();

			// konfigurieren
			//con.setRequestMethod("POST");
			//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(Anfrage query) {
			try {
				// get input data for sending
				Anfrage input = query;

				// send data to the servlet
				URLConnection con = getServletConnection();
				OutputStream outstream = con.getOutputStream();
				jTextField4.setText("Bin hier jetzt in onSendData2" + con );
				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);
				AusgabeDB tabelle = (AusgabeDB) inputFromServlet.readObject();
				System.out.println("Die Ausgabe sieht wie folgt aus " + tabelle);
				inputFromServlet.close();
				instr.close();

				// show result
				//outputField.setText(result);

			} catch (Exception ex) {
				ex.printStackTrace();
				System.out.println(ex.toString());
			}
		}

jetzt die anderer Seite von Servlet:

Code:
		/*
		 * (non-Java-doc)
		 * 
		 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
		 *      HttpServletResponse response)
		 */
		protected void doPost(HttpServletRequest request,
				HttpServletResponse response) throws ServletException, IOException {
			try {
				new Test("OK");
				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);
				Anfrage dbanfrage = (Anfrage) inputFromApplet.readObject();

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

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

		public void init() throws ServletException {
			//dbUrl = getServletConfig().getInitParameter("URL");
			//dbUser = getServletConfig().getInitParameter("Login");
			//dbPassword = getServletConfig().getInitParameter("pass");

		}

Ich bin sehr dankbar für jede Hilfe ; )
 
HI mariella,
gibt das programm irgendeine fehler meldung aus?
Ich habe jetzt so beim überfliegen keinen fehler gesehen.
Kommt am Servlet überhaupt was an?
Ich werde es heute abend mal daheim testen.
 
Im Servlet
Code:
...
AusgabeDB result= getDaten(dbanfrage);

OutputStream outstr = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstr);

response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("application/octet-stream");

oos.writeObject(result);
oos.flush();
und im Applet
Code:
URLConnection con = url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setDefaultUseCaches(false);
con.setUseCaches(false);
con.setRequestProperty("Content-Type", "application/octet-stream");
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(con.getOutputStream()));
out.writeObject(...);
out.flush();
out.close();
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(con.getInputStream()));
Object response = in.readObject();
in.close();
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben