0

I built a Java web application with multiple servlets and jsps. I first had servlets only for shopping cart options such as adding products and deleting products etc. But then noticing lot of Java code in jsps I made a servlet for every Dynamic jsp which solved part of my problem. I want to know whether my design is good or bad. Or is their other way to reduce Java code from the jsp?

Chan
  • 2,601
  • 6
  • 28
  • 45

4 Answers4

1

You could consider a MVC framework such as Spring MVC. This will allow you to consolidate your java code in Controller classes and use JSP's for what they are generally good for which is serving as a template engine. Plus with Spring you will only need to configure a single servlet for the dispatcher servlet.

Leveraging an MVC has additional benefits because you are working with a well known abstraction. A lot of boiler plate has been written for you. You can map your model data to simple POJOs that can be rendered cleanly in the JSPs through the Spring Tag Library. The Spring Bean Container will provide boiler plate Dependency Injection code that will allow you to write more modular,testable code faster.

The net out is working with raw JSPs and Servlets is a thing of the past. A ton of convenience code has been built on top of the spec and its implementations. Any implementation that you do will end up repeating a lot of this work(without the benefit of thousands of users testing it for you).

nsfyn55
  • 14,875
  • 8
  • 50
  • 77
  • @downvoter Care to defend your downvote or do you just have a nervous twitch? – nsfyn55 Jun 24 '11 at 13:19
  • I did not vote on your answer, just passing by. Could you explain in your words why Spring is so cool? Topic starter already has MVC if used servlets as controllers. Would it be faster to write all xml's (ok in Spring v.3 not too much), annotations and debug it? – Sergei Chicherin Jun 24 '11 at 13:33
  • @chro I happen to be putting spring out here in this instance because its what I am familiar with(Please feel free to find/replace with your framework of choice since you obviously feel strongly about it). My point is that the OP is facing a problem that framework's like Spring is specifically designed to address. Sure OP has "MVC" but its home-grown and obviously sub-optimal if his JSP's are littered with java code. To answer your second question, for simple implementations it seems like unnecessary overhead, but how often do simple implementations stay simple? – nsfyn55 Jun 24 '11 at 15:12
0

You have to use tag libraries. Refer to this question How to avoid Java code in JSP files? to get a very good idea.

Community
  • 1
  • 1
Varun
  • 1,014
  • 8
  • 23
0

In general, you should implement servlet and jsp Using MVC model. keep the minimal java code in jsp pages and move most of them to controller and model part.

bingjie2680
  • 7,643
  • 8
  • 45
  • 72
0

Seems like your problem is complex enough to use some MVC framework such as spring. The trick is how you go from your working solution to the new one. I would suggest writing some selenium tests to make sure that you dont break anything

Note: I'm assuming that you are dealing with production code here.

Jiraiya
  • 336
  • 2
  • 8