从 scriptlet 转换为 JSF 托管 bean 问题
我正在尝试将此 scriptlet 代码 转换为 JSF 类。
视图代码
<f:view>
<h:form binding="#{jsfSocketClient.form}">
<h:outputText binding="#{jsfSocketClient.text}"/>
</h:form>
</f:view>
和java代码
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);
//writer.write((char)character);
//characters += (char)character;
}
//text.setValue(characters);
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;
}
当我运行scriptlet代码时,我得到的答案是 “服务器收到此消息:Hello!”
当我运行 JSF 代码时,我没有收到此回复。请纠正我的错误
,提前致谢
I am trying to convert this scriptlet code to JSF class.
The view code
<f:view>
<h:form binding="#{jsfSocketClient.form}">
<h:outputText binding="#{jsfSocketClient.text}"/>
</h:form>
</f:view>
and the 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);
//writer.write((char)character);
//characters += (char)character;
}
//text.setValue(characters);
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 scriptlet code, I am getting the answer as
"The server got this: Hello! "
When I run the JSF code I am not getting this reply. Please correct my mistake
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我纠正我的答案。问题在于输入流的处理。这是固定代码:
I correct my answer. The problem is the processing of the input stream. Here is the fixed code: