Passing array from servlet to java

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

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

posted Aug 27, 2006 10:31:00

0

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

posted Aug 28, 2006 09:57:00

0

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.

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

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.