JSP/Servlet - Request Dispatcher

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • webster5u
    New Member
    • Jan 2008
    • 28

    JSP/Servlet - Request Dispatcher

    I'm design MVC structure for current developing system.
    Initially, the jsp will post data to servlet class. After processing, servlet will use the requestDispatch er to return processed data back to jsp.

    RequestDispatch er dispatcher = request.getRequ estDispatcher("/webapps/hrd/test.jsp");
    dispatcher.forw ard(request,res ponse);

    Now, the process the successful but i found the url display on address bar still remain as servlet url. It cannot change to the jsp url. who's know how to change it, if we don't use session?

    Cause, i though MVC suppose separate the java code from the jsp, let jsp for display purpose and servlet for business process logic. Is the servlet can send data to jsp webpage?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by webster5u
    Now, the process the successful but i found the url display on address bar still remain as servlet url. It cannot change to the jsp url. who's know how to change it, if we don't use session?

    Cause, i though MVC suppose separate the java code from the jsp, let jsp for display purpose and servlet for business process logic. Is the servlet can send data to jsp webpage?
    The Dispatcher.forw ard( ... ) never reaches the client; as far as the client is concerned it receives a response from the URL it sent to the server. That's why the URL display on your client doesn't change.

    Yep, keep the functionality separated: JSPs for display purposes and Servlets (and beans called from it) for the control logic and business logic. The Servlet can send data to the JSP through the request/response objects.

    kind regards,

    Jos

    Comment

    • webster5u
      New Member
      • Jan 2008
      • 28

      #3
      Hi, thank for you rapidly response.

      In my case, the servlet use dispatcher.forw ard method to send REQUEST object to jsp and receive HTML output from jsp. It is didn't move to jsp but just receive and display the HTML code from jsp. Is it what's you mean?

      In this case, how you design the servlet send back data to jsp. What's method are you applying?

      Comment

      • webster5u
        New Member
        • Jan 2008
        • 28

        #4
        OK, finally i still need store data into session in order to transfer from servlet to jsp.
        Who has experience the javabean before. Can it list down a list of table instead of single object?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by webster5u
          OK, finally i still need store data into session in order to transfer from servlet to jsp.
          Who has experience the javabean before. Can it list down a list of table instead of single object?
          You can put any Java object in the request/response/session object you want. That includes Maps, Lists, Tables etc.

          kind regards,

          Jos

          Comment

          • devgupta01
            New Member
            • Jan 2008
            • 20

            #6
            change your code like that...
            RequestDispatch er dispatcher = request.getRequ estDispatcher("/test.jsp");
            dispatcher.forw ard(request,res ponse);

            I think it will work...

            Comment

            • webster5u
              New Member
              • Jan 2008
              • 28

              #7
              thk for reply. The code is not problem to execute.
              I jz want to know how to change the url address after dispatcher has been execute.
              If you have any idea about it?

              Comment

              • dmjpro
                Top Contributor
                • Jan 2007
                • 2476

                #8
                Originally posted by webster5u
                thk for reply. The code is not problem to execute.
                I jz want to know how to change the url address after dispatcher has been execute.
                If you have any idea about it?
                What you want that's not possible using Dispatcher as Josh mentioned, request does not reach the client after Dispatcher.forw ard. If you want to show the URL then use response.sendRe direct but you have to start MVC from the beginning.
                Why do you want to show the URL? As the developers try to hide the exact URI from the user... ;)

                Comment

                • jayesh
                  New Member
                  • Nov 2011
                  • 1

                  #9
                  Hi webster5u,

                  the request dispatcher is used to hide the url for that page which you dispatch and thats is a secure way, thats no one can finr your page link,

                  but if you want to show the url in address bar then you can use response.sendRi derct("xyz.jsp" ) this method.

                  and for storing data, i dont know javabeans but you can store data in session using session.setAttr ibute("paaramet er","value") this method.

                  let me know if you have any doubt.

                  Comment

                  Working...