No Slide Title

Download Report

Transcript No Slide Title

Servlets
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
File System
Database
Web Browser
Servlet
Servlet
Servlet
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
<html>
<head>
<title>Testing a Simple Servlet</title>
</head>
<body >
<p><H1><a href="/servlet/HelloSample">
Click ME!</a></H1>
</body>
</html>
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(”<HTML>\n <HEAD> <TITLE>” +
“SAMPLE HTML PAGE” </HEAD>\n” +
“<BODY> \n” + “<H1> HELLO HTML</H1>\n” +
“</BODY> </HTML>”);
}
}
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("<HTML>\n<HEAD><TITLE>Random Numbers" +
"</TITLE></HEAD>\n <BODY>\n" +
"<H1> LOTTERY NUMBERS ARE: </H1>\n<OL>");
for (int i=0; i < numbers.length; i++)
out.println("<LI>" + numbers[i]);
out.println("</OL> \n </BODY></HTML>");
}
}
Form Data Handling
GET - form data is attached to the end of the URL after the
question mark.
http://process.cgi?name=john+doe&date=12-20-99
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(”<HTML>\n <HEAD> <TITLE>” +
“SAMPLE FORM DATA” </HEAD>\n” +
“<BODY> \n” + “<H1> HELLO “ +
request.getParameter(“name”) + “</H1>\n” +
“</BODY> </HTML>”);
}
}
The Web Page Using Form
<html>
<head>
<title>A Simple Form</title>
</head>
<body >
<form action=“/servlet/FormDataSample”>
<center> name:<input type=“TEXT” name=“name”>
<br>
<input type=“submit” >
</center>
</form> </body>
</html>
<HTML>
<HEAD><TITLE> ORDER PROCESSING</TITLE></HEAD>
<BODY BGCOLOR="lightsalmon">
<H1 align="center"> COMPLETE TRANSACTION INFORMATION </H1><br>
<Form action="/servlet/myServlets.ProcessData" METHOD="Post">
Part Number: <select name="ONUM">
<option value="1010"> 101010
<option value="2020"> 202020
<option value="3030"> 303030
<option value="4040"> 404040
</select>
&nbsp; &nbsp Quantity: <input type="text" name="QTY">
&nbsp Unit Price($): <input type="text" name="PRICE"><br>
<HR> First Name: <input type="text" name="FNAME"> &nbsp
Last Name: <input type="text" name="LNAME"> &nbsp Middle Initial: <input type="text" name="MNAME"><br>
<br>Shipping Address: <textarea name="ADDRESS" ROWS=3 COLS=30> </Textarea><br><br>
Credit Card: <br> &nbsp; &nbsp
<input type="radio" name="card" value="V"> Visa &nbsp
<input type="radio" name="card" value="M"> Mastercard &nbsp
<input type="radio" name="card" value="D"> Discover &nbsp
<input type="radio" name="card" value="A"> American Express &nbsp
<input type="radio" name="card" value="B"> BankCard &nbsp <br>
Credit Card number: <input type="password" name="CARDNUM"><br>
<Center> <input type="SUBMIT" value="SUBMIT">
<input type="RESET" value="CLEAR">
</Center><HR>
Please let us know by checking the proper box: <br>
&nbsp <input type="checkbox" name="CHKEMAIL"> Email Notification? <br>
&nbsp <input type="checkbox" name="CHKWAIT"> Wait for complete order before shipment?<br>
&nbsp <input type="checkbox" name="CHKPROMO"> Inform you of Promotions? <br>
</form>
</body>
</html>
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("<HTML>\n<HEAD><TITLE>INPUT PARAMETERS" + "</TITLE></HEAD>\n <BODY>\n" +
"<H1> PARAMETERS ARE: </H1>\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 + " = ********* <BR>");
else
out.println(PNameVal + " = " + PValues + "<BR>");
}// end of while
out.println(" \n </BODY></HTML>");
}// end of doGet
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
doGet(request, response);
}
}