0

I have a jsp page with a drop-down control mid-way down the page. When the user changes the selection of the drop-down the page submits to a servlet and forwards back to the same page with details about the user selection to be displayed. I'd like the page to automatically navigate back to the drop-down instead of the user having to scroll back to it on their own.

In the servlet after the code that retrieves the appropriate data, I have:

String forwardTo = "/WEB-INF/jsp/task-create.jsp#taskTypeSelection"
request.getRequestDispatcher(forwardTo).forward(request, response);

The link in the jsp I want the servlet to go to is placed above the drop-down control and looks like:

<a name="taskTypeSelection" id="taskTypeSelection"></a>
<select class="form-control" id="taskTypeID" name="taskTypeID">
   <option  value="">Select a task type.</option>
   <option...

However, this doesn't work and I get a 404 error when the Dispatcher tries to forward. Is there a way to make this work while using my current setup with the RequestDispatcher? I figure this would be less of a problem if I used HttpServletResponse.sendRedirect, but I am using RequestDispatcher for added security and because I have put all my jsps in the WEB-INF folder and therefore are inaccessible for sendRedirect (at least that is my understanding. A lot of this is new to me).

Thanks!

arcademonkey
  • 109
  • 8
  • 1
    Why don't you use scrolling function of pure Javascript or JQuery? I think it can be applied easier than RequestDispatcher you mentioned above. – Quan Nguyen Feb 18 '16 at 15:31
  • Because I need to have the scroll/navigate action triggered from the servlet. Is it possible to do that? I didn't think it was. – arcademonkey Feb 18 '16 at 15:57
  • Anyway, I think you have to use client code to do that. Maybe this demo will help you: http://stackoverflow.com/questions/6677035/jquery-scroll-to-element – Quan Nguyen Feb 18 '16 at 16:17
  • 1
    All you need is forward to the jsp page to want then in the page load event handler, you add code to scroll to that element. – Quan Nguyen Feb 18 '16 at 16:24
  • Alright, thanks, I was able to get it working with that approach! If you want to post it as an answer I would be happy to accept it. – arcademonkey Feb 18 '16 at 19:25
  • You can vote up by clicking up arrow on the left side of my comment. – Quan Nguyen Feb 19 '16 at 01:53

1 Answers1

0

This is more of a workaround based on Quan Nguyen's help than an answer to the original question.

In the servlet, I just set a request variable, "scrollTo" to the name if the link id that I want the forwarded page to go to. Then, I created a JS script (based off jQuery scroll to element) to look for the "scrollTo" request variable (using JSTL el) that fires on the document's ready() event. If one is found then it scrolls the page to the link in the jsp that was indicated.

Servlet:

request.setAttribute("scrollTo", "taskSelection");

JS Script:

 $(document).ready(function(){
    var scrollTo = "${scrollTo}";

    if(scrollTo !== "" && scrollTo!== null && scrollTo !== undefined){
        $('html, body').animate({
            scrollTop: $("#" + scrollTo).offset().top
        }, 0);
    } 
});
Community
  • 1
  • 1
arcademonkey
  • 109
  • 8