aspose file tools
The moose likes Servlets and the fly likes Lots of Query Strings: Best Approach Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Lots of Query Strings: Best Approach" Watch "Lots of Query Strings: Best Approach" New topic
Author

Lots of Query Strings: Best Approach

Gregg Bolinger
Ranch Hand

Joined: Jul 11, 2001
Posts: 15230

In my application I am working on I have a JSP called manageissues that will display, you guessed it, issues. Depending on what query strings I pass in from other pages will determine the initial view of the issues. It could one or more of the following:

  • my issues
  • everyone's issues
  • 1 of 3 different issue levels
  • today's issues
  • a Date range of issues


  • So I mignt have a link that looks something like:

    manageissues?level=2&view=mine&range=bla

    And that's not even all of them. I have a servlet that gets called that actually gets all the data I need and forwards this data in the request to the manageissues page.

    What I am wondering is, what is the best way to handle all the different possible combinations of query strings I might have? Should I use more than one servlet? Should I be doing this a totally different way alltogether? Any hints, tips, suggestions would be helpful.

    Thanks.
    Mark Spritzler
    ranger
    Sheriff

    Joined: Feb 05, 2001
    Posts: 17234
        
        1

    One word, Struts.

    That way you can have forwardmappings to forward them. or gettign the "parameters" from the form.

    I know that Struts can help, my brain is just not in Struts mode right now. I am currently writing JUnit tests, so my mind is elsewhere.

    Mark


    Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
    How to Ask Questions the Smart Way FAQ
    Gregg Bolinger
    Ranch Hand

    Joined: Jul 11, 2001
    Posts: 15230

    Let's pretend I am not using struts, because I'm not. And I won't.
    friso dejonge
    Ranch Hand

    Joined: Jul 11, 2002
    Posts: 162
    gregg,
    whether You use struts or not, you may be better of using some sort of MVC. Struts is just an implementation of the good old and well appreciated MVC pattern.
    Anyway, the struts classes help you a bit, now you have to write it yourself. What I mean is the following. Your code of what the user wants is in html and is transferred in the request when submitting the html form. (sorry if I make it too easy to understand) Then struts classes can automatically put all the request variables in a bean (a simple pojo, with getters and setters) If you are not using struts then you have to write that yourself.
    After having the variables in a simple java object, your servlet has to do something with the variables. But servlets are not designed to do all the work. You have to create an object that in its constructor receives the javabean, (or a static method might do as well, but that depends on design) and that object creates the querystring for you depending on the variables in the javabean. That is send back to the servlet and presto.
    Hope that answers your question.
    Anyway make sure the servlet does not do too much work itself. Let helper classes do all the hard work.

    regards,
    friso


    swimming certificate (A & B), shoelaces diploma, and some useless java ones.
    Gregg Bolinger
    Ranch Hand

    Joined: Jul 11, 2001
    Posts: 15230

    I think I am being misunderstood. I don't have a form. I am creating links, some static, some dynamic. These links are links to my manageissues servlet and they have query strings attached to them. So when my servlet gets called, I do some work based on those query strings and then I forward to my JSP.

    I am wondering, with the amount of query strings and the many possible combinations I have, what is the best approach to this.
    [ September 30, 2004: Message edited by: Gregg Bolinger ]
    friso dejonge
    Ranch Hand

    Joined: Jul 11, 2002
    Posts: 162
    hi gregg,
    another thing you could do on your jsp is look at custom tags. Depending on which variables are passed in show the correct view. This takes out all the clutter in the jsp page.
    cheers
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 56201
        
      13

    True or false:

    1)The JSP page will list a collection of issues.
    2)The request params are used to filter which issues get shown.

    If both answers are true, your servlet shoud handle all the filtering and just deliver a list of issues to the JSP which could care less how the list of issues was obtained.

    Especially if using JSTL and EL, your JSP page will factor down into a small, concise page.


    [Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
    Gregg Bolinger
    Ranch Hand

    Joined: Jul 11, 2001
    Posts: 15230

    Originally posted by Bear Bibeault:
    True or false:

    1)The JSP page will list a collection of issues.
    2)The request params are used to filter which issues get shown.

    If both answers are true, your servlet shoud handle all the filtering and just deliver a list of issues to the JSP which could care less how the list of issues was obtained.

    Especially if using JSTL and EL, your JSP page will factor down into a small, concise page.


    True and true and that is my goal. What I guess I am really asking and not conveying property is what is the best way to handle all the different query string options in the servlet? A bunch of if/else's? Should I specify different servlet for different filters?
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 56201
        
      13

    When I've done something similar, I abstracted the query into a series of model classes (all implementing a common interface) that the single servlet would use based upon the settings of the filters. Bascially, I pushed the details as far "down" into the hiearchy as I could so that the JSP and the servlet did as little work as possible.
    Gregg Bolinger
    Ranch Hand

    Joined: Jul 11, 2001
    Posts: 15230

    Originally posted by Bear Bibeault:
    When I've done something similar, I abstracted the query into a series of model classes (all implementing a common interface) that the single servlet would use based upon the settings of the filters. Bascially, I pushed the details as far "down" into the hiearchy as I could so that the JSP and the servlet did as little work as possible.


    That's a solid approach. I'll play around with that and see where it takes me. Thanks.
    Adeel Ansari
    Ranch Hand

    Joined: Aug 15, 2004
    Posts: 2874
    Sounds like command pattern.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Lots of Query Strings: Best Approach
     
    Similar Threads
    Data Entry Flow Servlet
    Is my MVC design under RMI reasonable?
    Balancing good design versus performance & memory
    [B&S] Construction of a data row Object
    JSP ViewHelper DesignPattern