applet-servlet communication

hi,

i am doing a applet-servlet communication

my applet is sending name and number

i am able to sent the data from the applet but in the servlet i am not able to receive it

in my servlet i am using the following method

public void DoGet((HttpServletRequest req, HttpServletResponse res)

res.getParameter("name");

can some one tell me where am i going wrong ?

my applet source code is as follows

Applet Source

public void actionPerformed(ActionEvent e)

{

if (e.getSource()==newbutton)

{

try

{

Properties args = new Properties();

String name="hi";

String number="hello";

args.put("name", name);

args.put("number", number);

URL newurl = new URL("http://localhost/servlet/DecryptNew");

InputStream result = sendMessage(args,POST,newurl);

// textarea.append(result);

//URLConnection conn = newurl.openConnection();

textarea.append("gone");

AppletContext context = getAppletContext();

context.showDocument(newurl,"_top");

/* URLConnection connect;

connect = (new URL("http://192.168.3.13/keytools/servlet/DecryptNew").openConnection();

connect.setDefaultUseCaches(false); //for future connections

connect.setUseCaches(false); //for this one

connect.setDoInput(true);

connect.setDoOutput(false);

connect.connect();

DataInputStream in = new DataInputStream(connect.getInputStream());

String response = in.readLine();

*/

}

catch(Exception ae){}

//URL newurl=new URL("http://192.168.3.13/keytools/servlet/DecryptNew");

// newurl.openconnection();

}

}

public InputStream sendMessage(Properties args, int method,URL newurl)

throws IOException

{

textarea.append(" i am here 1");

// Set this up any way you want -- POST can be used for all calls,

// but request headers cannot be set in JDK 1.0.2 so the query

// string still must be used to pass arguments.

if (method == GET) {

URL url = new URL(newurl.toExternalForm() + "?" +

toEncodedString(args));

return url.openStream();

} else {

URLConnection conn = newurl.openConnection();

textarea.append(" i am here 2");

conn.setDoInput(true);

conn.setDoOutput(true);

conn.setUseCaches(false);

// Work around a Netscape bug

conn.setRequestProperty("Content-Type",

"application/x-www-form-urlencoded");

// POST the request data (html form encoded)

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

if (args != null && args.size() > 0) {

out.writeBytes(toEncodedString(args));

System.out.println("ServletMessage args: " + args);

System.out.println("ServletMessage toEncString args: " + toEncodedString(args));

}

out.flush();

out.close(); // ESSENTIAL for this to work!

// Read the POST response data

return conn.getInputStream();

}

}

public String toEncodedString(Properties args) {

StringBuffer sb = new StringBuffer();

if (args != null) {

String sep = "";

Enumeration names = args.propertyNames();

while (names.hasMoreElements()) {

String name = (String)names.nextElement();

sb.append(sep + URLEncoder.encode(name) + "=" +

URLEncoder.encode(args.getProperty(name)));

sep = "&";

}

}

return sb.toString();

}

[3583 byte] By [madhuvr] at [2007-9-26 3:47:16]
# 1
hi this should help u http://developer.java.sun.com/developer/qow/archive/53/index.htmlVinay
psvinayram at 2007-6-29 12:29:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Hello,Your code looks ok to me except for the name of the service method in the Servlet. The service method name for GET requests should be doGet and not DoGet.If you make that change, it should work.Kashabm
kashabm at 2007-6-29 12:29:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
[madhuvr],Like what [kashabm] said, you have a syntax error with doGet() method. Your posting shows DoGet() it should be doGet().HTH.Allen LaiDeveloper Technical SupportSUN Microsystems http://www.sun.com/developers/support
allenlai at 2007-6-29 12:29:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

hi ,

what you said is correct but that was just a spelling mistake of mine i am sorry for that

But my problem is my servlet is not accepting any value

my servlet code is as follows

public class DecryptNew extends HttpServlet

{

public void init(ServletConfig conf)

throws ServletException

{

super.init(conf);

}

public void doGet(HttpServletRequest req, HttpServletResponse res)

throws ServletException,IOException

{

res.setContentType("text/html");

try

{

String name = req.getParameter("name");

String number = req.getParameter("number");

//String name = (String)req.getAttribute("name");

out.println(name);

out.println("here ");

}

catch (Exception e)

{

System.out.println("Error: "+e);

}

}// end of doGet

public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException

{

doGet(req,res);

}// end of DoPost

}

But i am not able to print the values

what can be the problem?

madhuvr at 2007-6-29 12:29:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

[madhuvr],

You are missing the PrinterWriter object in your doGet() service method such as:

PrintWriter out = res.getWriter();

Try including this line in your doGet() code and let us know the results.

Allen Lai

Developer Technical Support

SUN Microsystems

http://www.sun.com/developers/support

allenlai at 2007-6-29 12:29:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

hi,

i used both PrintWriter object and ServletOutputStream

but no use

//PrintWriter out = res.getWriter();

// ServletOutputStream out = res.getOutputStream();

Now i think i am getting where my problem is

but still i am trying to get a solution for that

in my applet

i have added the following code to read the response from the servlet

URL newurl = new URL("http://localhost/servlet/DecryptNew");

InputStream result = sendMessage(args,POST,newurl);

// read Servlet Message

int chr = result.read();

StringBuffer sb = new StringBuffer();

while( chr != -1 ){

sb.append( String.valueOf((char)chr) );

chr = result.read();

}

String s=sb.toString();

textarea.append(s);

and when i print the String s it's printing me the correct value

that means my program is working very fine the only thing is i am not able to print it there in the servlet

Still i don't know y my servlet is not printing it

madhuvr at 2007-6-29 12:29:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

[madhuvr],

I'm now confused about what your objective is for the servlet.

What do you get on a HTML browser if you access the servlet from a HTML browser? Do you get the same values on the browser as the values you get in your applet?

Allen Lai

Developer Technical Support

SUN Microsystems

http://www.sun.com/developer/support/

allenlai at 2007-6-29 12:29:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

What do you mean by Servlet not printing? I do not see any System.out.prints in your Servlet code to print Strings name and "here". It looks to me that out is response outputStream and not System.out

Just add System.out.println( name ) and System.out.println( "here" ) lines to your servlet. You should see the output on your web server console.

Hope that helps.

Kashabm

kashabm at 2007-6-29 12:29:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

[madhuvr],

What does your Servlet with these 2 lines: System.out.println(name) and System.out.println("here") output onto your web server console?

These 2 lines should give you the values that your applet would have sent over to your Servlet.

Allen Lai

Developer Technical Support

SUN Microsystems

http://www.sun.com/developers/support/

allenlai at 2007-6-29 12:29:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...