(NOTE:  this is the duplicate of a post I sent to the
Tomcat user's mailing list.  So far (a few hours only)
I've gotten no replies.  I have searched the arhives of
this list, but have found no *direct* statements about
why the following may/may not work.  -mg)

Hello all,

I'm developing a GUI applet in swing that communicates
with Servlets handling a multitude of connections
to other applications.  Strangely enough, one thing
I'm having problem with is sending the JSESSIONID
cookie back to Tomcat (through Apache, if that
matters).

This seems relatively straightforward, but does not work when
the cookie is a existing JSESSIONID.  By not work, I mean that
I get no response from my servlet, and no apparent errors in
the "tomcat.log" file.  My servlet code is supposed to bounce the
header information sent by the client back in the form of HTML.
(The applet and servlet code are included below).

Note that I've hard-coded strings into the cookie property, even
strings with JSESSIONID={some bogus id} and the servlet
is invoked and returns just fine (of course, it creates a new
session).  So, the servlet (or Tomcat) only has problems when
the session id maps to a current session.  Here is the header
my applet sends to Tomcat and below that is the servlet
source.  If anyone has any suggestions as to why Tomcat
(or the Servlet API) doesn't like this, please let me know.
(This is Apache 1.3.11, Tomcat 3.1 release build)

Thanks in advance,

Mike Grady



Sample HTTP header sent to Tomcat/Servlet (servlet does NOT respond)

content-type=application/x-www-form-urlencoded
connection=keep-alive
cookie=JSESSIONID=To1037mC4023944164377369At
accept=text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
host=devbox
content-length=63
user-agent=Java1.3.0


In my JApplet, I have code like the following:

   HttpURLConnection servCon = (HttpURLConnection)
urlServlet.openConnection();

   if (m_strCurrentCookie.length() > 0)
   {
        servCon.setRequestProperty("Cookie", m_strCurrentCookie);
   }

   servCon.setDoOutput(true);
   servCon.setDoInput(true);
   servCon.setRequestMethod("POST");

   StringBuffer postString = new StringBuffer();

    /*I set some variables in the post string */

   java.io.DataOutputStream out = new java.io.DataOutputStream(
                              servCon.getOutputStream());

   out.writeBytes(postString.toString());
   out.close();

    java.io.BufferedReader in = new java.io.BufferedReader(
    new java.io.InputStreamReader(
     servCon.getInputStream()));

/*    get the cookie sent back by the servlet container for later...*/
   m_strCurrentCookie = servCon.getHeaderField("Set-Cookie");
   m_strCurrentCookie = m_strCurrentCookie.substring(0,
m_strCurrentCookie.indexOf(";"));

/*Do something with return from servlet*/


This is my Servlet:

public class Bounce extends javax.servlet.http.HttpServlet
{
     public void doPost(javax.servlet.http.HttpServletRequest request,
          javax.servlet.http.HttpServletResponse response)
     {
          javax.servlet.http.HttpSession session = request.getSession(true);

          /* get the contents of the request and "bounce" them back to the
user*/
          java.util.Enumeration headerContents = request.getHeaderNames();
          java.lang.StringBuffer headerString = new
java.lang.StringBuffer("");
          while (headerContents.hasMoreElements())
          {
               java.lang.String headerKey = (java.lang.String)
headerContents.nextElement();
               headerString.append(headerKey + "=");
               headerString.append(request.getHeader(headerKey) + "<BR>");
          }

        /*get an input stream and store it in another string*/
       java.lang.StringBuffer bodyStream = new java.lang.StringBuffer();
      try
      {
           java.io.BufferedReader bodyReader = new java.io.BufferedReader(
                       new
java.io.InputStreamReader(request.getInputStream()));

           java.lang.String bodyLine;
           while ((bodyLine = bodyReader.readLine()) != null)
           {
                bodyStream.append(bodyLine);
           }
           bodyReader.close();

           response.setContentType("text/html");
           java.io.PrintWriter outPost = response.getWriter();

           outPost.println("<HTML>");
           outPost.println("<TITLE>Bounce!</TITLE>");
           outPost.println("<BODY><H3>Header Data follows</H3><BR>");
           outPost.print(headerString.toString());
           outPost.println("<BR><H3>POST Data follows</H3><BR>");
           outPost.print(bodyStream.toString());
           outPost.println("</BODY>");
           outPost.println("</HTML>");
      }
      catch (java.io.IOException ioE)
      {
           System.out.println("IOException!");
      }
   }
}

___________________________________________________________________________
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