• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Sending data from applet to servlet

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I am trying to learn how to communicate to servlet from applet.I have build an applet containing a text field and a button.When I press the button The text field value would be sent to a servlet where it will be displayed.But when I press the button nothing happens. I am giving the layout of my files and code fragments where I suspect I have made mistakes.

Layout of files: I got a web dynamic project "Test" there I have a package"pkTwo" which contrains the servlet "MyServlet.java" For the Applet I used a normal java project and made a jar file and kept it in the "webContent"
along with my jsp which calls the applet using this jar.

heres the method(From my Applet) used for my buttons listener to send the string of text field



here is the doGet() of my servlet


and heres is the web.xml code
What are the mistakes I have made?please let me know
 
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start by making sure that you're using the right URL. For that I'd use a the URL constructor that takes a single parameter. That also makes it easier to test the URL outside of the applet. The "codebase " anyway an applet concept that I wouldn't use to create an URL to a servlet. If the servlet listens to "/servletlink", then the URL you're using is almost certainly incorrect.

Furthermore, if you're setting doOutput to true, then you're doing a POST, and consequently need to override doPost instead of doGet.
 
Shamim Emon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:Start by making sure that you're using the right URL. For that I'd use a the URL constructor that takes a single parameter. That also makes it easier to test the URL outside of the applet. The "codebase " anyway an applet concept that I wouldn't use to create an URL to a servlet. If the servlet listens to "/servletlink", then the URL you're using is almost certainly incorrect.

Furthermore, if you're setting doOutput to true, then you're doing a POST, and consequently need to override doPost instead of doGet.



So now I am using single parametered URL connection
but still doesnt work..ALso I have overrieden thr doPost() insted of doGet().But still can't go to servlet with the bitton press in applet.so what am I forgetting to do??

Thanks
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check your server-side access logs which URL is getting accessed.

Also, "Test/servletlink" looks like a relative URL, it may not be the one that you think it is.
 
Shamim Emon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I put " inside my doPost() and found "http://localhost:8080/Test/MyServlet" is the url being accessedwhen I run the servlet file directly.so isnt it the right url?.But from the applet its not working just as before. When I click on the submit button nothing happens as if I didn't use action listener(although I did) .So where is the bug? isnt it the right url or have I made any other mistake?Please let me know
Thanks
 
Shamim Emon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way if it isnt too much to ask then can I send you my whole project(.War) any how? This site won't allow me to upolad any zip/war file.Sorry if it seem rude
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"http://localhost:8080/Test/MyServlet" is the url being accessedwhen I run the servlet file directly.so isnt it the right url?.


If you don't know that, then nobody does :-) You can test the URL by adding a doGet method to the servlet - that would be accessed if you hit that URL in a browser.

I click on the submit button nothing happens as if I didn't use action listener


Is the code in the action listener being executed at all? Add a System.out.println statement or some other visible cue to make sure it is. (If you don't see anything in the server access log while the applet runs then that likely means the applet does not execute the code at all.)

By the way if it isnt too much to ask then can I send you my whole project(


That is too much to ask (at least of me, maybe someone else would like to take a look).
 
Shamim Emon
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Add a System.out.println statement or some other visible cue to make sure it is. (If you don't see anything in the server access log while the applet runs then that likely means the applet does not execute the code at all.)
yap I did that before.When its an applet(not inside the web project as jar file it works..but when it in the web project it doesnt output anything on the console.


That is too much to ask (at least of me, maybe someone else would like to take a look).

Its okay!I completely understand.And sorry I asked for that.
You already have been really helpful.Thanks for that

Cheers
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am posting the complete code to transfer image file.



Sending a file from Applet to Servlet

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;


public class TestApplet extends JApplet implements ActionListener
{

JButton jbutton = null;
public void init()
{
jbutton = new JButton("Send file");
jbutton.addActionListener(this);
this.getContentPane().add(jbutton);
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == jbutton)
{
try
{
File file = new File("C:\karthik.jpg");

FileInputStream in = new FileInputStream(file);
byte[] buf=new byte[in.available()];
int bytesread = 0;


String toservlet = "http://localhost:8080/servlet/MyServlet";

URL servleturl = new URL(toservlet);
URLConnection servletconnection = servleturl.openConnection();
servletconnection.setDoInput(true);
servletconnection.setDoOutput(true);
servletconnection.setUseCaches(false);
servletconnection.setDefaultUseCaches(false);

DataOutputStream out=new DataOutputStream(servletconnection.getOutputStream());

while( (bytesread = in.read( buf )) > -1 )
{
out.write( buf, 0, bytesread );
}

out.flush();
out.close();
in.close();

DataInputStream inputFromClient = new DataInputStream(servletconnection.getInputStream());
//get what you want from servlet
//.......
inputFromClient.close();
}
catch(Exception e)
{
e.printStackTrace();
}


}
}

}


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class MyServlet extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse res)
{

ServletContext sc = this.getServletContext();
try
{
String fileName = "karthik.jpg";
String path = sc.getRealPath(File.separator)+fileName;

File yourFile = new File(path);
FileOutputStream toFile = new FileOutputStream( yourFile );
DataInputStream fromClient = new DataInputStream( req.getInputStream() );

byte[] buff = new byte[1024];
int cnt = 0;
while( (cnt = fromClient.read( buff )) > -1 ) {
toFile.write( buff, 0, cnt );
}
toFile.flush();
toFile.close();
fromClient.close();

}
catch(Exception e)
{
e.printStackTrace();
}
}
}






//========================= www.karthikelango.in ===================================
 
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic