Java

Java Servlets

An alternate form of server-side computation that uses Java.

The Web server is extended to support an API, and then Java programs use the API to create dynamic web pages.

Using Java servlets provides a platform-independent replacement for CGI scripts.

Servlets can be embedded in many different servers because the servlet API, which you use to write servlets, assumes nothing about the server’s environment or protocol.

Servlet Life Cycle:
Initialization: the servlet engine loads the servlet’s *.class file in the JVM memory space and initializes any objects

Execution: when a servlet request is made,
a ServletRequest object is sent with all information about the request a ServletResponse object is used to return the response.

Destruction: the servlet cleans up allocated resources and shuts down

Simple Servlet example to find records in Database

1. Create an HTML file with following syntax.

<html>
<head>
<title>
Find Employee Information
</title>
</head>
<body>
<form method="get" action="http://localhost:70/classes">
<h2 align=center>Find Employee Information<center></h2>
<table>
<tr>
<th>Enter Employee Id</th>
<td> <input type=text name="id"> </td>
</tr>
</table>
<input type=submit value=Submit>
</form>
</body>
</html>

2. Now, create a Java Servlet program with following code in it.

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class EmployeeDetails extends HttpServlet{
  static int i;
  Connection con;
  PrintWriter out;
  ResultSet rs;
  public void init(){
    i=0;
    con=null;
    out=null;
    rs=null;
  }


  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    i++;
    out=response.getWriter();
    out.println("<b>You are user number " + i + "to visit this site</b><br><br>");
    try{
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      con=DriverManager.getConnection("jdbc:odbc:test");
      PreparedStatement pstmt=null;
      String query=null;
      query="select fname,lname,address,salary from Emp_Det where id=?";
      pstmt=con.prepareStatement(query);
      pstmt.setInt(1,Integer.parseInt(request.getParameter("id")));
      rs=pstmt.executeQuery();
      out.println("<b><center>Employee Details</center></b><br><br>");
      ResultSetMetaData rsmd=rs.getMetaData();
      int colcount=rsmd.getColumnCount();
      out.println("<table align=center border=1 cellpadding=2>");
      out.println("<tr>");
      for(int i=1; i<=colcount; i++){
        out.println("<th>" + rsmd.getColumnLabel(i) + "</th>");
      }
      out.println("<tr>");
      while(rs.next()){
        out.println("<tr>");
        out.println("<td>" + rs.getString("fname") + "</td>");
        out.println("<td>" + rs.getString("lname") + "</td>");
        out.println("<td>" + rs.getString("address") + "</td>");
        out.println("<td>" + rs.getString("salary") + "</td>");
        out.println("</tr>");
      }
      out.println("</table>");
      out.println("</body>");
    }
    catch(Exception e){
      out.println(e.toString());
    }
  }
  public void destroy(){
    try{
      i=0;
      con.close();
      out.close();
      rs.close();
    }
    catch(SQLException se){
      out.println(se.toString());
    }
  }
}

Now, run the above HTML file in the browser from localhost and see the output.

SKB