Hi DENIZ :-)


Because today is a holiday in Canada :-) ,  so I sleep for long time :-) :-) ,
so when I came to office, I find several your emails :-) and I find with
Abhay's email( using "_self"), you have already fixed your problem :-)


0
but I think if we use this way ,  in fact now we don't use
"sendRedirect" any more,  because now we "invoke" servlet2 directly
in our applet. But from your last email, with this way, you also can:
"invoke servlet2" + keep the same "session"
So I think it is a good way :-)  but I didn't ever try it :-)

1
After I receive your this mail, I did a testing:
     a   in my applet  --  [send+read]
     b   in servlet1 -- doPost
          [a]   [read+sendRedirect_to_servlet2]
          [b]   [read+send_directly_from_servlet1+sendRedirect_to_servlet2]
          [c]   [read+sendRedirect_to_servlet2+send_directly_from_servlet1]
          [d] -- like your code -- [b] + [put "flush" BEHIND "sendRedirect"]

I think if we want to "sendRedirect" to servlet2 in "doPost" of servlet1,
it means now we want to let servlet2 to response to our applet, so
now we [don't need+should not :-)] let servlet1 to response anything
to our servlet! -- but I find your code is SIMILAR with [b]  -- BUT not
very similar :-) , because you put:
[response.sendRedirect("http://deniz:8080/servlet/Servlet_2");]
BETWEEN
[response.getOutputStream();]
AND
[out.flush();]

So did you want to let BOTH servlet1 and servlet2 ALL response
to our applet ?  I don't know if it is possible :-)   but I also did a #[d]
testing
-- [b] + [put "flush" BEHIND "sendRedirect"] ,  but now the result is same
with [a] -- my applet only get the response from servlet2, and no Exception.

The following is the result of my testing:
[a]  -- it can "sendRedirect" to servlet2   //haha :-)
[b]  -- now our applet only get the "response" from servlet1,  and servlet2
          didn't be "invoked". --  because servlet1 has already responsed
          to our applet, so servlet1 will not "sendRedirect" to servlet2
[c]  -- it can "sendRedirect" to servlet2 , but I get a Exception:
          IllegalStateException ...   because now the response
          has already been "sendRedirect" to servlet2, so now
          servlet1 can not response again  :-)
[d] --   same with [a] -- it can "sendRedirect" to servlet2, and
           I didn't get Exception.

2
My suggesting:
     a   if you don't want to let BOTH servlet1 and servlet2 ALL response
          to our applet, I suggest you don't let servlet1 response anything to
          applet -- servlet1 just read some data from applet, and decide which
          servlet shold be "sendRedirect" , that is enough .
      b  But if you want to let BOTH servlet1 and servlet2 ALL response to
          our applet, I don't know how to do it :-)

3
the following is my testing code:

    a   in doPost in servlet1:

          //read...
         is=new ObjectInputStream( request.getInputStream() );
         s_tmp=(String)is.readObject();

         //response directly from servlet1...
        response.setContentType("application/octet-stream");
        os=new ObjectOutputStream( response.getOutputStream() );
        os.writeObject("in 0, in POST="+s_tmp);

        //for DENIZ's code -- #[d] case
        os.flush();

       //sendRedirect to servlet2...
       response.sendRedirect(response.encodeUrl("/servlet/servlet_ht2"));

       is.close(); is=null;
       os.close(); os=null;

and my testing is adjust the "order" of the following:
"response directly from servlet1..."
"sendRedirect to servlet2..."
"for DENIZ's code -- #[d] case"

    b   in my applet  --  it is similar with your code for your button

    //send to servlet1...
    os=new ObjectOutputStream( con.getOutputStream() );
    os.writeObject( "from applet" );
    os.flush();

    is=new ObjectInputStream(con.getInputStream());
    s_tmp=(String)is.readObject();
    System.out.println("return from servlet1 or servlet2 ="+s_tmp);
    s_tmp=null;

    is.close(); is=null;
    os.close(); os=null;


Hope my testing is right :-)  now I find I am confused -- my head
is a little bigger_and_heavier  than before :-) :-) :-) :-) :-)    -- or
it is because I sleep too long this morning?  :-)

Welcome to any instruction!   So I can set the size of my head
to "default" !  :-) :-) :-) :-) :-)

Bo
Oct.09, 2000

DENIZ DEMIR wrote:

> I have tried to invoke sendRedirect() in doPost method when the applet's
> button is pushed, but it did not work. Here is the code:
> Please help me...
>
> -------this is applet acctionPerformed code------------
>
>   void jButton1_actionPerformed(ActionEvent e) {
>     System.out.println("Sending data...\n");
>   try {
>     URL url = new URL("http://localhost:8080/servlet/Servlet_1");
>     URLConnection con = url.openConnection();
>
>     con.setUseCaches(false);
>     con.setDoOutput(true);
>
>     ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
>     DataOutputStream out = new DataOutputStream(byteOut);
>     out.writeUTF(txtField.getText());
>     out.flush();
>     byte[] buf = byteOut.toByteArray();
>     con.setRequestProperty("Content-type", "application/octet-stream");
>     con.setRequestProperty("Content-length", "" + buf.length );
>
>     DataOutputStream dataOut = new DataOutputStream(con.getOutputStream());
>     dataOut.write(buf);
>     dataOut.flush();
>     dataOut.close();
>   }catch(Exception ex) { ex.printStackTrace(); }
>     System.out.println("Data was sent...");
>   }
>
> ------and this is the servlet doPost code----------
>
>   public void doPost(HttpServletRequest request, HttpServletResponse
> response) throws ServletException, IOException {
>     response.setContentType("text/html");
>     PrintWriter out = new PrintWriter (response.getOutputStream());
>     out.println("<html>");
>     out.println("<head><title>Servlet1</title></head>");
>     out.println("<body>");
>     out.println("The servlet has received a POST. This is the reply.");
>     out.println("</body></html>");
>
>     DataInputStream in = new DataInputStream(request.getInputStream());
>     String s = in.readUTF();
>     in.close();
>     out.println("<p>s: " + s);
>     response.sendRedirect("http://deniz:8080/servlet/Servlet_2");
>     out.flush();
>     out.close();
>   }
>
> ___________________________________________________________________________
> 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

Reply via email to