REMOTE METHOD INVOCATION
1. Assume that a user enters a URL to web browser. The browser then generates a HTTP request for this URL and sends it to the appropriate server
2. This HTTP request is received by the web server. The server maps this request to a particular servlet. It is dynamically retrieved and loaded into the address space of the server.
3. The server invokes the init() method of the servlet. This method is invoked only when the servlet is first loaded into memory.
4. The server invokes the servlet’s service() method, which is called to process the HTTP request.
5. The servlet remains in the server’s address space and is available to process any other HTTP requests received from clients.
6. Finally, the server may decide to unload the servlet from its memory. The server calls the destroy() method to relinquish any resources that are allocated for the servlet.
Algorithm:
1. Start.
2. Create a Html page that gets the Employee id from the user.
3. Create a servlet file, which EmpServlet.java which extends the HTTPServlet.
4. Declare the necessary variables for the employee details.
5. In the doGet/doPost method of the servlet receive the employee id from the html page, using the request.getstring() method.
6. Establish a connection to the database using JDBC.
7. Create a PreparedStatement object and execute the query to retrieve the information’s from the database.
8. Declare a PrintWriter object, and print the details of the employee with it.
9. Stop the program execution.
Source code:
SERVLET // Employee.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<table width="350" height="196" border="0" align="center" cellpadding="0" cellspacing="0">
<form id="form1" name="form1" method="post" action="">
<tr>
<td width="350"><div align="center" class="style1">Employee Informations </div></td>
</tr>
<tr>
<td><div align="center"><strong><a href="Employee.html">Click here to Store Employee Informatiopn</a> </strong></div></td>
</tr>
<tr>
<td><div align="center"><strong><a href="http://localhost:8080/Mca/View">Click here to View Employee Information </a></strong></div></td>
</tr>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<table width="275" height="196" border="0" align="center" cellpadding="0" cellspacing="0">
<form id="form1" name="form1" method="post" action="http://localhost:8080/Mca/Reg">
<tr>
<td colspan="2"><div align="center" class="style1">Employee Informations </div></td>
</tr>
<tr>
<td width="100"><strong>Name </strong></td>
<td width="175">
<input name="Ename" type="text" id="Ename" /></td>
</tr>
<tr>
<td><strong>Address </strong></td>
<td>
<textarea name="Eadd" id="Eadd"></textarea></td>
</tr>
<tr>
<td><strong>Phone</strong></td>
<td>
<input name="Ephn" type="text" id="Ephn" /> </td>
</tr>
<tr>
<td colspan="2">
<div align="center">
<input type="submit" name="Submit2" value="Submit" />
<input type="reset" name="Reset" value="Reset" />
</div> </td>
public class ServletReg extends HttpServlet {
Connection dbcon;
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
// Establishing the connection with the database
System.out.println("Database driver not found");
System.out.println(e.toString());
// This program records the details in the Registration table
//---res.setContentType("text/html");
PrintWriter out=res.getWriter();
String Ename=req.getParameter("Ename");
String Eadd=req.getParameter("Eadd");
String Ephn=req.getParameter("Ephn");
// inserting the values in the registration table
//---int rows=0;
try {
PreparedStatement s = dbcon.prepareStatement("INSERT INTO Employee(Ename,Eadd,Ephn)VALUES(?,?,?)");
s.setString(1,Ename);
s.setString(2,Eadd);
s.setString(3,Ephn);
rows=s.executeUpdate();
}
System.out.println("Error inserting data in the registration table");
} else {
System.out.println("The values have been inserted in the table successfully");
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<div align="center">
<p>Employee Id
<input type="text" name="textfield" />
</p>
<form id="form1" name="form1" method="post" action="http://localhost:8080/Mca/View">
<input type="submit" name="Submit" value="Submit" />
</form>
<p> </p>
</div>
</body>
</html>
// ServletRet.java import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.*;
import java.sql.*;
public class ServletRet extends HttpServlet {
Connection dbcon;
ServletContext ctx;
public void init(ServletConfig cfg) {
ctx=cfg.getServletContext();
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
// Establishing the connection with the database
//---try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
dbcon = DriverManager.getConnection("jdbc:odbc:Mca", "Scott",
"tiger");
System.out.println("Connection established");
}
catch(ClassNotFoundException e) {
System.out.println("Database driver not found");
System.out.println(e.toString());
}
catch (Exception e) {
System.out.println(e.toString());
} // end catch
//---res.setContentType("text/html");
PrintWriter out=res.getWriter();
String batchno = req.getParameter("batchno");
String lesson = req.getParameter("lesson");
ctx.setAttribute("lesson",lesson);
ctx.setAttribute("batchno",batchno);
// Picking up the Questions from the table for the user
//---String Ename =new //---String(" ");
String Eadd = new String(" ");
String Ephn = new String(" ");
try {
PreparedStatement s=dbcon.prepareStatement("select Ename,Eadd,Ephn from Employee where Eid =(?)");
s.setString(1,id);
ResultSet r=s.executeQuery();
out.println("<html>");
out.println(" <head>");
out.println("<meta http-equiv='Content-Type' content='text/html;
charset=iso-8859-1' />");
out.println("<title>Untitled Document</title>");
out.println("<style type='text/css'>");
out.println("<!--");
out.println(".style1 {font-size: 24px}");
out.println("-->");
out.println("</style>");
out.println("</head>");
out.println("<body>");
out.println("<table width='275' height='196' border='0' align='center' cellpadding='0' cellspacing='0'>");
out.println("<tr>");
out.println("<td colspan='2'><div align='center' class='style1'>Employee Informations </div></td>");
out.println("</tr>");
while (r.next()) {
Ename = r.getString(1);
Eadd = r.getString(2);
Ephn = r.getString(3);
out.println("<tr>");
out.println("<td width='100'><strong>Name </strong></td>");
out.println("<td width='175'>"+Ename+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td><strong>Address </strong></td>");
out.println("<td>"+Eadd+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td><strong>Phone</strong></td>");
out.println("<td>"+Ephn+"</td>");
out.println("</tr>");
} out.println("</table>");
out.println("</body>");
out.println("</html>");
}
catch(Exception e) {
System.out.println(e.toString());
} try {
dbcon.close();
}
catch(Exception e) {
System.out.println(e.toString());
} }
}
Output:
D:\java\bin\javac ServletRet.java D:\java\bin\javac ServletReg.java