aspose file tools*
The moose likes Servlets and the fly likes Post to Servlet from Applet doesn't work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Post to Servlet from Applet doesn Watch "Post to Servlet from Applet doesn New topic
Author

Post to Servlet from Applet doesn't work

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
I'm trying to set up an Applet to send an ArrayList of objects to a servlet.
Though the code works fine in local debug mode (locally), when I execute the code below from the applet to the Servlet, nothing happens in the servlet.
I click the button which "should" post the data from the Applet, but no XML file is created. I've put various messageboxes in the servlet code below, but ***none of them fires***.
Perhaps this is a Tomcat setup issue, but I'm not sure...I get no error screens. I just remain on the applet after clicking the "post" button with no files created.
Thanks for anybody's advice or ideas.
-- Mike
Applet code that sets up a call to the servlet:
.
.
.
ObjectOutputStream outputToServlet;
try
{
// finally, create the URL.
URL dataURL = new URL(
"http://localhost:8081/servlet/tp.HomeServlet");

URLConnection servletConnection = dataURL.openConnection();
// inform the connection that we will send output and accept input
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);
// Don't use a cached version of URL connection.
servletConnection.setUseCaches (false);
servletConnection.setDefaultUseCaches (false);
// Specify the content type that we will send binary data
servletConnection.setRequestProperty(
"Content-Type", "application/x-java-serialized-object");
// send the Pilates ArrayList object to the servlet using serialization
outputToServlet = new ObjectOutputStream(
servletConnection.getOutputStream());
// serialize the object
// finalClassSchedule is an ArrayList of objects.
outputToServlet.writeObject(finalClassSchedule);
outputToServlet.flush();
outputToServlet.close();
}
catch (Exception mfe)
{
mfe.printStackTrace();
}

Servlet code that receives this call:
public class HomeServlet extends HttpServlet implements Serializable
{
static final private String CONTENT_TYPE ="application/x-java-serialized-object";
ObjectInputStream inputFromApplet = null;
ArrayList classSchedule = null;
//Initialize global variables
public void init() throws ServletException
{
JOptionPane.showMessageDialog(null, "inside servlet init."); //never is seen
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try
{
response.setContentType(CONTENT_TYPE);
// get an input stream from the applet
inputFromApplet = new ObjectInputStream(request.getInputStream());
JOptionPane.showMessageDialog(null, "inside get in servlet"); // never is seen
ObjectInputStream appletIn =
new ObjectInputStream(request.getInputStream());
classSchedule = (ArrayList) appletIn.readObject();
appletIn.close(); // close stream.
// write data to createXML
CreateXML xml = new CreateXML("finalXMLFile.xml",
classSchedule,
"schedule.xsl");
}
catch (Exception e)
{JOptionPane.showMessageDialog(null, "Error in servlet Get");}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doGet(request, response);
}
//Clean up resources
public void destroy()
{
}
}
Graham Thorpe
Ranch Hand

Joined: Mar 25, 2002
Posts: 265
Try to put service method

public void service(HttpServletRequest *,....)
{
}
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
Thanks for your reply.
According to Marty Hall (author of Core Servlets and JavaServer Pages), it's better to just have doGet() call doPost() or vice versa rather than overriding service.
My code has doPost() call doGet().
I tried to override the service method, but it, unfortunately, made no difference...
My problem seems to be that the writeObject() method in the applet (after I've connected to the URL of the servlet) isn't somehow connecting with the servlet.
Any additional ideas would be appreciated.
-- Mike
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
Here's a simpler example of the code that won't work.
Again, here the applet seems not to be connecting the servlet. However, I verified the servlet worked from the browser before changing its code. That is, the port assignment is OK.
Also, the getInputStream() line is the last line that executes in the Applet.
=============================
Applet code:
=============================
package testapplettoservlet;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.servlet.http.*;
import java.io.*;
import java.net.*; // for URL class
public class Applet1 extends Applet implements Serializable
{
private boolean isStandalone = false;
private JButton cmdSend = new JButton();
private JLabel lblStatus = new JLabel();
//Get a parameter value
public String getParameter(String key, String def)
{
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
//Construct the applet
public Applet1()
{
}
//Initialize the applet
public void init()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception
{
cmdSend.setBounds(new Rectangle(155, 37, 67, 27));
cmdSend.setForeground(Color.blue);
cmdSend.setText("Send!");
cmdSend.addActionListener(new Applet1_cmdSend_actionAdapter(this));
lblStatus.setText("Waiting....");
lblStatus.setBounds(new Rectangle(43, 88, 292, 17));
this.setLayout(null);
this.add(lblStatus, null);
this.add(cmdSend, null);
}
//Get Applet information
public String getAppletInfo()
{
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo()
{
return null;
}
void cmdSend_actionPerformed(ActionEvent e)
{
ObjectOutputStream outputToServlet;
try
{
// finally, create the URL.
URL dataURL = new URL("http://localhost:8081/servlet/testapplettoservlet.Servlet1");
// URL dataURL = new URL(protocol, host, port, urlSuffix);
URLConnection connection = dataURL.openConnection();
if (connection == null)
{
JOptionPane.showMessageDialog(null, "No URL connection established!");
return;
}
// inform the connection that we will send output and accept input
connection.setDoInput(true);
connection.setDoOutput(true);
// Don't use a cached version of URL connection.
connection.setUseCaches (false);
connection.setDefaultUseCaches (false);
String stringToSend = "Hello from Applet!";
// Specify the content type that we will send binary data
// not necessary when sending serialized objects as below.
connection.setRequestProperty(
"Content-Type", "application/x-java-serialized-object");

// send the Pilates ArrayList object to the servlet using serialization
outputToServlet = new ObjectOutputStream(
connection.getOutputStream());
// serialize the object
if (stringToSend != null)
{
outputToServlet.writeObject(stringToSend);
}
else
{
JOptionPane.showMessageDialog(null, "No Message to Send.");
}
ObjectInputStream in = new ObjectInputStream(connection.getInputStream());
// try to read servlet's response
String line;
this.lblStatus.setText("");
line = in.readLine();
lblStatus.setText(line);
outputToServlet.flush();
outputToServlet.close();
}
catch (Exception mfe)
{
JOptionPane.showMessageDialog(null, mfe.getMessage());
}
}
}
class Applet1_cmdSend_actionAdapter implements java.awt.event.ActionListener
{
private Applet1 adaptee;
Applet1_cmdSend_actionAdapter(Applet1 adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.cmdSend_actionPerformed(e);
}
}
============================================
Servlet code:
============================================
package testapplettoservlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class Servlet1 extends HttpServlet
{
static final private String CONTENT_TYPE =
"application/x-java-serialized-object";
//Initialize global variables
public void init() throws ServletException
{
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
/*
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Servlet1</title></head>");
out.println("<body>");
out.println("<p>The servlet has received a GET. This is the reply.</p>");
out.println("</body></html>");
*/
{
ObjectInputStream inputFromApplet = null;
// this.gotoPage("/webcalendar/Register.jsp", request, response);
try
{
System.out.println("Inside doGet() in Servlet!");
// get an input stream from the applet
inputFromApplet = new ObjectInputStream(request.getInputStream());
String inString = (String) inputFromApplet.readObject();
// write back to applet
response.setContentType(CONTENT_TYPE);
ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());
String reply = "cool, you did it!";
out.writeObject(reply);
out.flush();
out.close();
inputFromApplet.close(); // close streams.
}
catch (Exception e)
{}
}

}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doGet(request, response);
}
//Clean up resources
public void destroy()
{
}
}
 
 
subject: Post to Servlet from Applet doesn't work