Click to See Complete Forum and Search --> : Servlet/Applet


August 2nd, 1999, 05:43 PM
Hi,
I want to invoke an servlet from an applet and send some data(int value)back to the applet.How can I do this?.I'm not able to get the result back to the applet.
My code is as follows.....


import java.awt.*;
import java.io.*;
import java.net.*;


public class CountServletApplet extends java.applet.Applet
{
private TextField SessionField;
private Button SessionCount;
private Button GlobalCount;
private Button FileCount;



private URLConnection connect;
private URL u1;

String sum = "0";
public CountServletApplet()
{
setLayout(new FlowLayout());
add(new Label("Count"));
SessionCount = new Button("Session Count");
FileCount = new Button("File Count");
GlobalCount = new Button("GlobalCount");
SessionField = new TextField("1");
add(SessionCount);
add(GlobalCount);
add(FileCount);
add(SessionField);
setVisible(true);
setSize(600,600);
}
public boolean action(Event e, Object arg)
{
if(e.target == SessionCount)
{
try
{
System.out.println("Resetting Count..");
u1 = new URL(getDocumentBase(),"/servlet/CountServlet?operation=increment");
connect = u1.openConnection();
connect.setDefaultUseCaches(false);
connect.setUseCaches(false);
connect.setDoInput(true);
connect.setDoOutput(true);
connect.connect();
DataInputStream in = new DataInputStream(new BufferedInputStream(connect.getInputStream()));
String response = in.readLine();
System.out.println(response);
SessionField.setText(response);
in.close();
}
catch(Exception x)
{
System.out.println("System Exception " + x);
}
return true;
}
return false;
}

}


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

public class CountServlet extends HttpServlet
{
int count;

public void init(ServletConfig conf) throws ServletException
{
super.init(conf);
count = 0;
}

public void service(HttpServletRequest req, HttpServletResponse res)
{
try
{
String str;
str = req.getParameter("operation");

if(str.equals("increment"))
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
HttpSession session = req.getSession(true);
Integer count = (Integer)session.getValue("tracker.count");
if(count == null)
count = new Integer(1);
else
count = new Integer(count.intValue() + 1);
session.putValue("tracker.count",count);
ServletOutputStream out1 = res.getOutputStream();
res.setContentType("text/html");
out1.println(" " + count);
out1.close();
}

else if(str.equals("reset"))
{
count = 0;
ServletOutputStream out = res.getOutputStream();
res.setContentType("text/html");
out.println(" " + count);
out.close();
}
}
catch(Exception e)
{
System.out.println("Error" + e);
}
}
}

unicman
August 3rd, 1999, 12:18 AM
Have checked if the servlet is running fine? I can't check it here as, I can't run servlets on my machine.

Just in case, one way is to give the URL in ur browser 'http://yourhost/servlet/CountServlet?operation=increment'.
If u get some error page, it means there is some problem on server side.
If browser shows a blank page, view the source and see if u have got the count.
If u have got the count also, then server-side is correct.

In the applet try this code...

URL url = new URL(getDocumentBase(),"/servlet/CountServlet?operation=increment");
DataInputStream in = new DataInputStream(new BufferedInputStream(url.openStream()));

// read the contents in the stream




- UnicMan
http://members.tripod.com/unicman

Saleem Ansari
August 29th, 1999, 07:17 AM
Visit http://servlets.com and download COS.JAR package.
It contains lot of classes for sending messages/objects to and fro applet/servlet.

Regards,
saM