aspose file tools*
The moose likes JSP and the fly likes my jsp code is ugly, how can i clean it? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "my jsp code is ugly, how can i clean it?" Watch "my jsp code is ugly, how can i clean it?" New topic
Author

my jsp code is ugly, how can i clean it?

Thomas Burette
Greenhorn

Joined: Oct 09, 2005
Posts: 3
I'm new to the, oh wondeful, world of jsp and i have problems to organize my jsp code. the .jsp files i write tend to get ugly pretty fast.
I am trying to do a simple game for 2 players. Basicly there is one page to connect to the game where you put your name in a form and another one which is the game itself.

What's the problem?
For example the content of the page with the form can change. If you are the first player it will ask for a name and the game parameters. But if you are the second player it will only ask for your name. And if there are already 2 players than it will display "Sorry a game is going on, try again later".

how did i implemented that?
i have a huuuge <% %> containing all the java code and the logic is :
if(noplayer)->display full form
if(1 player)->display name form
if(2 player)->try again later
Add an optional parameter giving the default name and another optional parameter for a message to the mix and you obtain a big ugly code of 3 pages full of duplications and nested if.

It's even worse for the game page because it can display depending on the game state and the current player:
-a message for the first player when he waits for a second player
-a "please wait while the other player make his move"
-a page where the active player can make his move
-a you have won page
-a you have lost page

What should i do?
-Split the game in a lot of page and create a redirection fest? I don't think it's a good idea because i would have "a lot" of different pages which logically represent the same page.
-create methods inside the jspcode which would take care of each case? It sound good to me but i'm using eclipse-lomboz with jboss and when i try to include the JspWrite with :

it gives me the following error : "The import javax.servlet.jsp cannot be resolved". Thus i can't pass out as a parameter to my methods.
[ October 09, 2005: Message edited by: Thomas B. ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

There are quite a few tools in the Server Side Java world that can help you to organize and structure your code both for maintainability and for scalability.
None of them work particularly well with scriptlets <% %>.

My suggestion: put the game on the back burner for a short period of time and work through a book or some web tutorials to get a feel for the various building blocks that J2EE offers. You may want to pick up a book on design patterns too (there are several out their aimed specifically at Java and even J2EE).

Once you have, most of your questions will answer themselves.

A server side game shouldn't be limited to two users. For a two player game most people would expect start out by entering a "lobby" or page where player's can pair pair up to play. J2EE's concept of beans and scoping variables will come in very handy when building something like this.
[ October 09, 2005: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

"Thomas B.",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

To add to Ben's excellent advice, it sounds like you are trying to do much too much on one page, and are trying to do much too much on the pages. A pattern you should particularly research is the "Model 2" pattern in which all or most of the logic takes place in servlet controllers which then forward to simplified JSP pages whose purpose is merely to render the view.
Thomas Burette
Greenhorn

Joined: Oct 09, 2005
Posts: 3
Originally posted by Bear Bibeault:
"Thomas B.",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender


I'm sorry. I read it but i thought it was ok to use an abbreviation.
By the way your PM mailbox is full.


As for my problem, i'll go read some books/tutorial/whatever before asking more (stupid?) questions.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

Thomas, initials are OK for the first name, not the last name. Thanks for adjusting your name.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Thomas Basseilo:
As for my problem, i'll go read some books/tutorial/whatever before asking more (stupid?) questions.


I don't think the questions were stupid at all.
That you recognize the mess that Model1 JSPs can be, and that you realize that further growth with this architecture is only going to make them uglier shows good intuition and foresight. I suspect you'll take to Model2 coding quite naturally.

I've put together the simplest example of Model2 coding that I could in SimpleMVC at http://simple.souther.us. It's not a tutorial but it sometimes helps to have a working example when you are reading the tutorials.

Good-Luck
Thomas Burette
Greenhorn

Joined: Oct 09, 2005
Posts: 3
I already came across your examples. They are really useful to me, thank you.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

I don't think the questions were stupid at all.


I concur with Ben. Not being satisfied with "ugly" JSP pages just because they work is a very very good thing!
[ October 10, 2005: Message edited by: Bear Bibeault ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: my jsp code is ugly, how can i clean it?
 
Similar Threads
why can't some people play my game
HELP!! source code for tic tac toe game..
Loop for players
OO design question?
Awkward question (sorry)