2 Replies Latest reply on May 2, 2009 7:17 AM by Adrien Adrien

    How to submit jsf page from applet with HttpURLConnection

    Adrien Adrien Apprentice

      Hi,
      I want to submit a jsf form from an applet:

      The jsf page:

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml"
       xmlns:f="http://java.sun.com/jsf/core"
       xmlns:h="http://java.sun.com/jsf/html"
       xmlns:rich="http://richfaces.org/rich"
       xmlns:a4j="http://richfaces.org/a4j"
       xmlns:ui="http://java.sun.com/jsf/facelets"
       >
      <head>
      
      </head>
      <body>
      <f:view>
       <h:form id="chat">
       <h:inputText id="msg" value="#{ctrlChatSendMsg.msg}" />
       <h:commandButton id="send" action="#{ctrlChatSendMsg.send}" value="" />
       </h:form>
      </f:view>
      </body>
      </html>


      And the code from my applet :



      HttpURLConnection connection = (HttpURLConnection) lorizonRead.openConnection();
       //HttpURLConnection connection =(HttpURLConnection) conLorizonRead;
       connection.setUseCaches(false);
       connection.setDoOutput(true);
       connection.setDoInput(true);
       connection.setRequestMethod("POST");
       connection.setRequestProperty("Content-Type",
       "application/x-www-form-urlencoded");
       String url="http://localhost:8080/testchat.jsf?"
       +URLEncoder.encode("chat")+"="+URLEncoder.encode("chat")
       +"&"+URLEncoder.encode("chat:msg")+"="+URLEncoder.encode("")
       +"&"+URLEncoder.encode("chat:send")+"=";
       DataOutputStream stream = new
       DataOutputStream(connection.getOutputStream ());
       stream.writeBytes(url);
       stream.flush();
       stream.close();
      

      This is not work, i see in server log the session is created, but may back bean never called.
      I found no example.
      If i pass by a browser then the backbean is called. But from my applet nothing append expected a log that the server has created a session
      Any body has idea of what is wrong in my applet?
      Thanks!

        • 1. Re: How to submit jsf page from applet with HttpURLConnectio
          Nick Belaevski Master

          Hello,

          JSF needs several service parameters to be passed to Faces Servlet. Check all form fields.

          • 2. Re: How to submit jsf page from applet with HttpURLConnectio
            Adrien Adrien Apprentice

            Ok thanks i found :

            1-First you need the jsessionid,if you don t have create a connexion :

            private static final String REG_EXPR_JSESSIONID = "jsessionid=[^\"]+";
            private static final String REG_EXPR_ACK="ackmsg[0-9]+";
             public static final Pattern patternSession = Pattern.compile(REG_EXPR_JSESSIONID);
            public static String createSessionID(String jsfpath) throws MalformedURLException, IOException{
             URLConnection urlConnection = new URL(jsfpath).openConnection();
             urlConnection.setDoOutput(true);
             BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            
             String jid =null;// catsession(in);
             String form=null;
             while ((form = in.readLine()) != null && jid==null){
             Matcher m = patternSession.matcher(form);
             if(m.find()){
             jid=m.group();
             }
             }
             in.close();
             return jid;
            
             }


            2-send the request, becarefull at 2 things :
            a) the url must contains the jsessionid like :http://localhost/mypage.jsf;jsessionid=XXXXX
            b) pass the view state in request params like javax.faces.ViewState=j_id1
            For me i already send j_id1 and i ve got the value when i call the page for the first time, you need to change it.

            Map<String, String[]> params=new Hashtable<String, String[]>();
             params.put("chat", new String[]{"chat"});
             params.put("chat:msg", new String[]{"BRAVOOOOOOOOOOOOOOOOOO"});
             params.put("chat:send", new String[]{""});
             params.put("chat:msgid", new String[]{"464"});
             params.put("javax.faces.ViewState", new String[]{"j_id1"});
            
             InputStream is = doPost("http://localhost:8080/testchat.jsf;"+jsessionid, params);
             is.close();


            I ve got the method doPost from here :
            http://balusc.blogspot.com/2006/05/httpservletutil.html#doPost