wood burning stoves 2.0*
The moose likes Applets and the fly likes POST method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "POST method" Watch "POST method" New topic
Author

POST method

Sarada Bhasker
Ranch Hand

Joined: Sep 11, 2000
Posts: 94
How to invoke the POST method of a servlet from an applet?
Selva P
Greenhorn

Joined: Oct 24, 2000
Posts: 11
The following programs show u how to invoke a post method in a servlet from an applet
servlet program
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class calldemo extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
String s = req.getParameter("name");
PrintWriter pw = res.getWriter();
pw.println("hello from post"+s);
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
String s = req.getParameter("name");
PrintWriter pw = res.getWriter();
pw.println("hello from get"+s);
}
}
Applet program
(run this using appletviewer since i have printed the response from the servlet in the dos prompt)

import java.applet.*;
import java.net.*;
import java.io.*;
public class call extends Applet {
public void init() {
try{
URL url = new URL("http://localhost:8080/servlet/calldemo?name=selva");
URLConnection uc = url.openConnection();
DataInputStream out = new DataInputStream(new BufferedInputStream(uc.getInputStream()));
System.out.println(out.readLine());
}catch(Exception e)
{}
}
}
//<applet code = call height = 400 width = 400></applet>

bye

Sun Certified Java 2 Programmer
Sarada Bhasker
Ranch Hand

Joined: Sep 11, 2000
Posts: 94
what difference will you make in the applet code if I want to invoke get instead of post method
Tanveer Rameez
Ranch Hand

Joined: Dec 11, 2000
Posts: 158
hi
in get you simle append the name and value pair to the address. for eg.
URL url=new URL(getDocumentBase(),"http://yrsite.com?name1=value1&name2=value2");
And you don't have to write anyhting in the body part of the request by obtaining the OutputStream of the URLConnect object. TO retrieve info from the servlet/asp page you use the InputStream


Author of JPhotoBrush Pro (www.jphotobrushpro.com)
 
wood burning stoves
 
subject: POST method
 
Similar Threads
What is the disadvantage of Post method?
How to cast a String to java.sql.types.CHAR
Array declaration in a class
what happens after you pass your parameters along with your submits
Hidding parameters