Quantcast
Channel: Active questions tagged servlets - Stack Overflow
Viewing all articles
Browse latest Browse all 675

JSP redirection trough a servlet is not functioning as expected [duplicate]

$
0
0

Hey so i am running a program that when a cell on a board(on a jsp) is clicked, a servlet is launched, and that servlet can redirect to 1 of 2 jsp, my problem is that this redirection seems to not be working

I have 2 jsp jogo.jsp and jogoAEspera.jsp, i launch at jogo.jsp first, and after clicking a cell, i want to redirect to the other jsp if a play is valid (I am making a board game, and basically when 1 person plays, i want that person to be redirected to a jsp that is waiting for the other guy to play)

function makeMove(row, col) {            const xhr = new XMLHttpRequest();            xhr.open("POST", "JogoServlet", true);            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");            xhr.onreadystatechange = function() {                if (xhr.readyState === 4) {                    if (xhr.status === 200) {                        location.reload();                    } else if (xhr.status === 400) {                        logInvalidMoveMessage(); // Log if move is invalid                    }                }            };            xhr.send("row=" + row +"&col=" + col);        }<div class="board"><%                 String[][] boardState = (String[][]) session.getAttribute("boardState");                Boolean minhavez = (Boolean) session.getAttribute("minhavez");                boolean myTurn = minhavez != null && minhavez;                if (boardState != null) {                    for (int row = 0; row < 8; row++) {                        for (int col = 0; col < 8; col++) {                            String cellContent = boardState[row][col];                            out.print("<div id='cell-" + row +"-" + col +"' class='cell' onclick='if(" + myTurn +") makeMove(" + row +", " + col +");'>");                            if ("B".equals(cellContent)) {                                out.print("<div class='black'></div>");                            } else if ("W".equals(cellContent)) {                                out.print("<div class='white'></div>");                            }                            out.print("</div>");                        }                    }                }            %></div>

Above is my jsp

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub        HttpSession session = request.getSession();        session.setAttribute("initialized", true);        System.out.println("PLAYER " + session.getAttribute("username"));        PrintWriter pw = (PrintWriter) session.getAttribute("writer");        BufferedReader br = (BufferedReader) session.getAttribute("reader");        String rowParam = request.getParameter("row");        String colParam = request.getParameter("col");        if(rowParam != null) {            pw.println(rowParam +"/" + colParam);        }        System.out.println("azeitona");        String tabuleiro =atualizarGUI(br);        String vez = tabuleiro.split("\n")[8];        int[][] tabuleiro1 = atualizar(br,tabuleiro);        if(tabuleiro1[0][0]==3) {            session.setAttribute("jogadainvalida",true);            response.sendRedirect("jogo.jsp");            return;        }        System.out.println((String) session.getAttribute("username") +" pimento ");        if(vez.equals((String) session.getAttribute("username"))) {            System.out.println("zeggi");            session.setAttribute("minhavez",true);        }        else {            session.setAttribute("minhavez",false);        }        String [][] truetabuleiro=convertToIntToString(tabuleiro1);        if(session.getAttribute("minhavez").equals(false) | session.getAttribute("initialized").equals(false)) {            session.setAttribute("initialized",true);        }        session.setAttribute("boardState", truetabuleiro);       // session.setAttribute("initialized", true);        session.setAttribute("initialized",false);        if(vez.equals((String) session.getAttribute("username"))) {            response.sendRedirect("jogo.jsp");        }        else {            response.sendRedirect("jogoAEspera.jsp");        }    }

this is the code of the servlet that is launched on a cell is clicked, my problem is that i have checked in debug, and after clicking a cell, i can tell that the line

response.sendRedirect("jogoAEspera.jsp");

is being executed, but my jsp does not change, it just seems like it just reloads the session atributes but stays in the same jsp

Thank you and sorry if the post is incoherent.


Viewing all articles
Browse latest Browse all 675

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>