• Post Reply Bookmark Topic Watch Topic
  • New Topic

Invoking a Servlet from a J2SE application.

 
David Garratt
Ranch Hand
Posts: 213
Eclipse IDE Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to use a serlet to generate some sequential numbers for me in a multi user enviroment. The application will be partly web based and partly run as a standard J2SE applcation running on a clients PC. Assuming that I have a "synchronised" very small section of code in a servlet that goes away to a database, gets a number, increments in and returns it etc, how would I be able to use (call) this servelet from a J2SE app. The servlet in this case would be completely no-visual, no html required. I don't think it would even need to pick up any parameters, but I would need to return a result back to the J2SE app. The reason behind the synchronised code is to avoid and contention issues in the database. I have deliberately chosen this approach knowing the potential performance bottlenecks of synchronised code as I think it's a clever way of ensuring that two or more processes running on different machines are not reading and incrementing a counter in a database at the same time. I am trying to avoid proprietary techniques like oracle sequences etc.

Any hints / links to articles much appreciated. I'm using Tomcat 5.5 and J2SE 6 on the client side.

Thanks in advance

Dave
 
Animesh Saxena
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Best way I can think of is use a hidden control which gets populated by the servlet by submitting to the same form.

Not sure but I guess you want to hook up the event for generating these numbers via a submit button or something. You can have a form submit or something, do a submit to the same page and not a different one in your case. Use servlet to populate the hidden control with the result and use that control.

Animesh
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way of greatly reducing traffic and the requirement for the client to be connected to the net is to use globally unique identifiers with high/low parts.

You should be able to find out more with a simple search, and we once based an implementation on some pseudo code, but essentially the db only hands out the 'high' parts, and the clients are smart enough to determine the low part of the key. As long as they are able to create more 'low parts' they don't need to ask for a new ID, they can generate it themselves.

eg using binary hi/lo keys: assume the GUID is 8 bits and by design we allow the client to build 8 keys (3 bits) on their own. therefore the client connects to the server and gets it's 5 high bits ABCDE, and can add any three bits FGH of its own to create a key guaranteed not to collide in the db.

There are actually similarities to Oracle sequences in this, since Oracle reserves a bunch of IDs for a connection so it can hand them out without needing to go check the sequence table. The down side is that you tend to waste keys, but by playing with the hi'lo precision you can balance traffic versus waste for something that suites you.

Just a thought.
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
If I understand your question to call a servlet from J2SE application then you should be using HttpURLConnection object to connect and request to the servlet.
Read the inputstream on the connection to read the servlet response. For this to happen servlet will have to write the response on output stream (may be by using ServletOutputStream)

regds
amit
 
David Garratt
Ranch Hand
Posts: 213
Eclipse IDE Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks amit that seems to be what I need.

Dave
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"amit l", you have previously been warned on one or more occasions regarding adjusting your display name to meet JavaRanch standards. This is not optional. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it prior to your next post.

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

Be aware that accounts with invalid display names are disabled.

bear
JavaRanch Sheriff
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!