0

I am working on a very small project and my design would be using technologies such as JSP, servlets, and POJOs.

I'm considering a workflow where a JSP page will receive input data, then submit it to a servlet. The servlet would then have a lot of helper POJOs in it that would do all the real work.

One thing I've noticed with this approach very early on is that it grew into a huge number of if-then-else statements.

Is there a better way to accomplish what I'm trying to do without having to resort to a framework?

3
  • Hi simon, I've tried to clean up your question and match your title to what you're asking, but please feel free to improve the title if the spirit of what you're looking for has been lost.
    – user8
    Dec 19, 2011 at 9:42
  • Can you give and example of the if then else statements? It does sound like you're trying to write this in a MVC pattern, which is a good thing, but there are some frameworks that have already codified this pattern for you. Dec 19, 2011 at 10:52
  • @MartijnVerburg hi, for example I got 4 jsp pages named as Create, Retrieve, Update and Delete. When one form/page submits, my doPost method would look like if (action.equals("create")) { // stuff } else if (action.equals("retrieve")) { // stuff } ... and the if then else continues to satisfy all the form submits
    – chip
    Dec 19, 2011 at 12:14

2 Answers 2

1

JSP pages are served by a servlet, so your sentence "a JSP page will receive input data, then submit it to a servlet" sounds to me like a strange approach (perhaps I didn't understand it well). Normally, you first see your data in the "servlet", then render a document (i.e. JSP) that is sent to client browser.

If you don't want to use a web framework, you'll end up writing lots of common stuff yourself (parsing parameters, converting them from strings to the needed types, validating them, make the results available to your JSP...), and yes, that needs a lot of if/then/else.

In very general terms, what you want to do is to use a single servlet that services your requests:

 public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {

    // Parse the request and select an aproppriate POJO that services this action
    // Your service POJOS should have a common interface (i.e. validate(),
    // service()...)

    // Call a binding method that reads parameters from GET/POST and maps them
    // to properties of your POJO

    // Call the POJO "validate()" so you know that input is ok

    // Call the POJO "service()" so it runs the appropriate process:
    // This should return all needed data and the name of the "JSP" you will use

    // Generate and send JSP to client

  }

But actually, you need much more flexibility: you need to be able to use redirects from your action, you'll need to handle exceptions in a reasonable way, you'd need to consider content encoding... in short, I strongly suggest you to reconsider using a framework (Struts 2, Spring MVC, Tapestry.... there are so many), or alternatively post the code that services one of your requests so people can suggest improvements.

I hope it helps.

5
  • hi, you explained it well. It is just that we are in a tight situation (the deadline is the DEADLINE) and there is not enough time to learn a new framework but you really refreshed my mind why a framework is to be used. I think I might reconsider this thing with my boss. thanks, appreciate the help :D
    – chip
    Dec 19, 2011 at 12:23
  • 1
    just a suggestion, but if you are in a rush for something simple, setting up Spring MVC is a breeze and will do so much for you...
    – jjmontes
    Dec 19, 2011 at 12:34
  • nice.. will surely keep that suggestion in mind for work tomorrow :D thanks again
    – chip
    Dec 19, 2011 at 15:35
  • 1
    Don't forget that the common code you've already written and are familiar with can be pulled out into a commons library. Instant framework.
    – user43349
    Dec 19, 2011 at 18:49
  • yes, I think it is time to put my name all over the code I have written. :D Thanks for the suggestion
    – chip
    Dec 20, 2011 at 0:19
1

I would try to shape the application developing a DSL (domain specific language).

That would leverage the protocol logic, decupling from the actual input, and via reflection should allows execution (instance of POJO) of the required duties. The protocol grammar could be developed and tested stand alone and incrementally, with the help of a parser generator.

Of course, if you're akin to 'syntax love'.

The answer from @jjmontes is of course somewhat more practical.

2
  • hey, thanks for the answer. I believe I have read some documents about this programming principle but with the use of groovy only. This is kinda out of the question but can you give me some links that can make me understand more about this DSL? thanks :D
    – chip
    Dec 19, 2011 at 12:29
  • 1
    Hi Simon. I didn't realized your deadline was so strict. My suggestion it's a bit too much generic. Anyway, here [1] a link (rather generic, doesn't show code, just jargon). This other [2] it's more practical, doesn't require any parser generator, just methodology. [1] javaworld.com/javaworld/jw-06-2008/jw-06-dsls-in-java-1.html [2]weblogs.java.net/blog/carcassi/archive/2010/02/04/…
    – CapelliC
    Dec 19, 2011 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.