【发布时间】:2016-02-28 20:52:41
【问题描述】:
我是 websocket 的新手,所以我的项目中有一些问题。我正在创建一个聊天室。我有一个由 UsernameServlet 处理的登录页面,然后它将链接到 chatpage.html,它将打开一个 websocket(聊天室服务器端点)。
这是我的 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?
【问题讨论】:
标签: javascript java websocket mapping chatroom