I am trying to convert this script code to a JSF class.
Submission code
<f:view>
<h:form binding="#{jsfSocketClient.form}">
<h:outputText binding="#{jsfSocketClient.text}"/>
</h:form>
</f:view>
and java code
private HtmlForm form = new HtmlForm();
private HtmlOutputText text = new HtmlOutputText();
public HtmlForm getForm()
{
System.out.println("instance: "+FacesContext.getCurrentInstance().getResponseWriter());
ResponseWriter writer = (FacesContext.getCurrentInstance()).getResponseWriter();
try{
int character;
Socket socket = new Socket("127.0.0.1", 8765);
InputStream inSocket = socket.getInputStream();
OutputStream outSocket = socket.getOutputStream();
String str = "Hello!\n";
byte buffer[] = str.getBytes();
outSocket.write(buffer);
char characters = 0;
while ((character = inSocket.read()) != -1) {
text.setValue((char)character);
}
if(str.equalsIgnoreCase("bye"))
{
socket.close();
}
}
catch(Exception e)
{
e.printStackTrace();
text.setValue("You must first start the server application (YourServer.java) at the command prompt.");
}
return form;
}
When I run the script code, I get a response like "The server received this: Hello!"
When I run the JSF code, I do not get this answer. Please correct my mistake.
Thanks in advance
source
share