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. ]
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 ]
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.
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.
Joined: Oct 09, 2005
I already came across your examples. They are really useful to me, thank you.