简体   繁体   中英

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.

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}">

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, eg as a script variable, and then switch to the proper tab using a script (with jQueryUI Tabs using eg the active option).

getRequestDispatcher() expects a servlet path (ie 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, ie 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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM