This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes Multiple Users on a Page 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 » JSP
Bookmark "Multiple Users on a Page" Watch "Multiple Users on a Page" New topic
Author

Multiple Users on a Page

Jakub Kulpa
Greenhorn

Joined: Nov 29, 2011
Posts: 10

Hello,

This is my first post, although not the first time viewing, on this website. I have a question about JSP pages that I cannot understand. I am currently working on a Connect 4 project where I need to have two users connect to the same webpage with the game on it. The first page a user encounters is a "login" page asking for a name and once they submit the name they are taken to the page with the game.

My question is how would I go about connecting two different browser windows to this game? (For testing) I know that a JavaBean would be one way to do it but I have never created one and am completely lost as to how to do it. Any help would be necessary and I can provide the code I have upon request.

Thank you so much.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60783
    
  65

Are you a newbie to Servlets and JSP? If so, this might be too ambitious for a first outing.

If not, then you know that each user will have their own session. So you'd need to keep track of the various games (I assume you want to have more than just one game going at a time) in application scope.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jakub Kulpa
Greenhorn

Joined: Nov 29, 2011
Posts: 10

I am fairly new to servlets but I do not have a choice, this is for a project that I have to do for a course I am taking. I am completely lost on how to implement a JavaBean, I know that it must be in application scope. How would I go about actually putting it in the code and having it run with multiple users?

Thank you.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60783
    
  65

Yikes -- way too complex an assignment for an intro course, in my opinion.

In any case, create a class or classes to abstract the state of the game. That's rather independent of servlets or JSP. Once you have classes that work to capture the game, you can store it (them) in app scope so that both users have access to it.
Jakub Kulpa
Greenhorn

Joined: Nov 29, 2011
Posts: 10

Bear Bibeault wrote:Yikes -- way too complex an assignment for an intro course, in my opinion.


Agreed! Haha.

How would I go about storing the game in application scope? I'm sorry, but I am extremely new to the concept of servlets, JSPs, JavaBeans and everything. All that I have ever done is some minor work in Java but nothing to this extent and nothing on the web.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60783
    
  65

You really need to find yourself a good modern tutorial on servlets and JSP. There's way too much to gather with a handful of forum posts.

To place an object in app scope, use ServletContext.setAttribute().
Jakub Kulpa
Greenhorn

Joined: Nov 29, 2011
Posts: 10

Okay, thank you. I appreciate your help!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60783
    
  65

I'm not saying not to ask questions here -- but I think you're going to need more info than we can feed you in forum posts.

Jakub Kulpa
Greenhorn

Joined: Nov 29, 2011
Posts: 10

:thumbup:

I got ya! No worries. One more question then, I'm not sure if this is the right forum for it or not, is it possible to take a input from an HTML form on one page and use that for the value of a variable in a javascript program on the next page?

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60783
    
  65

Yes. The whole point of JSP is to be able to dynamically create HTML markup, and that includes JavaScript code.
Jakub Kulpa
Greenhorn

Joined: Nov 29, 2011
Posts: 10

Okay, thanks for the helpful info!
Jakub Kulpa
Greenhorn

Joined: Nov 29, 2011
Posts: 10

I am pretty new to JSP pages and I was wondering how to get two browsers on my computer to connect to the same page and see each other.

I have a login page set up, so both browsers would access that login page first, then login with unique usernames and then once they login I want to post-login page to refresh so that all the users that are accessing that page are visible.

Is there a quick way to do this?

Any help is appreciated. Thank you
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60783
    
  65

It all goes back to the same concept: the data needs to be stored somewhere where the entire application has access to. For run-time information (most appropriate for a game), that would be application scope. For most other non game (e.g. business) uses, a database is frequently used.
Jakub Kulpa
Greenhorn

Joined: Nov 29, 2011
Posts: 10

Do you know of any places I can see examples of beans with application scope? I am completely lost on how to implement this and put it all together.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60783
    
  65

Can't think of any off the top of my head. Most business applications will not share information in this manner. In fact, they rather go out of their way to make sure that data is not shared between sessions.

But it's really not all that complicated. Anything you put into app scope is accessible to all of the application. The ServletContext's setAttribute() and getAttribute() methods are the access methods.

The only tricky thing to be careful of is synchronization for read-write data.
Jakub Kulpa
Greenhorn

Joined: Nov 29, 2011
Posts: 10

I don't know why I'm having the hardest time ever understanding this concept.

Currently I have a index jsp page where the user is asked to type in the name and click submit. Once that is submitted it is sent to a servlet that grabs the name submitted and prints it out on the screen. Where does the bean come in to all of this and how would I then pass the name from the servlet to the bean? Or would I pass the name straight from the index to the bean?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60783
    
  65

The problem is that you are trying to do something rather advanced before understand the very basics of web application technology.

You keep mentioning "the bean". Have you defined "the bean" and what it represents? As I said earlier, worrying about the mechanics is premature before you have decided how you are going to abstract the data that you need to maintain to represent the model of the game. Have you done that?
Nauman Hasan
Ranch Hand

Joined: Jul 27, 2005
Posts: 34
I agree with Bear, this seems pretty complex as a first assignment.

You should try to reach through an introductory tutorial and that will help you understand the overall context Java EE tutorial

~Nauman
 
jQuery in Action, 2nd edition
 
subject: Multiple Users on a Page
 
Similar Threads
JSP Page with multiple users
my jsp code is ugly, how can i clean it?
Awkward question (sorry)
"Random" server
J2ME Server & Image Size