• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

POST method

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to invoke the POST method of a servlet from an applet?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sarada Bhasker
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what difference will you make in the applet code if I want to invoke get instead of post method
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic