Failed to update bean bound component value

I am trying to create a socket client with a JSF web interface. In this application, the client connects to the server, sends a message to the server, receives a message from the server and displays it on the JSF page.

I was able to connect to the socket send server and receive the message. I can not show the message from the server in the browser window. When I print to the console, it displays correctly.

My jsf code is:

<f:view> <h:form binding="#{jsfSocketClient.form}"> <a4j:keepAlive beanName="jsfSocketClient"/> <h:outputText binding="#{jsfSocketClient.outputMessageBinding}"/> <br/> <h:inputText value="#{jsfSocketClient.inputFromUser}"/> <br/> <h:commandButton action="#{jsfSocketClient.sendMessage}" value="Send"/> </h:form> </f:view> 

And my java code:

 public HtmlForm getForm() { try { socket = new Socket("192.168.1.115", 4444); response = "Connection Success"; outputMessageBinding.setValue("Connection Success"); out = new PrintWriter(socket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); } catch (Exception e) { e.printStackTrace(); response = "You must first start the server application (YourServer.java) at the command prompt."; outputMessageBinding.setValue(response); } return form; } public String sendMessage() { outputMessageBinding.setValue(""); try { //String str = "Hello!\n"; out.println(getInputFromUser()); try { String line = in.readLine(); outputMessageBinding.setValue(line); System.out.println("Text received :" + line); } catch (IOException e) { outputMessageBinding.setValue(e.getMessage()); System.out.println("Read failed"); System.exit(1); } //response = result.toString(); if (getInputFromUser().equalsIgnoreCase("bye")) { socket.close(); } } catch(Exception e) { outputMessageBinding.setValue(e.getMessage()); e.printStackTrace(); } return ""; } 

When I load the jsf page, if the server is connected, "Connection Success" is displayed correctly, if it is not connected, the error message is displayed correctly. When I try to display the server message on the screen, it is not displayed. How can i fix this?

Update If I create a new output text component and set the message from the server as its value, the server message displays correctly. I want to know why the binding does not work in my case?

+4
source share
1 answer

Opening new sockets from a JSF / web page is the main anti-pattern. Why would you want to do that?

Do you know all the consequences / limitations / risks / problems?

Update:

Creating sockets from web pages has several performance and security implications.

If you just want to practice Java sockets, the easiest way is with command line clients. http://download.oracle.com/javase/tutorial/networking/sockets/clientServer.html

No need to add extra complexity with JSF or any other web technologies. You may have sockets without a web server. (In fact, sockets existed long before http).

+2
source

All Articles