response.setContentType("text/html"); will work for both conditions...
Vj ----- Original Message ----- From: "Jean-Baptiste Noël" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Thursday,February 27,2003 9:16 PM Subject: applet communic with servlet > Hello, > I include/understand or comes the error but I do not know how resolve > the problem. > I have applet which commnunic with a servlet. The applet sends > a "string" to the servlet. The servlet receive the "string" and > compares it with another. If it is not good, then the servlet re-send > a "stringError". > If not the servlet returns a page HTML. > It is here that is the problem when the servlet returns page HTML I > have the error "java.io.StreamCorruptedException: Does InputStream > does not contain have serialized object" . > How to make so that the applet manages the two types of objects which > it receives? Is this possible? > The code of the servlet and the applet below. > Thank you very much. > > //My Applet : > public class EchoApplet extends Applet { > ... > ... > sendButton.addActionListener(new ActionListener() { > public void actionPerformed(ActionEvent e) { > onSendData(); > } > } > /** > * Get a connection to the servlet. > */ > private URLConnection getServletConnection() > throws MalformedURLException, IOException { > URL urlServlet = new URL(getCodeBase(), "echo"); > URLConnection con = urlServlet.openConnection(); > con.setDoInput(true); > con.setDoOutput(true); > con.setUseCaches(false); > con.setRequestProperty("Content-Type", > "application/x-java-serialized-object"); > return con; > } > > /** > * Send the inputField data to the servlet and show the result in the > outputField.*/ > private void onSendData() { > try { > String log = login.getText(); > // send data to the servlet > URLConnection con = getServletConnection(); > OutputStream outstream = con.getOutputStream(); > ObjectOutputStream oos = new ObjectOutputStream(outstream); > oos.writeObject(log); > 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()); > } > } > } > > > //MY servlet > public class EchoServlet extends HttpServlet { > public void doPost(HttpServletRequest request,HttpServletResponse > response) > throws ServletException, IOException { > try { > InputStream in = request.getInputStream(); > ObjectInputStream inputFromApplet = new ObjectInputStream(in); > String echo = (String)inputFromApplet.readObject(); > String resultToApplet; > if (!(echo.equals("ok")) > { > response.setContentType("application/x-java-serialized-object"); > resultToApplet = "Error login or password"; > // echo it to the applet > OutputStream outstr = response.getOutputStream(); > ObjectOutputStream oos = new ObjectOutputStream(outstr); > oos.writeObject(resultToApplet); > oos.flush(); > oos.close(); > } > > else > { > //my new html page > response.setContentType("text/html; charset=ISO-8859-1"); > PrintWriter out = response.getWriter(); > out.print("<html><head></head><h1>OK</h1></html>"); > } > } catch (Exception e) { > e.printStackTrace(); > } > } > } > > ___________________________________________________________________________ > To unsubscribe, send email to [EMAIL PROTECTED] and include in the body > of the message "signoff SERVLET-INTEREST". > > Archives: http://archives.java.sun.com/archives/servlet-interest.html > Resources: http://java.sun.com/products/servlet/external-resources.html > LISTSERV Help: http://www.lsoft.com/manuals/user/user.html ___________________________________________________________________________ To unsubscribe, send email to [EMAIL PROTECTED] and include in the body of the message "signoff SERVLET-INTEREST". Archives: http://archives.java.sun.com/archives/servlet-interest.html Resources: http://java.sun.com/products/servlet/external-resources.html LISTSERV Help: http://www.lsoft.com/manuals/user/user.html