wood burning stoves 2.0*
The moose likes Servlets and the fly likes Passing array from servlet to java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Passing array from servlet to java" Watch "Passing array from servlet to java" New topic
Author

Passing array from servlet to java

George Botsaris
Greenhorn

Joined: Aug 27, 2006
Posts: 4
Hello guys
I am writing a game in J2ME. In this game i am using a servlet to communicate with my database in which i store my online highscores.
When a player wants to view the online highscores a servlet loads the highscores from my mysql database and saves them in an array. I would like to send this array from my servlet to my java program. Can this be done???
Can i send an array using the responce parameter of the servlet? If yes how can i retrieve it from my application?
Thanking you in advance
George Botsaris
Chad Clites
Ranch Hand

Joined: Aug 16, 2005
Posts: 134
I don't quite understand your question. Are you trying to pass the array to a client side app, or a server side app? If it is server side, you just need to declare an instance of your class, and pass the array as a parameter like you normally would.

Or am I misunderstanding your question? (little low on caffeine this morning)
George Botsaris
Greenhorn

Joined: Aug 27, 2006
Posts: 4
I am trying to send an array with my highscores from a servlet to a client side app(my J2ME game).As i understand i have to use the 'HttpServletResponse responce' in my doGet method in my servlet to do that. Is it possible to pass my array as a parameter and then read it somehow in my J2ME game?
Thanks for your help
Chad Clites
Ranch Hand

Joined: Aug 16, 2005
Posts: 134
Hmm.. I thought that someone who actually knew how to do this would have responded by now, because I have never done anything like this.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I do know that it's possible to return a serialized java object in a servlet response (for consumption by an applet). There are several articles on this available on the web. http://www-128.ibm.com/developerworks/java/library/j-tunnel/?dwzone=java

I haven't commented earlier because I don't know enough about J2ME to say whether or not it is capable of deserializing the object on the client end.

Say the word and I'll move this to our J2ME forum for you.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

Is the servlet hit being initiated by the client application? If so, how? You're not being very clear about the mechanisms you are using.

Most likely, you'll need to return the data as the body of the reponse. Perhaps as XML?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
George Botsaris
Greenhorn

Joined: Aug 27, 2006
Posts: 4
Thanks for your responce guys
I managed to send my highscores from the servlet to my app by opening a connection and sending one highscore eachtime.
So if i have 10 highscores in my database i will have to open a connection 10 times, each time sending a different highscore from my servlet to my app as a string.
I dont know if thats very wise, but thats the only way i could thought.
Thanks again for replying
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

It's nice when someone asks a question and completely ignores the replies.
George Botsaris
Greenhorn

Joined: Aug 27, 2006
Posts: 4
I didnt mean to be rude, but i found an alternative way to do the job and moved on with my project
Thanks again for showing interest in my post
amr gamal
Greenhorn

Joined: Feb 09, 2011
Posts: 2
George Botsaris wrote:I didnt mean to be rude, but i found an alternative way to do the job and moved on with my project
Thanks again for showing interest in my post



Can you tell me this way please ?
because I'm falling in the same problem.
Eamonn Moloney
Greenhorn

Joined: Jan 09, 2009
Posts: 1
Hey,

I know this is a bit late but I just found the post today. You could seralise the java object in the servlet and then deseralise it on the client however that can be quite limiting. A nicer way would be to send the scores back to the client as xml or json. An XML parser comes with j2me or you can use one of the many json parsers available such as https://launchpad.net/j2me-json

The basic idea is to have you client make a request to the servlet and have your servlet return the xml or json formatted response. Set your array in the request attribute and forward the servlet to a JSP. Then use JSTL to render the JSP.

Another alternative... albiet more complex solution is the apache thrief project. If you are planning an iPhone, android, j2me, javascript version of you app it would be very well suited to fit your needs.

http://thrift.apache.org/
amr gamal
Greenhorn

Joined: Feb 09, 2011
Posts: 2
Hi,
I've found an easy way, just putting the array elements in a string separated with delimiters.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing array from servlet to java