• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Applet and Servlet

 
Teja Venkat
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Teja Venkat
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct. It will need to be POST in this case, since there's data in the body of the request.
 
Teja Venkat
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanku bro is there any possibility of sending two different arrays from a servlet to Applet
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you mean; can you rephrase?
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhanu Garimella wrote:Thanku bro

Bhanu, please UseRealWords <-link
 
Teja Venkat
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we send a Result Set object from servlet to an applet
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suggest me a solution to this problem
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I didn't had much time for learning servlets deeply
I have to submit a module tomorrow
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even when I make it as

the same error was repeating
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic