Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSP文件中的JavaScript函数_Javascript_Java_Jsp - Fatal编程技术网

JSP文件中的JavaScript函数

JSP文件中的JavaScript函数,javascript,java,jsp,Javascript,Java,Jsp,我将javascript函数附加到JSP文件中,但它不起作用。我在chatPage.jsp中发现了“sendMessage()未定义”错误。请检查我的代码,告诉我哪里做错了 在我的loginpage.jsp中,我将获得username和roomName,它将通过UserNameServlet,然后它将getRequestDispatcher发送到chatpage.jsp,转发username和roomName的值 这是我的loginpage.jsp: <html> <h

我将javascript函数附加到JSP文件中,但它不起作用。我在chatPage.jsp中发现了“sendMessage()未定义”错误。请检查我的代码,告诉我哪里做错了

在我的loginpage.jsp中,我将获得username和roomName,它将通过UserNameServlet,然后它将getRequestDispatcher发送到chatpage.jsp,转发username和roomName的值

这是我的loginpage.jsp:

<html>
    <head>
        <title>TODO supply a title</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form action="UserNameServlet" name="submitForm" onchange="handleNewRoom()" method="GET">
            <mark>Please select a room:</mark>
            <select id="roomSelect" name="roomSelect">
                <option value="room1">Room 1</option>
                <option value="room2">Room 2</option>
            </select>
            <br>
            <br>

            <mark>Please Enter username</mark>
            <input type="text" name="username" size="20"/>
            <input type="submit" value="Enter" />
        </form>

    </body>
</html>
这是我的chatPage.jsp

<html>
    <head>
        <title>TODO supply a title</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    </head>
    <body>
        <textarea id="messagesTextArea" readonly="readonly" rows="10" cols="45"></textarea> <br>
        <input type="text" id="messagesText" size="50" /> <br>
        <input type="button" value="Send" onclick="sendMessage()"/>

    </body>

        <script type="text/javascript">
            var websocket = new WebSocket("ws://"+document.location.host+"+"document.location.pathname+"+""chatroomServerEndpoint/"${roomName});
            websocket.onmessage = function processMessage(message){
                var jsonData = JSON.parse(message.data);
                if(jsonData.message !== null) messagesTextArea.value += jsonData.message + "\n";
            }; 

            function sendMessage(){
                alert("hello");
            }
        </script>   
</html>

当然,您需要关闭
input
标记。看起来您的代码的其余部分还可以。如果你有

sendMessage()
未定义消息

-
sendMessage()
正文或此处未提供的JavaScript代码的其他部分中存在错误。例如,错误可能出现在
标记包含的js文件中

更新

您的脚本位于
主体
元素下方。这是不正确的。 您可能在上面的
sendMessage()
行中有错误,例如:
“document.location.pathname++”“chatroomServerEndpoint/”${roomName})


尝试注释所有这些行。

onsubmit
应与
form element
一起使用,而不是与
input element
一起使用(并且应关闭其他答案中提到的输入标记)


提供头衔
函数sendMessage(){
//执行代码。。
警惕(“你好”);
返回false;
//根据您的条件/要求返回false或true
}



您在
标记上缺少一个结束括号,它应该是:
是的,我刚刚意识到我忘记关闭括号,但即使如此,它仍然不起作用。我仍然会遇到相同的错误。当我在sendMessage()中放入一些基本内容时与警报一样,它仍然不起作用。@MinhHoangVO浏览器控制台中还有其他错误吗?请使用代码的最后一个变体更新您的问题(使用
onclick
)。
<html>
    <head>
        <title>TODO supply a title</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    </head>
    <body>
        <textarea id="messagesTextArea" readonly="readonly" rows="10" cols="45"></textarea> <br>
        <input type="text" id="messagesText" size="50" /> <br>
        <input type="button" value="Send" onclick="sendMessage()"/>

    </body>

        <script type="text/javascript">
            var websocket = new WebSocket("ws://"+document.location.host+"+"document.location.pathname+"+""chatroomServerEndpoint/"${roomName});
            websocket.onmessage = function processMessage(message){
                var jsonData = JSON.parse(message.data);
                if(jsonData.message !== null) messagesTextArea.value += jsonData.message + "\n";
            }; 

            function sendMessage(){
                alert("hello");
            }
        </script>   
</html>
@ServerEndpoint(value="/chatroomServerEndpoint/{chatroom}", configurator=ChatroomServerConfigurator.class)