Java Servlet
Java Servlet
Servlet
What is a Servlet?
• Servlet is a technology i.e. used to create web application.
• Servlet is an API that provides many interfaces and classes
including documentations.
• Servlet is an interface that must be implemented for creating any
servlet.
• Servlet is a class that extends the capabilities of the servers and
responds to the incoming requests. It can respond to any type of
requests.
• Servlet is a web component that is deployed on the server to create
dynamic web page.
Server-side Programming
• The combination of
• HTML
• JavaScript
• DOM
is sometimes referred to as Dynamic HTML
(DHTML)
• Web pages that include scripting are often
called dynamic pages (vs. static)
Server-side Programming
• Similarly, web server response can be static or
dynamic
• Static: HTML document is retrieved from the file system
and returned to the client
• Dynamic: HTML document is generated by a program in
response to an HTTP request
• Java servlets are one technology for producing
dynamic server responses
• Servlet is a class instantiated by the server to produce a
dynamic response
CGI(Commmon Gateway Interface)
1) In case of Get request, only limited amount In case of post request, large amount of
of data can be sent because data is sent in data can be sent because data is sent in
header. body.
2) Get request is not secured because data is Post request is secured because data is
exposed in URL bar. not exposed in URL bar.
3) Get request can be bookmarked. Post request cannot be bookmarked.
4) Get request is idempotent. It means second Post request is non-idempotent.
request will be ignored until response of first
request is delivered
5) Get request is more efficient and used more Post request is less efficient and used
than Post. less than get.
Session Tracking
• HTTP is a stateless protocol.
• Sessions provide storage of session information.
• A session can be created via the getSession( ) method of
HttpServletRequest.
• An HttpSession object is returned. This object can store a set of
bindings that associate names with objects.
• The setAttribute( ), getAttribute( ), getAttributeNames( ), and
removeAttribute( ) methods of HttpSession manage these bindings.
• It is important to note that session state is shared among all the
servlets that are associated with a particular client.
• import java.io.*;
• import java.util.*;
• import javax.servlet.*;
Session •
•
import javax.servlet.http.*;
public class DateServlet extends HttpServlet
{
Tracking
•
• public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
• {
• // Get the HttpSession object.
• HttpSession hs = request.getSession(true);
• // Get writer.
• response.setContentType("text/html");
• PrintWriter pw = response.getWriter();
• pw.print("<B>");
• // Display date/time of last access.
• Date date = (Date)hs.getAttribute("date");
• if(date != null)
• {
• pw.print("Last access: " + date + "<br>"); }
• // Display current date/time.
• date = new Date();
• hs.setAttribute("date", date);
• pw.println("Current date: " + date);
• }
• }
• Thank You