0
我是websocket的新手,所以我在我的項目中遇到了一些問題。我正在創建一個聊天室。我有一個登錄頁面,將由UsernameServlet處理,然後它將鏈接到chatpage.html,它將打開一個websocket(聊天室服務器端點)。與Java的Web套接字映射
這裏是我的loginpage.html
<body>
<form action="localhost:8080/chatRoom/UserNameServlet" name="submitForm" onchange="handleNewRoom()">
<mark>Please select a room:</mark>
<select id="roomSelect" name="roomSelect">
<option value="room1">Room 1</option>
<option value="room2">Room 2</option>
</select>
<mark>Please Enter username</mark>
<input type="text" name="username" size="20"/>
<input type="submit" value="Enter" />
</form>
<script>
window.onload = function(){document.submitForm.action=submitAction();}
function submitAction(){
return document.location.pathname;
}
</script>
</body>
這裏是我的UserNameServlet @WebServlet( 「/ UserNameServlet」)
public class UserNameServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
String username = request.getParameter("username");
HttpSession session = request.getSession(true);
String roomName = request.getParameter("roomSelect");
session.setAttribute("username", username);
PrintWriter out = response.getWriter();
if(username == null) response.sendRedirect("loginpage.html");
else response.sendRedirect("chatPage.html");
}
這裏是我的chatPage.html
<script>
var websocket = new WebSocket("ws://""+"+document.location.host+"+"document.location.pathname+"+""chatroomServerEndpoint/"+roomName+"");
</script>
我ChatroomServerEndpoint.java
@ServerEndpoint(value="/chatroomServerEndpoint/{chatroom}", configurator=ChatroomServerConfigurator.class)
因此,我的問題是:我的鏈接到websocket服務器是否正確,我如何在chatpage.html中訪問roomName?