• Post Reply Bookmark Topic Watch Topic
  • New Topic

Silly broad design question

 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm really new to java (about 4 months) and even more new to J2EE (about 2 weeks), so I'm not sure if I should post this question in the beginning java forum, or here. I have an assignment where we need create a craps game that runs automatically with no user interaction outside of hitting refresh in your browser. The example given is this: Craps Example. I'm not looking for anyone to give me the total solution or write the code for me, but rather point me in the right direction.

I get that I'll need math.random as well as ifs and nested if elses, that part I can wrap my head around. If this were just a .java file, I think I could write it in 20 minutes (don't hold me to that though )

I guess I'm just trying to figure out which files I actually need to create to do this. Is this something that I can do without any .jsp's? Just make a servlet java class file, edit my web.xml file, create the .war, and deploy(we're using Tomcat 8 for now by the way)? Or will something of this nature be more difficult (difficult for me at least) and I'll need .jsp files or more than one servlet?

This is my thinking as of right now, but nonetheless I feel lost:

1. create directory structure
2. create servlet class file?
3. edit my web.xml in WEB-INF?
4. create my .war file?
5. deploy?

Again very new to all this, and this place has proved helpful on more than one occasion. Thanks in advance for any direction.

David
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The generated HTML is simple enough that you could simply generate it in the servlet for simplicity. But be aware that building up HTML in strings, rather than using a JSP template, is not a good practice. Don't make it a habit, but baby steps sometimes...

Also, avoid putting any, and I mean any, game logic in your servlet. That's not its job.

Write the game logic in a class or set of classes that could be used in any environment. Then, use the servlet to interact with it. If your game model cannot also be used with a console UI, or with a Swing UI, (or any other UI) your design has failed.
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:The generated HTML is simple enough that you could simply generate it in the servlet for simplicity. But be aware that building up HTML in strings, rather than using a JSP template, is not a good practice. Don't make it a habit, but baby steps sometimes...

Also, avoid putting any, and I mean any, game logic in your servlet. That's not its job.

Write the game logic in a class or set of classes that could be used in any environment. Then, use the servlet to interact with it. If your game model cannot also be used with a console UI, or with a Swing UI, (or any other UI) your design has failed.


Thanks for the response. I'm still pretty hairy on the syntax of servlets, jsps, and html in general so I think that's where a good portion of my confusion is coming in. But what you said makes sense. I just need to figure out how exactly to have the servlet interact with the created "game logic" class. Thank you again for the response!

EDIT: And we have definitely been using HTML in strings (I'm guessing because we're just getting started in the class), so I may end up going this baby steps route, but good to know that in general, that isn't good practice
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of it this way. Your game logic class might be a class called Dice. Let's assume you are rolling two dice, so it would make sense for that class to have a method called rollDice() that returns an array with two integers of 1 to 6. Your servlet will instantiate an instance of that class and call the rollDice() method, then use the returned integers something like this.

Now diceValues[0] and diceValues[1] will contain your values.

As Bear pointed out, you could use out.println statements to generate the html, or you could store the integers as request attributes and forward the request to a jsp page.

This way the DiceRoller class doesn't need to know that it's being called by a servlet. It could be a Swing UI, a mobile app, whatever. All it does is send two integers to it's caller. It doesn't care who the caller is.

If you want to be a bit more sophisticated, you can pass the number of dice you need because later on you might want to make a Yahtzee game that uses 6 dice. So just create a rollDice(int numRolls) method that will roll as many dice as you need and return an array with that many integers.

Give it a shot and post your code if you need more help. I hope this helps.
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:Think of it this way. Your game logic class might be a class called Dice. Let's assume you are rolling two dice, so it would make sense for that class to have a method called rollDice() that returns an array with two integers of 1 to 6. Your servlet will instantiate an instance of that class and call the rollDice() method, then use the returned integers something like this.

Now diceValues[0] and diceValues[1] will contain your values.

As Bear pointed out, you could use out.println statements to generate the html, or you could store the integers as request attributes and forward the request to a jsp page.

This way the DiceRoller class doesn't need to know that it's being called by a servlet. It could be a Swing UI, a mobile app, whatever. All it does is send two integers to it's caller. It doesn't care who the caller is.

If you want to be a bit more sophisticated, you can pass the number of dice you need because later on you might want to make a Yahtzee game that uses 6 dice. So just create a rollDice(int numRolls) method that will roll as many dice as you need and return an array with that many integers.

Give it a shot and post your code if you need more help. I hope this helps.



Thanks for the response. What I've been doing for the last hour or so has been just writing a java file that will compile and run (thanks for not holding me to the aforementioned 20 minutes ), just to see if I could get that logic right. I have it working...kind of. I have each line numbered so the line numbers repeat themselves if the player doesn't win by the 3rd roll.



So is it totally silly to keep all my logic in one file? And then use this class file to interact with the servlet? I definitely want to take baby steps, but I don't want to overlook any way I could be more efficient (like what you're saying with the method call returning an int[]. Thanks again for the response
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


took out the print.ln with line# .7 that was previously at line 40 and at least it looks cleaner, but still repeating line numbers. Now to figure out this servlet/jsp/xml nonsense

EDIT: that was short lived; ran it a few times and I lost rolling a three (after the first roll, only a 7 should cause a loss :/ )
 
David Borchgrevink
Ranch Hand
Posts: 93
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it working for anyone who is curious:



i won't post the .xml file since it's kind of irrelevant. but it works so yay!
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations on getting it working. Now it needs to be polished. You still have too much code in the servlet. The servlet is the controller, he is just the traffic cop that sits between the logic/business classes and the view (jsp or html) pages. Many times, my servlets are only 4 or 5 lines of code. All that dice rolling code should be in a separate class that can stand on it's own and be used in other applications. Like I showed you in my last post, the servlet should just create an instance of the Dice class and call the rollDice() method. Then it will take the returned values and either use println statements to generate html, or preferably, set the results as request attributes and forward them to a jsp page.

Suppose your next assignment was to build a Swing GUI for the same game. Do you have any reusable code there? If you had a Dice class that could stand on it's own, you wouldn't need to change anything except build the GUI; the logic would already be done.

Still, you deserve lots of credit for your perseverance. That's what will make you successful at this. Good work.
 
Tim Holloway
Bartender
Posts: 18417
60
Android Eclipse IDE Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I first began working with J2EE, JSPs hadn't been invented yet, and servlets were the only way to generate content.

It wasn't much fun, believe me.

In modern times, the Model/View/Controller paradigm is common because it enforces Separation of Concerns - meaning that you have several simple special-purpose components instead of one big, ugly complicated does-everything component.

A JSP can be (and should be) used as a View Template. The Java EL (Expression Language) provides an easy way to replace those complex print statements in your servlet with simple expressions referencing backing javabean (Model) properties in the View (JSP). That leaves the servlet to be the Controller - whose task it is to make the Model and View data values correspond, and a business logic bean to hold the actual game logic. This bean may also contain the data model properties, but it's generally preferable to hold them in a session or request-scope JavaBean object and have the business logic bean act upon that object.

If you are not familiar with JavaBeans, their uses and their coding conventions, you should be. It's one of the core concepts of Java, and not just in the J2EE/JEE environments.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!