Panos Koukios
Panos Koukios

Reputation: 21

How to forward a request from a servlet to a specific tab of a JSP page?

If I use the following url in the getRequestDispatcher method I get the below error from Tomcat

404 The requested resource is not available

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/nodes/node_configuration.jsp#tabs-3");

This url if hit directly on the address bar would normally take me to the third tab of a jQuery-built tab structure (used the source code from https://jqueryui.com/tabs/). If I remove the #tabs-3 it will not throw an error but it will take me to the first tab always.

I tried to workaround this problem by modifying the jsp by passing the tab as a url parameter. So, now the url would be nodes/node_configuration.jsp?selectedTab=#tabs-3. This works fine in the address bar, but it is neglected by the dispatcher.

Upvotes: 1

Views: 1401

Answers (3)

Panos Koukios
Panos Koukios

Reputation: 21

Since I could not do it the easy way I did it the hard way. In the servlet I pass the selected tab as a request object attribute:

request.setAttribute("selectedTab", "3");

Here is the script in the jsp:

<script>
    $(function() {
        var param = document.getElementById("selectedTabInput").value;
        if (param != 0) {
            $('#tabs').tabs({
                active : param
            });
        } else {
            $('#tabs').tabs();
        }
    });
</script>

I am taking the selected tab from a hidden input value inside the body of the jsp:

<input type="hidden" id="selectedTabInput" value="${requestScope.selectedTab}">

Upvotes: 1

gsl
gsl

Reputation: 676

getRequestDispatcher() expects a servlet path (i.e. the path part in a URL without the app's context path, and without all things starting with ? and #). You cannot pass a whole URL into it.

You could pass the hash thing into the JSP using a request attribute, i.e. request.setAttribute( "hashTarget", "tabs-3"); Then you have to read this in the JSP and start a JavaScript to act on it. (Like proposed in the previuos answer.)

Upvotes: 0

Jozef Chocholacek
Jozef Chocholacek

Reputation: 2924

You have to realize, that the tab is a front-end (browser side) concern. So you have to pass the tab name (or id) from the back-end to the front-end, e.g. as a script variable, and then switch to the proper tab using a script (with jQueryUI Tabs using e.g. the active option).

Upvotes: 0

Related Questions