This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Applets and the fly likes Sending data from applet to servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Sending data from applet to servlet" Watch "Sending data from applet to servlet" New topic
Author

Sending data from applet to servlet

Shamim Emon
Greenhorn

Joined: Jun 16, 2013
Posts: 8
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
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41091
    
  44
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.


Ping & DNS - my free Android networking tools app
Shamim Emon
Greenhorn

Joined: Jun 16, 2013
Posts: 8
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
Marshal

Joined: Mar 22, 2005
Posts: 41091
    
  44
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

Joined: Jun 16, 2013
Posts: 8

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

Joined: Jun 16, 2013
Posts: 8
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
Marshal

Joined: Mar 22, 2005
Posts: 41091
    
  44
"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

Joined: Jun 16, 2013
Posts: 8


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
karthik elango
Greenhorn

Joined: Aug 28, 2013
Posts: 1
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 ===================================
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sending data from applet to servlet
 
Similar Threads
HTTP Status 404 - simple Custom Cookie from HF book
send post request with HttpClient and use servlet to display the request
Trying to use AJAX to call a servlet
Calling DB onSelectRadio and populating another table in the same JSP page
How to POST data to a form