开发者

How do I alter the URL?

I'm currently working on a big web application for a company and we are about 4 months in, but we have a harmless(but annoying) problem that we have just left because we didn't time to change it.

The way we setup our MVC is leaving us with the Servlet being stacked one after the other endless amounts of times on the URL so if we had a Servlet named "ControllerServlet" and I did something on the website I would get a result such as this the first time.

WebsiteXXXXXXX.com/XXX/ControllerServletXXXX

And the next time I were to do something everything will work fine, but the URL will stack the ControllerServlet Path like this..

WebsiteXXXXXXX.com/XXX/ControllerServlet/ControllerServlet/XXXX

WebsiteXXXXXXX.com/XXX/ControllerServlet/ControllerServlet开发者_高级运维/ControllerServlet/XXXX

and so on....

Although it is working perfectly fine, something is obviously not right.

I imagine this is an easy fix, but could really use somebodies help.

Thanks alot


When a context-relative URL in the form action is used (i.e. an URL without a domain part and without a leading slash /), then it is relative to the last context of the current request URL.

When a page which is requested by http://example.com/webapp/ControllerServlet and contains the following form action:

<form action="ControllerServlet/action">

Then the absolute action URL will be http://example.com/webapp/ControllerServlet/ControllerServlet/action. To fix this, you need to ensure that the form action URL is in a correct manner relative to the request URL. For a page which is requested by http://example.com/webapp/ControllerServlet/action there are several ways, depending on the ways how you can request the same page.

Either

<form action="action">

..which is relative to the last context in the request URL, or

<form action="/webapp/ControllerServlet/action">

..which is relative to the domain root, or

<form action="../ControllerServlet/action">

..which is relative to the context before the last context in the URL (it will effectively remove /ControllerServlet from the current request URL and append it again -a bit pointless indeed, but useful if you have more servlets in the context), or

<form action="${pageContext.request.contextPath}/ControllerServlet/action">

..which is relative to the domain root (useful when you don't want to hardcode the webapp's context path), or

<head>
    <base href="${pageContext.request.contextPath}">
    ...
</head>
<body>
    <form action="ControllerServlet/action">
    ...
</body>

..which would apply to all links and forms.

All of above will point to http://example.com/webapp/ControllerServlet/action.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜