Servlets
- Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
WEB SERVER SERVLET ENGINE Servlet Web Browser File System Database
Servlets vs Traditional CGI Programs Servlets are efficient. No new process is started for each HTTP request. Servlets are convenient. Extensive Java infrastructure is already available. Servlets are powerful. It has full access to Java’s advanced features such as networking, security, database access, etc. Servlets are portable. Servlets are Secure. Servlets are inexpensive to implement.
Compiling Servlets Tomcat Standard Installation Directories –Standard location for servlets: Tomcat/webapps/ROOT/WEB-INF/classes –Alternate location for servlet classes: Tomcat/classes –Location of JAR files containing classes: Tomcat/lib To compile servlets: –javac ServletName.java
Servlet Events init() method destroy() method service() method –Request are handled in here. –Objects involved: Request object (implements the ServletRequest interface). Response object(implements the ServletResponse interface).
Servlet Generating Plain Text import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloSample extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { PrintWriter out = response.getWriter(); out.println("HELLO WORLD in PLAIN TEXT"); } /* Make sure you compile this in a path specified in the CLASSPATH such as C:\Tomcat\webapps\ROOT\Web-INF\classes */
A Web Page Invoking the Servlet Testing a Simple Servlet Click ME!
Servlet Generating an HTML Page import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloSample extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType(“text/html”); PrintWriter out = response.getWriter(); out.println(” \n ” + “SAMPLE HTML PAGE” \n” + “ \n” + “ HELLO HTML \n” + “ ”); }
package myServlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class RandomNum extends HttpServlet { private int [] numbers = new int[6]; public void init( ) throws ServletException { for(int i=0; i<numbers.length; i++) numbers[i] = (int) (Math.random() * 100); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(" \n Random Numbers" + " \n \n" + " LOTTERY NUMBERS ARE: \n "); for (int i=0; i < numbers.length; i++) out.println(" " + numbers[i]); out.println(" \n "); }
Form Data Handling GET - form data is attached to the end of the URL after the question mark. POST - form data is sent to the server on separate lines. Parsing this form data is handled by the getParameter( ) method of the HttpServletRequest. This method returns a String data type. String PersonName = Request.getParameter(“name”);
Servlet Handling Form Data import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FormDataSample extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType(“text/html”); PrintWriter out = response.getWriter(); out.println(” \n ” + “SAMPLE FORM DATA” \n” + “ \n” + “ HELLO “ + request.getParameter(“name”) + “ \n” + “ ”); }
The Web Page Using Form A Simple Form name:
ORDER PROCESSING COMPLETE TRANSACTION INFORMATION Part Number:   Quantity:   Unit Price($): First Name:   Last Name:   Middle Initial: Shipping Address: Credit Card:   Visa   Mastercard   Discover   American Express   BankCard   Credit Card number: Please let us know by checking the proper box:   Notification?   Wait for complete order before shipment?   Inform you of Promotions?
package myServlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; public class ProcessData extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter( ); out.println(" \n INPUT PARAMETERS" + " \n \n" + " PARAMETERS ARE: \n"); Enumeration PNames = request.getParameterNames( ); while (PNames.hasMoreElements( )) { String PNameVal = (String) PNames.nextElement( ); String PValues= request.getParameter(PNameVal); String TString = "CARDNUM"; if (PNameVal.equals(TString))out.println(PNameVal + " = ********* "); elseout.println(PNameVal + " = " + PValues + " "); }// end of while out.println(" \n "); }// end of doGet public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doGet(request, response); }