aspose file tools*
The moose likes Applets and the fly likes Applet and Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Applet and Servlet " Watch "Applet and Servlet " New topic
Author

Applet and Servlet

Teja Venkat
Greenhorn

Joined: Oct 30, 2012
Posts: 27
Hi all..

I retrieved two array objects from my database into the servlet... Now I want to send these to array objects to an applet... how to send them?
Anyone please help me
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
While you could use the Object[In|Out]putStream classes for this -IF the JVMs on both sides are of the same version- a better approach would be a REST call using a library like Jersey, possibly using JSON as the transport format.


Ping & DNS - my free Android networking tools app
Teja Venkat
Greenhorn

Joined: Oct 30, 2012
Posts: 27
Ulf Dittmer wrote:While you could use the Object[In|Out]putStream classes for this -IF the JVMs on both sides are of the same version- a better approach would be a REST call using a library like Jersey, possibly using JSON as the transport format.


Actually I tried Object[In|Out] put Stream classes But I am getting an error
SEVERE: null
java.net.ConnectException: Connection refused: connect
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
That means there's a problem in network connectivity, or in your networking code, not that object serialization won't work. It may still not work due to differeting JVM versions, though.
Teja Venkat
Greenhorn

Joined: Oct 30, 2012
Posts: 27
Ulf Dittmer wrote:That means there's a problem in network connectivity, or in your networking code, not that object serialization won't work. It may still not work due to differeting JVM versions, though.


Then what is the solution for this problem?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
We can't know what might be wrong with your network connectivity. We also can't know what might be wrong with your code because we haven't seen it. The latter you could address by posting a relevant excerpt here.
Teja Venkat
Greenhorn

Joined: Oct 30, 2012
Posts: 27
Ulf Dittmer wrote:We can't know what might be wrong with your network connectivity. We also can't know what might be wrong with your code because we haven't seen it. The latter you could address by posting a relevant excerpt here.


Servlet code:



and Applet code:

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
Are there any exceptions in the Java Console?

Note that you need to run the applet from http://localhost:8084/, otherwise it needs to be signed.
Teja Venkat
Greenhorn

Joined: Oct 30, 2012
Posts: 27
Oct 30, 2012 4:02:56 PM BarApp paint
SEVERE: null
java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8084/WebApplication4/BarChartServ
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1615)
at BarApp.paint(BarApp.java:50)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:781)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:711)
at java.awt.EventQueue.access$000(EventQueue.java:104)
at java.awt.EventQueue$3.run(EventQueue.java:672)
at java.awt.EventQueue$3.run(EventQueue.java:670)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:681)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

This is the Stack trace.... Tomcat was configured in the NetBeans for port 8084 only
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
OK, so the request reaches the server, but the server can't handle it - time to debug the server code.

As regards your servlet code: note that you only need to load the JDBC driver once (like in the servlet's init method), and that you're violating the HTTP specification by treating GET and POST as if they were interchangeable.
Teja Venkat
Greenhorn

Joined: Oct 30, 2012
Posts: 27
Ok, then I have to delete that processRequest method and instead the code has to be written in either doGet or doPost right??
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
Correct. It will need to be POST in this case, since there's data in the body of the request.
Teja Venkat
Greenhorn

Joined: Oct 30, 2012
Posts: 27
Thanks a Lot boss will try it and post my feedback
is it ok to send sql connection object from Servlet to applet once go through the code
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
No, objects need to be serializable. Any kind of stream or network connection is tied to the machine where it was opened, so it can't be sent elsewhere. And anyway, you should not use JDBC across a network from an applet, it's a security risk.
Teja Venkat
Greenhorn

Joined: Oct 30, 2012
Posts: 27
Thanku bro is there any possibility of sending two different arrays from a servlet to Applet
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
Not sure what you mean; can you rephrase?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Bhanu Garimella wrote:Thanku bro

Bhanu, please UseRealWords <-link


luck, db
There are no new questions, but there may be new answers.
Teja Venkat
Greenhorn

Joined: Oct 30, 2012
Posts: 27
Ulf Dittmer wrote:Not sure what you mean; can you rephrase?


Thank you brother

Can we send two object arrays from a servlet (to an Applet) ?
Teja Venkat
Greenhorn

Joined: Oct 30, 2012
Posts: 27
Can we send a Result Set object from servlet to an applet
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

Bhanu Garimella wrote:Can we send two object arrays from a servlet (to an Applet) ?


If the objects are serializable, yes. As already discussed, you would use ObjectInputStream and ObjectOutputStream.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

Bhanu Garimella wrote:Can we send a Result Set object from servlet to an applet


No. The ResultSet contains a reference to the JDBC Connection. And as you already know, you can't send the Connection.

The normal procedure is to go through the ResultSet and extract all the desired data into something which is serializable. A List or array of some custom object which you define could work.
Teja Venkat
Greenhorn

Joined: Oct 30, 2012
Posts: 27
Applet Code :


Servlet Code:


while Executing I got an error like this :

Oct 31, 2012 10:27:32 AM BarApp paint
SEVERE: null
java.io.IOException: Server returned HTTP response code: 405 for URL: http://localhost:8084/WebApplication4/BarChartServ
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1615)
at BarApp.paint(BarApp.java:51)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:781)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:711)
at java.awt.EventQueue.access$000(EventQueue.java:104)
at java.awt.EventQueue$3.run(EventQueue.java:672)
at java.awt.EventQueue$3.run(EventQueue.java:670)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:681)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
Please UseCodeTags in the future when you post code of any length. As it is, it's very hard to read. See how nicely formatted and syntax-colored the code in your previous post looks when I added CODE tags to it? Please edit your post to include those CODE tags.

The server responds with a 405 response - start by finding out what that means, it's easily searchable. The problem is that the applet performs a GET, while the servlet only understands POST.
Teja Venkat
Greenhorn

Joined: Oct 30, 2012
Posts: 27
I placed a break point and tried to debug the applet and it is jumping to catch block at thi point of the code :
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
That's where the exception occurs because that's where the server is accessed. But the determination of whether to use GET or POST is made before that when you set up the connection.
Teja Venkat
Greenhorn

Joined: Oct 30, 2012
Posts: 27
Suggest me a solution to this problem
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
Well, I already mentioned that the problem is in how you're setting up the connection. Which lines of your code are involved in that? Have you read the javadocs of those method calls in detail?

I grant that the solution is not obvious, but just telling you what it is deprives you of a learning opportunity (which is what we try to provide here at JavaRanch).
Teja Venkat
Greenhorn

Joined: Oct 30, 2012
Posts: 27
Ulf Dittmer wrote:Please UseCodeTags in the future when you post code of any length. As it is, it's very hard to read. See how nicely formatted and syntax-colored the code in your previous post looks when I added CODE tags to it? Please edit your post to include those CODE tags.

The server responds with a 405 response - start by finding out what that means, it's easily searchable. The problem is that the applet performs a GET, while the servlet only understands POST.


I didn't understand this phrase... Can you explain this in detail
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
Then you need to read up on HTTP. GET and POST are the most commonly used HTTP methods (the ones handled in a servlet by the doGet and doPost methods, respectively). If you want to perform HTTP communication (or web development in general, actually) then you really need to understand the HTTP methods, their uses, their similarities and differences.

The HTTP specification goes into a lot of detail on this, but it's easier to start at Http.
Teja Venkat
Greenhorn

Joined: Oct 30, 2012
Posts: 27
Actually I didn't had much time for learning servlets deeply
I have to submit a module tomorrow
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
Then it comes back to:
Ulf Dittmer wrote:Well, I already mentioned that the problem is in how you're setting up the connection. Which lines of your code are involved in that? Have you read the javadocs of those method calls in detail?
Teja Venkat
Greenhorn

Joined: Oct 30, 2012
Posts: 27
Ulf Dittmer wrote:Then it comes back to:
Ulf Dittmer wrote:Well, I already mentioned that the problem is in how you're setting up the connection. Which lines of your code are involved in that? Have you read the javadocs of those method calls in detail?


Will read that JavaDocs and try to solve the problem
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
Hm, re-reading the javadocs now, they're actually not helpful in this case at all. The problem is in the con.setDoOutput(false) call - "false" means that GET will be used, whereas "true" would make this a POST.
Teja Venkat
Greenhorn

Joined: Oct 30, 2012
Posts: 27
Even when I make it as

the same error was repeating
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
You may have to write something to the output, even if you don't use it in the servlet: http://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html
 
 
subject: Applet and Servlet