File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes JSP Design - Basics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "JSP Design - Basics" Watch "JSP Design - Basics" New topic
Author

JSP Design - Basics

Sree Shan
Greenhorn

Joined: Oct 12, 2000
Posts: 8
Hey, got some basic doubt on JSP... Thought I could explain my doubt with this big example -- pardon the size.
Say I have a series of 4 screens for a user to get registered & start an order. First screen gets the user-name, email, password. Second gets his zip-code. If delivery is being available to the entered zip-code, pick up the corresponding State code and display it in the next page -- this third page would get the Shipping & Billing address and the items he needs. Fourth page would display the summary and get the confirmation.
The above screens need to be developed using JSP. Each screen would have a NEXT button and would also save the entered data into a bean. The last confirmation screen would update the database -- other screens don't access any database (except the select State code in screen 3, based on zipcode entered in Screen 2).
My Question is Totally, how many JSPs would the above case require, for these 4 screens? Please provide some explanation along (it might look like a dumb question - please bear with me).
Thanks in Advance.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
You would get:
(1) HTML page for the login dialog calling a JSP page (2) which takes the username/password and puts them into a bean which is stored in the session. It then displays the option to select a ZIP code (you might consider storing this ZIP code in the database and preselecting it when the user re-enters).
When the user has selected a ZIP, he enters the (3)rd JSP, which displays the shipping available to that ZIPcode and allows entering (changing if information can be pre-entered by the JSP) the shipping/billing info. After clicking OK, he goes to an HTML page generated by a servlet (4) where he can confirm his order. If he confirms he goes to the last (5) JSP where he gets his order confirmation number and a thank you message.
In all you get:
1 static HTML page
1 Servlet
3 JSPs
1 Javabean to store the transaction information
some helper classes maybe

42
Sree Shan
Greenhorn

Joined: Oct 12, 2000
Posts: 8
Thanks... It does explain how the process is all designed
Thanks again
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
The following raises a flag with me :
"an HTML page generated by a servlet "
I believe that servlets should NOT generate any HTML whatsoever.
I believe that servlets should restrict themselves to set up the (non-visual) JavaBean (there should be only ONE useBean tag if possible) from which the JSP will take everything it needs to form its dynamic content.
Today I finalized rewriting one JSP/Servlet combination that a contractor wrote for us (and was told to leave after only 3 weeks).
I had the opportunity to use the experience of ½ year (probably more than the expensive contractor had!?)
My philosophy: ONE BEAN PER JSP. Of course that means more work for the preceding servlet. But the separation of presentation (JSP) from logic (bean and/or servlet) should be maintained.
These are not my original ideas of course. They are Sun's recommended practices.
It is so easy to have servlets generate HTML on the fly, just like CGIs do (did!), that it is very tempting to do it.
The use of servlets do away with the bronze-era CGIs. CGIs where good when they were first used. Hats off to the CGI programmers of yore. Really.
The combination of Servlets and JSPs do away with all that; we progress into the 21st Century (which begins next Jan 1, BTW).


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
I still think JSP's are a hack (and if it wasn't for MS and ASP's we wouldn't even have them). They are difficult to debug and easily corrupted by the HTML crew doing design. HTML templates with servlets are 100X easier to work with. The HTML design group can work with the HTML without having to worry about JSP commands and the Java development group can develop their servlets without worrying about HTML. It gives true separation of responsibility.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
That is exactly what I said
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
And I forgot, the JSP/Servlet combo if far superior to Microsoft's so-called Visual Basic and INactive Server Pages. These are so inactive that they are compiled over and over for every request.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Thomas and Tony,
Well. If I understood both of you correctly, you both agree we have to couple and decouple the 'presentation' and other java work easily and give the 'Page design group' less work part. But you both slightly differ how we should 'implement' it.
Tony,
I also followed and still follow the 'exact' model2 design pattern which you mentioned. Servlet as Controller, jsp as View and JavaBeans as MOdel. Which means NO HTML WHATSOEVER in servlet code. The servlet coder NEED NOT KNOW about html and we can elminate the so many 'out.println("all html code")' lines in sevlet.
How I design is when a servlet is called,
1. it interprets what the request asks for,
2. Calls the needed business logic beans and gets the ResultSet or any other return value
3. Coins a 'viewbean' with set/get methods and puts in session with some name (if needed)
4. Gets the RequestDispatcher and forwards to the active/passive resource
5. The forwared resource then takes control over then onwards.
If it is a jsp, it grabs the bean by <jsp:useBean id='dispDataView' scope='session' class='ams.DispDataView' /> and all the 'GAPS' in jsp , which needed to be FILLED by the DYNAMIC DATA are written as <%= dispDataView.get***() %>
OR if the MIX-UP of 'jsp-expression' and HTML is more, then it is good to write the whole thing using <% .... %> with some java code.
Though it makes our [java/servlet/jsp people ] life easier, it 'STILL NEEDS THE PAGE DESIGNER WORK WITH ' th java stuff in their html code. Isn't? It is not FULLY decoupled right? We may reduce the bean sent to the jsp page to JUST ONE. But if the USE OF THAT bean is scattered all over the page in MANY PLACES, like many textFields, ComboBoxes, Tables , then the html page designer has to SEE lots of jsp scriplet/jsp expression also more and more isn't?
Another difficulty I personally experience is this. What happens if I want to change the OVERALL look and feel of the whole page. Though I know HTML, I don't want to write everything from scracth on my own. I use Web authoring tool NetFusion for generating the html pages. Now I have do the RE-WORK of inserting all the jsp code aginn in the NEW LOOK-AND_FEEL page. Isn't? If the frequency of the PAGE DESIGN CHANGE is more then the rework is also more.
What we do for this sitiations? I think this is where Thomas differs. He is saying some different technique.
Thomas,
If I am correct in this above paragraph, what are the other techs we can use so that the coupling and decoupling is easier. I don't want to re-work on the jsp-code insertion every time the html page is changed to have a new look and feel. What is the tech you explained? I couldn't get it quite well.
I really appreciate if you could explain how yours is 100x better for the seperation. You have told in other post, like creating a Hashtable of objects in the servlet and pass it to a class where the merging is done, something like that. I really appreciate if you could explain somemore. For example if I want to create a dynamic table with 2 diff cols act as a 'href link' to view further details. In other words it is like this.
Strtdate EndDate EmpName PassportNo
--------------------------------------
01/01/99 02/02/99 Maha 1234567
01/01/99 02/02/99 Anna 7654321
Here Maha,Anna and the PassportNos are 'Href links' which when clicked will bring a page of Employees personal info and Passport info in detail. Could you explain is how the merging is done in the class.
Thank you.

regds
maha anna

[This message has been edited by maha anna (edited October 14, 2000).]
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
I did some reasearch on the templates framework. I came to know that
1. WebMacro
2. FreeMarker
3. Jakarta's 'struct'
are the ones which have the framework to separate the presentation completely from Logic and avaliable for free. But I wonder if they are really worth learning and how realiable they are like that.
regds
maha anna
[This message has been edited by maha anna (edited October 15, 2000).]
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
We are using a slight variation of Jakarta's STRUTS framework. I didn't do the design; my boss did so I can't tell to what extent our framework is pure STRUTS.
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
If you want to use a visual editor for JSP pages, you must get one that visually upports JSPs, preferably version 1.0 or 1.1 (NOT 0.9x).
I don't like visual editors so I don't know of any that support JSPs, but you can be sure that if they are not out yet, they soon will be.
The HTML *editor* that I use (HomeSite 4.5) supports JSPs but I don't know if the visual part does; I never use that feature.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Struts is a bit different in that it is a servlet framework rather than a pure templating engine like WebMacro (WM) and FreeMarker (FM). The closest match in the Apache group to WM and FM is "Velocity", which is intended to be "WM-compatible", but is still under development.
How this works is best shown with an example. Here's a very simple template, very similar to your example above (in a WM/Velocity style; I don't rate FreeMarker's syntax as highly, but that's a different issue):

A bean to hold the data (this should be pretty much the same for all types of templating, inluding JSP):

Something to gather and hold a collection of the above beans:

And finally a simple dispatcher servlet:

The above may seem a bit sparse, but the majority of the hard work is done by the template engine (in this case WebMacro) using introspection. The template asks for $record.Start, so the engine tries various combinations, including the appropriate "getStart()" method to determine the result; the template aske for $pc.Passports, so it looks up "pc", and calls its "getPassports()" method, then realises that the result is an iterator, so uses the correct code to step through it.
I hope this goes a little way to showing just how powerful and flexible a decent template system can be. Check out the tutorials on the Webmacro site http://www.webmacro.org/ .

[This message has been edited by Frank Carver (edited October 16, 2000).]


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Thanks Tony and Frank.
Frank,
I went to WebMacro's homepage and slightly browse through the tutorial just to see waht they say. Their moto says 'I want the things get out of my fact which I don't like/want' something like that. Many people including Jason Hunter recommends this. But I also happened to see people's post that the script is not good and the performance is bad when we use WM.
Are you using WM for your production web applications. What's your experience with that. Do you recommend? That means not using jsp at all right? It also means forcing the web autors to learn the script. Isn't?
I highly appreciate your valuable feedback on this. I am in the middle of deciding which way to go. The complete seperation of presentation and others HIGHLY attracts me. Right now I am develpoing a small not so critical web appln. For that I may get away with jsp Because I am doing everything. But I wonder If I have to separate the responsiblities to different groups in future will learning WM now help? Or can I wait for somemore time to see if someother good and easy framework comes out. Is WM it easy to use?
regds
maha anna
[This message has been edited by maha anna (edited October 16, 2000).]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Here is what we do (remembering that we started our application when the first servlet specification was released and JSP wasn't even a thought).
We design the HTML using patterns where ever data should be placed. For example, this would be in the HTML:
Office Name: $$officeName$$
The servlet would create a HashMap containing "officeName" and "Wall Street Corporate Office". We then have a class that reads both the HTML file and the HashMap and sends the result of the merge to the browser. The HTML developer deals with nothing except "$$" pattern which they can use as place holders to design their page exactly how they want. The servlet programmer never has to look at the HTML page. The servlet programmers can do all their coding even before the HTML page is designed. The HTML developers can design their pages even before the servlets have been coded. TRUE SEPARATION!
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I actually use a variety of technologies, as appropriate to the problem at hand.
For some problems I use only software I have written myself - I have a small and simple webserver which can use servlets, or much simpler "handlers" according to my own API. I also have my own XML parser and a template engine. Together these can lead to *very* small and simple web applications, with good separation of presentation, data and logic. It was quite a lot of work to get all these tools built and usable, though, and they are not quite standard enough to be really worth learning. The template engine part is similar to Thomas Paul's approch, but with a few extras which have been required over time.
A template consists of any text, containing tokens marked with '%' characters. When the template is "expanded" by giving it any object which implements the Map interface, tokens are expanded to their values from the map. The simplest token is just a name like %something% - if it is present in the map the value is used, if not an empty string is used. The token syntax has been extended to use java-like query-colon triples, to define what should be substituted if the value is present or absent. Here is a table showing how this works

The last case does the "?" string only if the value in the map associated with the name "name" is "hello". This is very useful for generating <SELECT><OPTION> lists in HTML, as it allows the default selected item to be specified.
This system is more powerful than a simple Map substitution, but a lot less powerful than WebMacro, as it has no looping or real conditionals, and can't handle complex data types - everything must be a string in a Map. I will make this available on the web if anyone is interested.
For more complex systems I use either JSP, WebMacro or (increasingly) XML/XSLT to separate content from appearance. My current development system at home supports all of these technologies, and they all have their place. I'm not currently using WebMacro for any production applications, but one application is fairly near launch which will include some WebMacro. The main issue with WebMacro is that it is not quite at a 1.0 release yet, so some things are still subject to change, but it is definately robust and pretty fast. Each release seems to be faster than the last.
As for WebMacro preventing the use of JSP - certainly not. Use whatever is appropriate. I also feel that the WebMacro syntax is a lot easier for web designers to learn than the over-complex JSP syntax with its imports, beans and confusing pseudo-tags. Putting production Java code in the actual web pages is very dangerous and clumsy, even with tag libraries. WebMacro instructions just look like text in a web page editor.
My suggestion is get a bit of practice with WebMacro so you can be prepared if you are asked to suggest solutions, or to write your own web applications. Learn some JSP to enhance your resum´┐Ż now, and learn XML/XSLT to be prepared for the future.
Did any of that help?
Shankar Ganesh
Ranch Hand

Joined: Oct 01, 2000
Posts: 34
Thomas, I didn't get the idea clearly... Can you please give a code example (may be a small one) to explain how this is carried out..
Thanks,
Shankar.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Thanks Thomas. I was scratching my head a lot for the past few days about your idea. I was having a vague idea of what you said. But it is certainly much more than what you said Isn't? I think I have to expand the idea you just said. For simple one-to-one 'replacement' what you said is enough. But for creating dynamic tables with rows and cols and dynamic pulldowns and making a href etc I have to scratch my head more I think. Thanks.
Frank,
Certainly. Thank you Frank. You know much more than what I thought If possible , post your simple template system on the web. It will certainly be useful for many including myself.
regds
maha anna
Sudhindra Kumar
Greenhorn

Joined: Sep 04, 2001
Posts: 1
Hi..
As per your question or rather requirements,you will be requiring 4 Jsp's.It also depends upon the design of ur project.
According to your description..the first screen gets the user-name and email + password.The second jsp will get the zip-code.the 3rd jsp depending upon the condition will get you shipping and billing address and the 4th jsp will get show the confirmation summary.
These are the front ends.But you dont require to show any jsp for back ends.
I hope i have clarified your answer
Thanx
sudhi
 
Don't get me started about those stupid light bulbs.
 
subject: JSP Design - Basics
 
Similar Threads
Doubt
servlet doubt
Doubt on '#' in query.
doubt
codofobia