Invoke Servlets HTML Applet in Java Source Coding

Aim
To invoke servlets from Applet CS1404 INTERNET PROGRAMMING LABORATORY
Algorithm
Start the Invoking Servlets from Applet
Create the Ats.html
Use the one input type for roll no
When submit button is click it perform the action= http://localhost:8080/Student/Student
The save as the Ats.html
Then create java coding for AppletToServlet.java
In coding create three important objects
Label sent and received and button click to send to servlets.
Set the url as http://localhost:8080"+"/servlet/ServletToApple
Then create java coding for ServletToApplet.java and Intermediate.java (see the source code)
Finally save and run the program

Note important files are Ats.html ,Applettoservlet.java ,servlets to applet.java and Intermediate .java
Source code in Html invoking Servlets from Applets
<!—Ats.html --> CS1404 INTERNET PROGRAMMING LABORATORY
<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>

Source code in java programming Invoking Servlets from Applets –
AppletToServlet.java CS1404 INTERNET PROGRAMMING LABORATORY

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/ServletToApple
t");
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.getOutputStream());
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;
}
}


Source code in java programing Invoking Servlets from Applets –
ServletToApplet.java CS1404 INTERNET PROGRAMMING LABORATORY
import java.util.*;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class ServletToApplet extends HttpServlet
{
String valueGotFromApplet;
public void init(ServletConfig config) throws
ServletException
{
System.out.println("Servlet entered");
super.init();
}
public void service(HttpServletRequest request,
HttpServletResponse response) throws
ServletException, IOException
{
try
{
System.out.println("service entered");
ObjectInputStream ois=new
ObjectInputStream(request.getInputStream()
);
Intermediate ss=(Intermediate)ois.readObject();
valueGotFromApplet=ss.getFname();
System.out.println(valueGotFromApplet);
response.setContentType("application/octetstream");
ObjectOutputStream oos=new
ObjectOutputStream(response.getOutputStream());
oos.writeObject(ss);
oos.flush();
oos.close();
}
catch(Exception e){System.out.println(e);}
}
public String getServletInfo()
{
return "...";
}
}


Source code in java programing Invoking Servlets from Applets –
Intermediate.java CS1404 INTERNET PROGRAMMING LABORATORY

import java.io.*;
public class Intermediate implements Serializable{
String fname;
public String getFname(){
return fname;
}
public void setFname(String s){
fname=s;
}
}
D:\servlet\WEB-INF\classes>javac Intermediate.java
D:\servlet\WEB-INF\classes>javac AppletToServlet.java
D:\servlet\WEB-INF\classes>javac ServlettoApplet.java
D:\servlet\WEB-INF>type web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application
2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<!-- JSPC servlet mappings start -->
<servlet>
<servlet-name>ServletToApplet</servlet-name>
<servlet-class>ServletToApplet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletToApplet</servlet-name>
<url-pattern>/ServletToApplet</url-pattern>
</servlet-mapping>
<!-- JSPC servlet mappings end -->
</web-app>

D:\servlet>jar -cvf ServletToApplet.war .
added manifest
adding: ats.html(in = 97) (out= 76)(deflated 21%)
adding: WEB-INF/(in = 0) (out= 0)(stored 0%)
adding: WEB-INF/classes/(in = 0) (out= 0)(stored 0%)
adding: WEB-INF/classes/AppletToServlet.class(in = 3055) (out=
1627)(deflated 46
%)
adding: WEB-INF/classes/AppletToServlet.java(in = 1952) (out=
776)(deflated 60%)
adding: WEB-INF/classes/Intermediate.class(in = 433) (out=
273)(deflated 36%)
adding: WEB-INF/classes/Intermediate.java(in = 188) (out=
126)(deflated 32%)
adding: WEB-INF/classes/ServletToApplet.class(in = 1660) (out=
859)(deflated 48%
)
adding: WEB-INF/classes/ServletToApplet.java(in = 975) (out=
424)(deflated 56%)
adding: WEB-INF/web.xml(in = 698) (out= 315)(deflated 54%)
Step 1: Open Web Browser and type
Step 2: http://localhost:8080
Step 3: Select Tomcat Manager
Step 4: Deploy the war file and Run

OUTPUT of Servelet HTMl APPLET


Invoke Servlets HTML Applet in Java Source Coding

Related post