Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package furkan.app.tictactoewebsocket; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.math.NumberUtils; /** * * @author SVLLA */ @WebServlet(name = "ticTacToeServlet", urlPatterns = "/tictactoe") public class TicTacToeServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { request.setAttribute("pendingGames", TicTacToeGame.getPendingGames()); this.view("list", request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String action = request.getParameter("action"); if (action.equalsIgnoreCase("join")) { String gameIdString = request.getParameter("gameId"); String username = request.getParameter("username"); if (username == null || gameIdString == null || !NumberUtils.isDigits(gameIdString)) { this.list(request, response); } else { request.setAttribute("action", "join"); request.setAttribute("username", username); request.setAttribute("gameId", Long.parseLong(gameIdString)); this.view("game", request, response); } } else if (action.equalsIgnoreCase("start")) { String username = request.getParameter("username"); if (username == null) this.list(request, response); else { request.setAttribute("action", "start"); request.setAttribute("username", username); request.setAttribute("gameId", TicTacToeGame.queueGames(username)); this.view("game", request, response); } } else this.list(request, response); } private void view(String view, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("/WEB-INF/jsp/view/" + view + ".jsp").forward(request, response); } private void list(HttpServletRequest request, HttpServletResponse response) throws IOException { response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + "/tictactoe")); } }