Ajax không điều hướng sang Servlet

Chào mọi người!
Mình mới nghiên cứu về Ajax JQuery. Khi mình dùng $.get(url) để đi tới servlet thì không được. Mình nghĩ là sai URL nhưng search gg mãi không ra, mong các bạn giúp đỡ.
Ứng dụng của mình chỉ đơn giản như sau: “Từ Page1, khi người dùng bấm nút thì chuyển tới controller nhờ Ajax, và cuối cùng đi tới Page2”

Cấu trúc project của mình như sau:

Page1.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Page 1</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $("#GoPage2").click(function(){
                    $.get("/GoToPage2");
                });
            });
        </script>
    </head>
    <body>
        <input type="button" value="Go to Page2" id="GoPage2">
    </body>
</html>

Page2.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Page 2</title>
    </head>
    <body>
        Hi! I'm Page 2
    </body>
</html>

controller/GoToPage2.java

package controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GoToPage2 extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.getRequestDispatcher("/Page2.jsp").forward(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>GoToPage2</servlet-name>
        <servlet-class>controller.GoToPage2</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>GoToPage2</servlet-name>
        <url-pattern>/GoToPage2</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

“Ajax không điều hướng sang Servlet”
bạn vào phần network của browser xem có chạy k nhé :smiley:
Đơn giản hơn là dùng location.href = “/Page2.jsp”;

à, mình đang tập dùng Ajax ấy c
nếu chạy bằng URL của Browser thì vẫn đc
đang học online mà thử 1 câu lệnh đơn giản cũng ko đc, nản quá :3

Cái này là jQuery chứ nhỉ

Chuyển tới servlet mà bạn, t gửi cả code client và server kìa :3

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?