admin管理员组

文章数量:1287525

I have some JavaScript that is making an Ajax call to a relative url (using jQuery).

var servletUrl = "someservlet";

$.ajax({
    type: "POST",
    url: servletUrl,
    success: function(response) {
        // ...
    }
});

Where "someservlet" is:

@WebServlet("/someservlet")
public class SomeServlet extends HttpServlet

I use this same script in multiple pages. When used from a page that is in the servlet context root, then the relative url resolves relative to the servlet context root, which is correct. When used from a page that is in a subfolder the URL resolves relative to the subfolder, which will return 404 error.

I would like to be able to reuse this JavaScript without having to modify it depending on the type of page that it is used within. Ideally, I need the equivalent of the JSTL's <c:url> tag. Is there anything in JavaScript that allows me to create URLs relative to the servlet context root?

I have some JavaScript that is making an Ajax call to a relative url (using jQuery).

var servletUrl = "someservlet";

$.ajax({
    type: "POST",
    url: servletUrl,
    success: function(response) {
        // ...
    }
});

Where "someservlet" is:

@WebServlet("/someservlet")
public class SomeServlet extends HttpServlet

I use this same script in multiple pages. When used from a page that is in the servlet context root, then the relative url resolves relative to the servlet context root, which is correct. When used from a page that is in a subfolder the URL resolves relative to the subfolder, which will return 404 error.

I would like to be able to reuse this JavaScript without having to modify it depending on the type of page that it is used within. Ideally, I need the equivalent of the JSTL's <c:url> tag. Is there anything in JavaScript that allows me to create URLs relative to the servlet context root?

Share Improve this question edited Jun 15, 2020 at 12:57 BalusC 1.1m376 gold badges3.6k silver badges3.6k bronze badges asked Oct 6, 2011 at 22:27 KevinSKevinS 7,8734 gold badges41 silver badges62 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

Several options:

  1. Set a HTML <base> element with that value (note: has its own advantages and disadvantages)

    <head>
        <base href="${pageContext.request.contextPath}/" />
        ...
    </head>
    

    and then in JS:

    var contextPath = $("base").attr("href");
    var servletUrl = contextPath + "someservlet";
    // ...
    

  2. Or set a data attribute somewhere

    <html data-contextPath="${pageContext.request.contextPath}/">
        ...
    </html>
    

    and then in JS:

    var contextPath = $("html").data("contextPath");
    var servletUrl = contextPath + "someservlet";
    // ...
    

  3. Or set a global JS variable with that value (poor practice):

    <head>
        ...
        <script>var contextPath = "${pageContext.request.contextPath}/";</script>
    </head>
    

    and then in JS:

    var servletUrl = contextPath + "someservlet";
    // ...
    

本文标签: jqueryHow to perform equivalent of JSTL39s curl in JavaScriptStack Overflow