6

So I decided to "update" my website written with JSP to display a static URL instead of the common dynamic URL.

So far, I wrote a servlet to redirect a static URL to the corresponding dynamic URL. The data is stored in a database table.

This is the code:

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    StringBuilder redirectTo = new StringBuilder();
    redirectTo.append(getBaseURL());
    redirectTo.append(getBaseURI());
    redirectTo.append("/index.jsp");

    String url = request.getRequestURI();

    if (url.contains(".jsp")) return;

    try {
        KeyPageId_Lang k = SiteMap.getRedirectFromCanonicalURL(url);
        if (k!=null) {
            redirectTo.append("?id=" + k.getkPageId());
            redirectTo.append("&lang=" + k.getkLanguage());
        }
        else {
            log.error("CanonucalURLRedirect: No translation found for " + url);
        }
    }
    catch (Exception e) {
        log.error("CanonucalURLRedirect ERROR: " + e.getMessage());
    }

    response.sendRedirect(redirectTo.toString());

So, if I enter: http://localhost/jsp/en/home it successfully redirects to http://localhost/jsp/index.jsp?id=1&lang=en

If the static URL is not found, it will redirect to the default /index.jsp

Also, on index.jsp, I have the following line:

<link rel="canonical" href="<%=canonicalURL%>" />

For search engines to index the friendly static URLs.

Now, I've been searching a lot and I still can't manage a way to replace the dynamic URL with the static URL on the browser's navigation bar to display localhost/jsp/en/home instead of localhost/index.jsp?id=1&lang=en

Any idea?

Thank you!

cmmpsantos
  • 95
  • 9

0 Answers0