INVOKING SERVLET FROM HTML FORMS
<HTML>
<BODY>
<CENTER>
<FORM name = "postparam" method = “post"
action="http://localhost:8080/PostParam/PostParam">
<TABLE>
<tr>
<td><B>Employee </B> </td>
<td><input type = "textbox" name="ename" size="25" value=""></td>
</tr>
<tr>
<td><B>Phone </B> </td>
<td><input type = "textbox" name="phoneno" size="25"value=""></td>
</tr>
</TABLE>
<INPUT type = "submit" value="Submit">
</body>
</html>
INVOKING SERVLET FROM HTML FORMS
import java.io.*;
import java.util.*;
import javax.servlet.*;
public class PostParam extends GenericServlet
{
public void service(ServletRequest
request,ServletResponse response) throws
ServletException, IOException
{
PrintWriter pw = response.getWriter();
Enumeration e = request.getParameterNames();
while(e.hasMoreElements())
{
String pname = (String)e.nextElement();
pw.print(pname + " = ");
String pvalue = request.getParameter(pname);
pw.println(pvalue);
}
pw.close();
}
}
INVOKING SERVLET FROM APPLETS
<!—Ats.html -->
<HTML>
<BODY>
<CENTER>
<FORM name = "students" method = "post"
action="http://localhost:8080/Student/Student">
<TABLE>
<tr>
<td><B>Roll No. </B> </td>
<td><input type = "textbox" name="rollno" size="25" value=""></td>
</tr>
</TABLE>
<INPUT type = "submit" value="Submit">
</FORM>
<CENTER>
</BODY>
</HTML>
INVOKING SERVLET FROM APPLETS
AppletToServlet.java
import java.io.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.net.*;
/* <applet code="AppletToServlet" width="300"
height="300"></applet> */
public class AppletToServlet extends Applet implements ActionListener
{
TextField toSend;
TextField toGet;
Label l1;
Label l2;
Button send;
Intermediate s;
String value;
ObjectInputStream is;
ObjectOutputStream os;
public void init()
{
toSend=new TextField(10);
add(toSend);
toGet=new TextField(10);
add(toGet);
l1=new Label("value sent");
l2=new Label("value received");
add(l1);
add(l2);
send=new Button("Click to send to servlet");
send.addActionListener(this);
add(send);
validate();
s=new Intermediate();
}
public void actionPerformed(ActionEvent e)
{
value=toSend.getText();
sendToServlet();
}
public void sendToServlet()
{
try
{
URL url=new
URL("http://localhost:8080"+"/servlet/ServletToApplet");
URLConnection con=url.openConnection();
s.setFname(value);
writeStuff(con,s);
s=new Intermediate();
Intermediate p=readStuff(con);
if(p!=null)
{
value=p.getFname();
toGet.setText("value:"+value);
validate();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public void writeStuff(URLConnection connection, Intermediate value)
{
Try
{
connection.setUseCaches(false);
connection.setRequestProperty("CONTENT_TYPE”,
"application/octet-
stream");
connection.setDoInput(true);
connection.setDoOutput(true);
os=new ObjectOutputStream(connection.getOutputS tream());
os.writeObject(value);
os.flush();
os.close();
}
catch(Exception y){}
}
public Intermediate readStuff(URLConnection connection)
{
Intermediate s=null;
Try
{
is=new
ObjectInputStream(connection.getInputStream());
s=(Intermediate)is.readObject();
is.close();
}
catch(Exception e){}
return s;
}
}
INVOKING SERVLET FROM APPLETS
ServletToApplet.java
i mpor t j a va . uti l. *;
i mpor t j a va . i o. *;
i mpor t j a va x. s er vl et . ht t p. *;
i mpor t j a va x. s er vl et . *;
publi c cl a s s Se r vl e t ToAppl et e xt e nds Htt pSe r vl e t
{
St ri ng val ue Got Fr om Appl e t;
publi c voi d ini t( Se r vle t Conf i g c onfi g) t hr ows Ser vl et Exc e pti on
{
Sys t em . out . pri ntl n( "Se r vl e t e nt e r e d") ;
s upe r. i ni t () ;
}
publi c voi d s er vi ce ( Ht t pSer vl et Re que s t r e ques t , Htt pSe r vl e t Re s ponse re s pons e )
thr ows Ser vl et Exc e pti on, I OExc e pt i on
{
try
{
Sys t em . out . pri ntl n( " s er vi ce e nt e r e d") ;
Obj e c tI nput Str e am ois = ne w Obj e ct I nput St r e am ( re que s t. ge tI nput Str e a m( ) );
I nte r me di at e ss =( I nt e rm e di at e ) oi s .r e a dObj e ct ( );
va l ue Got Fr om Appl et = s s . ge t Fna me ( );
Sys t em . out . pri ntl n( val ue Got Fr om Apple t );
r e s pons e . se t Cont e nt Type ( " a ppl i c at i on/ oc t et - s tr e a m ") ;
Obj e c t Out put St re a m oos = ne w
Obj e c t Out put St re a m( r e s ponse . get Out put Str e a m( ) );
oos . wri t e Obj ec t (s s ) ;
oos .f l us h( ) ;
oos .c l os e( ) ;
}
c a tc h( Exc e pt i on e)
{
Sys t em . out . pri ntl n( e) ;
}
}
publi c Str i ng ge t Ser vl et I nf o( )
{
r et ur n ". .. ";
}
}
INVOKING SERVLET FROM APPLETS
// Intermediate.java
import java.io.*;
public class Intermediate implements Serializable
{
String fname;
public String getFname()
{
return fname;
}
public void setFname(String s)
{
fname=s;
}
}