Java Servlets 110317050338 Phpapp01 PDF
Java Servlets 110317050338 Phpapp01 PDF
u Servlets
u Provide a general framework for services built on the request-
response paradigm
u Portable to any Java application server
u Have access to the entire family of Java and Java EE APIs
u Power
u Can take advantage of all Java APIs
u Elegance
u Simplicity due to abstraction
u Efficiency & Endurance
u Highly scalable
Why
u Safety
Use Servlets? (2)
u Strong type-checking
u Memory management
u Integration
u Servlets tightly coupled with server
u Extensibility & Flexibility
u Servletsdesigned to be easily extensible, though
currently optimized for HTTP uses
u Flexible invocation of servlet (SSI, servlet-chaining,
filters, etc.)
Time Servlet – Example
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
HelloServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
response.setContentType("text/html");
ServletOutputStream out = response.getOutputStream();
String userName = request.getParameter("user_name");
out.println("<html><head>");
out.println("\t<title>Hello Servlet</title>");
out.println("</head><body>");
out.println("\t<h1>Hello, " + userName + "</h1>");
out.println("</body></html>");
}
Creating The Form in Eclipse
IDE
HTTP/1.1 200 OK
Content-Length: 100
Date: Fri, 26 Mar 2006 10:06:28 GMT
Server: Apache-Coyote/1.1
<html><head>
<title>Hello Servlet</title>
</head><body>
<h1>Hello, Nakov</h1>
</body></html>
Image Counter Servlet
session.removeAttribute("name");
u Objects in the session can be
removed when not needed more
Getting Additional Session
Information
public String getId();
u Getting the unique session ID associated
with this user, e.g. gj9xswvw9p
u Checking
public when the session was first
long getCreationTime();
created
u Checking
public when the session was last active
long getLastAccessedTime();
Session Timeout
<html>
<head>
<title>Error</title>
</head>
<body>
<h1>Invalid login!</h1>
Please <a href="LoginForm.html">try again</a>.
</body>
</html>
The Browser's Cache
Problems
response.setHeader("Pragma", "No-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-cache");
1. Problems
Create a servlet that prints in a table the
numbers from 1 to 1000 and their square root.
2. Create a servlet that takes as parameters two
integer numbers and calculates their sum.
Create a HTML form that invokes the servlet. Try
to use GET and POST methods.
3. Implement a servlet that plays the "Number
guess game". When the client first invoke the
servlet it generates a random number in the
range [1..100]. The user is asked to guess this
number. At each guess the servlet says only
"greater" or "smaller". The game ends when the
user tell the number.
1. Homework
Create a servlet that takes as a parameter a
number and displays it as image that is hard to
be recognized by OCR software. The image
should have intentionally inserted defects.
2. Create an HTML form and a servlet for
performing conversions of distances from one
metric to another. The metrics that should be
supported are: meter, centimeter, kilometer,
foot, inch, yard, mile.
1 cm = 0.01 meters 1 km = 1000 meters
1 foot = 0.3048 meters 1 inch = 0.0254 meters
1 yard = 0.9144 meters 1 mile = 1609.344 meters
3. Homework
Create (2) of HTML forms and servlets
a sequence
that allow entering information about a student.
The information is entered in 3 steps in 3 separate
forms:
Step 1: First name, last name, age
Step 2: Address (country, town, street)
Step 3: University, faculty, specialty
The data entered in the 3 steps should be stored
in the session and finally displayed.
4. Create a servlet that reads an image (from WEB-
INF\img\logo.gif) and returns it.