mercredi 31 juillet 2019

Javascript file included in HTML file not loaded correctly with RequestDispatcher.forward()

I have a simple gameboard.html file containing a

<script src="gameboard.js"></script>

at the end.

The gameboard.js file loads and runs correctly if I go directly to localhost:8080/gameboard.html.

However, when I try to forward to the HTML file from a servlet (in the interest to keep another URL), the gameboard.js file in developer tools shows the code of gameboard.html and gives a syntax error as it's invalid Javascript. The HTML code runs, but not the Javascript.

Servlet:

@WebServlet(name = "GameServlet", urlPatterns = {"/game/*"}, loadOnStartup = 1)
public class GameServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        request.getRequestDispatcher("/gameboard.html").forward(request, response);
    }
}

Result going directly to localhost:8080/gameboard.html - both HTML and Javascript files loaded correctly: Result going directly to localhost:8080/gameboard.html - both HTML and Javascript files loaded correctly

Result using the servlet forward to gameboard.html - Javascript file ends up with HTML code: Result using the servlet forward to gameboard.html - Javascript file ends up with HTML code




Aucun commentaire:

Enregistrer un commentaire