Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: BEGINNER: Asking user input from java/class to jsp

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default BEGINNER: Asking user input from java/class to jsp

    Hi all,

    Definitely new to java (but have 13 years of VB/.NET experience). I wrote a little Monopoly app that asks for user input through the course of the game (such as 'How many players' before creating the game and then asking each user for their name before starting). It works wonderfully from the console but now I'm trying to give it a bit of a front-end.

    I may not be taking the correct approach, but I'm working with JSP's and a copy of my original .java files. The problem I am having is that after posting the 'How many players' form and calling my createGame method, I can't figure out how to have my code ask the user(s) for their name instead of halting in the System.out.println(). Basically, I can't figure out how to get data to flow from .java to .jsp (I've got the .jsp to .java down, I believe).

    So, to put it in more of a flow, I have this:
    - index.html is the starting point and has a POST form asking for the number of players. The action goes to SetPlayers.jsp.
    - in the SetPlayers.jsp, I read in the number of players from request.getParameter. I then initiate my main class and call newGame(numPlayers)
    - newGame iterates over the numPlayers and creates a player for each one (addPlayer)
    - addPlayer first asks the player for their name and token (such as 'Joe' and 'Car'). It then creates a new player and sets the player attributes as per the user input. This is repeated for the number of players. This is pretty much where I am stuck as I can't figure out how to pass the input back to the .jsp instead of sticking to the console and causing my .jsp to spin/wait.

    And finally, here's the snippets of my code, as it might be more beneficial.
    //Index.html
            <FORM METHOD="POST" ACTION="SetPlayers.jsp">
                How many players will be playing? <INPUT TYPE="TEXT" NAME=numPlayers SIZE=5><BR>
                <P><INPUT TYPE="SUBMIT" VALUE="Create Game">
            </FORM>
     
    //SetPlayers.jsp
            <%= request.getParameter("numPlayers") %>
            <% 
                int numPlayers = Integer.parseInt(request.getParameter("numPlayers"));
     
                com.jlr.monopoly.TestApp monopoly = new com.jlr.monopoly.TestApp();
     
                monopoly.setNumPlayers(numPlayers);
     
                monopoly.newGame(numPlayers);
            %>
     
    //TestApp.newGame
        public void newGame(int numPlayers) {
            Game game = new Game();
     
            game.createGame(numPlayers);
     
            game.startGame(numPlayers);
        }
     
    //Game.createGame and Game.addPlayer
        public int createGame(int numPlayers) {
            Scanner input = new Scanner(System.in);
            int x = 1;
     
            playerArray = new Player[numPlayers];
     
            while (x < numPlayers+1) {
                playerArray[x-1] = addPlayer(x);
                x++;
            }
     
            return numPlayers;
        }
     
        public Player addPlayer(int playerSeq) {
            Scanner input = new Scanner(System.in);
     
            String playerKey = "Player" + playerSeq;
     
            System.out.println(playerKey + ": Please enter your name");
            String name = input.next();
     
            System.out.println(playerKey + ": Please enter your token");
            String token = input.next();
     
            Player player = new Player(name, token, 0);
     
             return player;
        }

    Any help to get me going in the right direction is greatly appreciated. I know my form isn't great, but I can always refactor as I go.

    Thanks in advance!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: BEGINNER: Asking user input from java/class to jsp

    Just to make sure I understand your question correctly, you are having trouble persisting data from one page to the next (or 'n' pages forward)? If this is correct, I might suggest to either a) keep the data saved within a Session or b) save the data to a database.

    It works wonderfully from the console but now I'm trying to give it a bit of a front-end.
    Any reason you decided to have the front end be a web-app? Just asking, because in my opinion (and from a learning perspective) a more gradual step might be to build a front end using Swing rather than diving head first into J2EE web applications

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: BEGINNER: Asking user input from java/class to jsp

    Thanks for the reply copeg and sorry for not getting back quickly. I kind of shelved my project for a short period of time to clear my head.

    Anyway, I can persist data throughout pages just fine and sessions are no problem for me. After taking the short break and doing some reading, I think my problem ultimately is that I'm taking the wrong approach. However, I will still attempt to explain better and more simply.

    I have a java class. When run, this class invokes a Scanner and asks the user for input via the console (System.out.println). Code continues based upon the value that the user entered. This works perfectly fine when I run it as a non-web/non-interfaced app.

    Now, I want to give this java class a web front-end. I created a jsp that imports my java class and makes the call to it. The class code runs, but the jsp page just spins because the code has passed the question to the console instead of to the jsp page. This is where my problem lies. I can't figure out how to have the java class pose the question on the front-end instead of in the console.

    I can't have my jsp knowing all of the code (but I suppose a servlet could; but that would mean I am essentially porting my app to servlets). Some of the input I can anticipate (such as asking how many players there are; that is static) but there are others that I can't anticipate (such as the next question after entering the number of players; the code then iterates over that player count and asks each player for his/her name and some other information; I can't anticipate how many players a user will enter so I can't static-code for it).

    How should I go about this? Is there something I am missing or is porting the code to servlets the approach I should take?

    Oh, and as for the question of the front-end being web-based, I took a 6-week ValTech J2EE "bootcamp" training 5 or 6 years ago when I was working for Blue Cross/Blue Shield and they were migrating from VB6 to Java. I never got the opportunity to put to use what I learned (and have thus forgotten most of it), but it is still more experience than I have with Swing. I have also seen more thin client java development jobs than I have thick client jobs. Since I am trying to branch out of my Microsoft suite developer role, I am trying to learn what I think will have the best career impact, thus why I went with web. I suppose I could also do it in Swing while I'm learning.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: BEGINNER: Asking user input from java/class to jsp

    t. The class code runs, but the jsp page just spins because the code has passed the question to the console instead of to the jsp page. This is where my problem lies. I can't figure out how to have the java class pose the question on the front-end instead of in the console.
    Thanks for the explanation. If it were me I would refactor the code of the underlying 'class' (class in quote to distinguish it from any class in the web container) as the current route seems to be mixing user interfaces, one of which is the web interface and the other the command line (not necessarily an approach I would recommend). The way one could refactor is to extract the model from your 'class', such that it can function independent of any user interface. In this manner you can plug it into any interface you wish, be it command line, swing, or jsp. If you've never heard of the MVC design pattern (or its been a while since you studied it) I would recommend reading up on it as it is a very important concept

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: BEGINNER: Asking user input from java/class to jsp

    I 100% understand what you're saying copeg, though I'm not seeing how I can make the model ask for user input independent of any interface. Well, scratch that, I have some ideas I can act on and I'm sure this is something I can read up on. I've heard of MVC, but haven't used it personally. I will do some readying and some trials and see what I can come up with. Thanks copeg!

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: BEGINNER: Asking user input from java/class to jsp

    though I'm not seeing how I can make the model ask for user input independent of any interface
    You don't, that's the beauty of it. Lets say your program deals with organizing employees for a company. Your model could consist of, amongst other things, a class named Employee. This class can contain fields with first name, last name, title, etc...Your GUI, whether it be JSP or command line (eg Scanner) would ask for first name, last name, etc...then create an instance of the Employee class and populate the appropriate field of this object. Just a simple example, it can get much more complex...good luck

  7. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: BEGINNER: Asking user input from java/class to jsp

    Ok, so basically the jsp (or any other interface I choose) is the controller of the questions and not the class files? That way the interface passes the response in to the class instead of the other way around?

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: BEGINNER: Asking user input from java/class to jsp

    Pretty much, but again it can get complex and tough to demonstrate without a concrete example. I'd suggest reading up on Object Oriented Programming concepts as well as design pattern concepts (in particular MVC). Good luck

  9. #9
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: BEGINNER: Asking user input from java/class to jsp

    Thanks copeg. I think I just had that 'ah-ha' moment. It makes perfect sense and definitely gets me in the right direction. I'll pick back up on my project after doing some MVC and OOP reading.

    Thanks again!

  10. #10
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: BEGINNER: Asking user input from java/class to jsp

    Well copeg, your information turns out to be exactly what I needed to know. I'm still learning a lot about MVC and OOP but I am making good progress now. I created a controller servlet that generates the presentation, interacts with the service layer, and handles all of the user interactions. Of course, that means I altered my classes as well so that they don't care about the user input, rather only what is passed in to them. Heck, it's going so well that I'm even handling my presentation with AJAX. Of course, I have a ton of refactoring to do when I'm done, but it's clicking at a good pace now.

    Thanks again copeg!

Similar Threads

  1. Java user input troubles using the scanner class
    By Lach in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 9th, 2013, 11:13 AM
  2. How to repaint() a class in a JFrame, with a JTextfield user input
    By DANGEROUSSCION in forum Object Oriented Programming
    Replies: 1
    Last Post: March 23rd, 2013, 03:06 PM
  3. Replies: 4
    Last Post: December 4th, 2012, 06:11 PM
  4. Reading user input from the console with the Scanner class
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: September 7th, 2011, 03:09 AM
  5. Reading user input from the console with the Scanner class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: November 11th, 2008, 02:47 PM