Advanced Java Programming
Advanced Java Programming
Assistant Professor
Computer Engineering,
Sandip Institute of Polytechnic, Nashik
Contents
Introduction to Servlets
Types of Servlets
Generic Servlets
HTTP Servlets
Servlet LifeCycle
Servlet Redirection
CGI?
Performance is Better
Execute within Server Space
No need to create separate process
Platform independent as Java compiled
Supports several Web Browsers
JSM implements security constraints to protect
resources on Server Machine
All Java Libraries are available in Servlet
Types of Servlet
Servlets
1.Init()
2.Service()
3.Destroy()
Servlet Lifecycle
putValue( )
getValue( )
getValueNames( )
removeValue( )
1. import java.io.*;
2. import java.util.*;
3. import javax.servlet.*;
4. import javax.servlet.http.*;
5. public class DateServlet extends HttpServlet {
6. public void doGet(HttpServletRequest request, HttpServletResponse response)
7. throws ServletException, IOException {
8. HttpSession hs = request.getSession(true);
9. response.setContentType("text/html");
10. PrintWriter pw = response.getWriter();
11. pw.print("<B>");
12. Date date = (Date)hs.getValue("date");
13. if(date != null) {
14. pw.print("Last access: " + date + "<br>");
15. }
16. date = new Date();
17. hs.putValue("date", date);
18. pw.println("Current date: " + date);
19. }
Servlet Redirection
It is based on the status code of headers
Document moved
Load balancing
randomization
setHeader(“Location=www.google.com”);
Servlet chaining
Generally one servlet is used to create web page