从 scriptlet 转换为 JSF 托管 bean 问题

发布于 2024-10-05 06:28:43 字数 1693 浏览 3 评论 0原文

我正在尝试将此 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

当梦初醒 2024-10-12 06:28:43

我纠正我的答案。问题在于输入流的处理。这是固定代码:

    String response = "";
    try {

        Socket socket = new Socket("127.0.0.1", 8765);

        Reader reader = new InputStreamReader(socket.getInputStream());
        OutputStream outSocket = socket.getOutputStream();

        String str = "Hello!\n";
        byte buffer[] = str.getBytes();
        outSocket.write(buffer);

        CharArrayWriter result = new CharArrayWriter();
        char[] buf = new char[4096];
        int charsRead = 0;
        while ((charsRead = reader.read(buf)) != -1) {
            result.write(buf, 0, charsRead);
        }
        response = result.toString();

        if (str.equalsIgnoreCase("bye")) {
            socket.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
        response = "You must first start the server application (YourServer.java) at the command prompt.";
    }
    text.setValue(response);

I correct my answer. The problem is the processing of the input stream. Here is the fixed code:

    String response = "";
    try {

        Socket socket = new Socket("127.0.0.1", 8765);

        Reader reader = new InputStreamReader(socket.getInputStream());
        OutputStream outSocket = socket.getOutputStream();

        String str = "Hello!\n";
        byte buffer[] = str.getBytes();
        outSocket.write(buffer);

        CharArrayWriter result = new CharArrayWriter();
        char[] buf = new char[4096];
        int charsRead = 0;
        while ((charsRead = reader.read(buf)) != -1) {
            result.write(buf, 0, charsRead);
        }
        response = result.toString();

        if (str.equalsIgnoreCase("bye")) {
            socket.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
        response = "You must first start the server application (YourServer.java) at the command prompt.";
    }
    text.setValue(response);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文