Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

servlet -> applet thru XML document

Posted on 2005-05-10
15
Medium Priority
?
214 Views
Last Modified: 2013-11-24
Hello,
Is there any articel which talk about a communication between servlet and applet through XML document?
I mean:
I have an applet which require the informations from database. I used servlet to read the database, and will output to XML document. My applet will open connection to this XML document and parse this one.
Please show me any idea
Thank you very much
0
Comment
Question by:nphnhi
  • 8
  • 5
13 Comments
 
LVL 3

Expert Comment

by:schybert
ID: 13967200
This shows you how to do applet to servlet communication:
http://java.sys-con.com/read/36044.htm

If you need help with the xml parsing just let me know.

'H
0
 

Author Comment

by:nphnhi
ID: 13967513
Thank you, but I don't need the communication like that (I knew that but there are a lot of data information from db, so if we save to variable, we will get OutOfMemory exception), that's why I need to transform to XML document
0
 
LVL 3

Expert Comment

by:schybert
ID: 13967598
I would suggest creating a collection of objects representing the data and then marshalling that to xml with for instance Castor (http://www.castor.org/xml-framework.html#The-Marshalling-Framework).
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:schybert
ID: 13967609
Or are you saying it's just too much data for this too and that you want to store the data temporary on the server as xml?

0
 

Author Comment

by:nphnhi
ID: 13967706
Or are you saying it's just too much data for this too and that you want to store the data temporary on the server as xml?
That what I want :)
0
 
LVL 3

Expert Comment

by:schybert
ID: 13967877
Ah, ok...

First of all you need to make sure your servlet engine has permission to write to the filesystem.

Then you need to create a file and write to it as you parse your ResultSet... something like:

BufferedWriter writer = new BufferedWriter(new FileWriter(myFileName));
writer.write("<root>\n");

while(rs.next()) {
  writer.write("\t<row><col1>"+rs.getString(1)+"</col1><col2>"+rs.getString(2)+"</col2></row>\n");
}

writer.write("</root>");
writer.close();

Then redirect to the xml file:

RequestDispatcher dis = req.getRequestDispatcher("[path to the newly created xml file");

Make sure the xml file has a unique name for each request and delete it when used. Let me know if you need more details.
0
 

Author Comment

by:nphnhi
ID: 13967888
but how to?
0
 

Author Comment

by:nphnhi
ID: 13967912
Oh no, I don't want to create an xml file and delete after using, I just mean the result of the servlet is a XML document, so from an applet, I will read this xml document by opening a connection and read the stream from this connection
0
 
LVL 3

Expert Comment

by:schybert
ID: 13968319
OK, then don't delete the file :-)

But if you let your servlet redirect to the xml file, all you need to do from the applet is something like this (this only prints the xml to standard out though):

HttpURLConnection conn = (HttpURLConnection) new URL("http://myhost/myapp/myservlet").openConnection();            
                        
conn.setInstanceFollowRedirects(true);
conn.setDoInput(true);
conn.connect();
InputStream ins = conn.getInputStream();
            
BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
            
String s = reader.readLine();
            
while(s != null) {
      System.out.println(s);
      s = reader.readLine();
}
            
ins.close();
            
conn.disconnect();

0
 
LVL 3

Expert Comment

by:schybert
ID: 13968339
If the xml is as large as it seems to be you would probably want to parse it with SAX.

0
 

Author Comment

by:nphnhi
ID: 13968603
One more question, if we don't have a right to create a file on server, so we can not using this :
BufferedWriter writer = new BufferedWriter(new FileWriter(myFileName));

In fact, I just think the way to display the data from database to applet, but just because there are a lot of data, so I can not save to variables and transfer to applet from a servlet, so I think about using XML to store them all....
0
 
LVL 3

Accepted Solution

by:
schybert earned 500 total points
ID: 13968693
"One more question, if we don't have a right to create a file on server, so we can not using this :
BufferedWriter writer = new BufferedWriter(new FileWriter(myFileName));"

Yeah, if you don't have the permission to create files on the server you can't do this.

"In fact, I just think the way to display the data from database to applet, but just because there are a lot of data, so I can not save to variables and transfer to applet from a servlet, so I think about using XML to store them all...."

I'm not sure I understand what you mean... Are you saying you want to store the data as xml on the server instead of in the database?

A little note here. If you're having trouble handling these amounts of data on the server (getting OutOfMemoryException) you will probably have the same problems on the client side when you try to display the data...


0
 
LVL 3

Expert Comment

by:schybert
ID: 14024971
So was any of my comments worth anything to you or are you still having trouble?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
A solution for Fortify Path Manipulation.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month13 days, 1 hour left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question