You are on page 1of 4

INVOKING SERVLET FROM AN APPLET EX.NO:6b DATE :14.2.2012 AIM: To invoke a servlet from an applet ALGORITHM: 1. Start. 2.

Create two java files to instantiate a servlet and an applet. 3. In the applet file create a frame with two text fields, input, output and a send button. 4. In the servlet file create an instance of the HttpServletRequest and HttpServletResponse. 5. Using request object create an instance of the InputStream and retrieve the object using the getInputStream() function. 6. Similarly create an instance for the OutputStream. 7. Embed the applet code in the applet file. 8. Stop. PROGRAM CODING: //Echoserv.java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.ServletException; public class Echoserv extends HttpServlet { public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException { try { res.setContentType("application/x-java-serialized-object"); InputStream ip=req.getInputStream(); ObjectInputStream oi=new ObjectInputStream(ip); String result=(String)oi.readObject(); oi.close(); ip.close(); OutputStream o=res.getOutputStream(); ObjectOutputStream os=new ObjectOutputStream(o); os.writeObject(result); os.flush(); os.close(); }

} } //Appi.java

catch(Exception e) { e.printStackTrace(); }

import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import javax.swing.*; /*<applet code="Appi.class" width=600 height=600> * </applet>*/ public class Appi extends Applet implements ActionListener { String s=""; Panel p = new Panel(); TextArea ta; TextField ip = new TextField(); TextField op = new TextField(); public void init() { setSize(400,300); p.setSize(100,100); p.setLayout(new GridLayout(3,2)); p.setBackground(Color.WHITE); p.add(new Label("Input")); p.add(ip); p.add(new Label("Output")); p.add(op); addButton("Send"); ta=new TextArea(5,20); p.add(ta); add(p); } public void addButton(String s) { Button z=new Button(s); z.addActionListener(this); p.add(z); } public void actionPerformed(ActionEvent e) { s=e.getActionCommand(); if(s=="Send")

{ } } public void paint(Graphics g) { } private URLConnection getServletConnection() throws MalformedURLException, IOException { URL urlServlet = new URL("http://172.16.2.147:8080/cs2k9a42/servlet/hello"); URLConnection con = urlServlet.openConnection(); con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false); con.setRequestProperty ("Content-type","application/x-java-serializedobject"); return con; } private void onSendData() { try { String input = ip.getText(); URLConnection con = getServletConnection(); OutputStream os = con.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(os); oos.writeObject(input); oos.flush(); oos.close(); InputStream ins = con.getInputStream(); ObjectInputStream inpfs = new ObjectInputStream(ins); String result = (String)inpfs.readObject(); inpfs.close(); ins.close(); op.setText(result); } catch(Exception ex) { ex.printStackTrace(); ta.setText(ex.toString()); } } } onSendData();

SCREENSHOTS:

RESULT: Thus a servlet was invoked from an applet.