1 / 37

SERVLET

Learn all about servlets, server-side programs that handle client requests efficiently, compared to other methods like ASP or CGI. Servlets are platform-independent and capable of handling various types of requests, making them a versatile solution. Discover the Request & Respond model, HTTP methods, and Response methods, and how servlets work within web servers. Also explore the role of Servlet Containers in managing dynamic pages and facilitating communication between web servers and servlets.

antoinettew
Download Presentation

SERVLET

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SERVLET

  2. SERVLET • A servlet is a server-side software program, Responds oriented • other server side prog GGI (perl) ASP (Microsoft) CGI -> every client request treat as separate process it takes time & resourses Asp-> every client request as separate thread It takes less time Servlet -> same as ASP Diff-> platform independent & can handle any type of request Does not depend on any protocol

  3. Request & Responds model • Client request some recourse to server • Server servers the request • server responds to the client request and return static page not dynamic • Client request through the web -> (http request) • http -> hyper text transfer protocol

  4. Request & Responds model

  5. Web server • Client request for a page in the web server • Server having set of static pages • This static page (html) sends to client again http response • Web server is a machine which can take client http request and response to client as static page

  6. HTT Request Methods • HTTP method • Contains the parameters that send to the server • It basically get or post • Form parameters -> http request contains the form parameters such as username, password..

  7. HTTP Response methods • A Status code method -> from server to client that the request is successful or not • Ie the requested page is found or not • If not send error page • Content type(text/img..) • Content type method-> if the requested page is found in the server then specify what type of content will send to client • It may be text/ img/jar file…. Content -> final data

  8. SERVLET • Web server cannot send dynamic page • Helper application  servlet

  9. SERVLET

  10. Web container

  11. Web container • Client request to web server • Webserver take help of servlet (java prog) • Webserver communicate with the servlet through web container (servlet engine) • Servlet engine responsible for invoking all methods on servlet • Container knows which method to call for that corresponding request in the servlet

  12. How container Handle the request

  13. How container Handle the request • Container responsibility to send the request to servlet, allow the servlet to create dynamic page and return back to the webserver • Request send by the client is http • Reponses to the client also http • Communication between webserver and container also http

  14. How container Handle the request • Servlet can understand only object (java code) • Container translate the http request to valid request object • If the request is get then container invoke the doGet() other wise doPost() • With in the doGet() can write the code • Finally java object response to the container • Container converts the object reponds to webserver

  15. How container knows which

  16. SERVLET • Package javax.servlet.x And Javax.servlet.http.x • Need a servlet public class extends HttpServlet( generic servlet class) • Servlet life cyle • Init() -> service()->destroy() (same of applet) • Init() and destroy() execute only once • Service() execute every client request

  17. SERVLET • Service() contain the logic for processing the client request and responds to it • All these methods from GenericServlet class (not specific any protocol) • Servlet does not have User Interface • JVM create an intense of Servlet class • Init() -> execute one time (connect database)Or initialization • Destroy()-> execute when not using for some time automatically destroy

  18. SERVLET • When implement servlet implement with get and post method • doget() -> takes parameters from HttpServletRequest & HttpServletResponse • dopost() ->takes parameters from HttpServletRequest & HttpServletResponse

  19. SERVLET • Service() from GenericServlet -> takes parameter from ServletRequest & ServletResponse • When client request the form service() invoked at the container first • All these are implemented by the container

  20. SERVLET • Container create objects to all methods all the methods • The request object holds the request details • Like where get or post method is used for request, what type of Brower • Response object contains all the details of data the sent back

  21. SERVLET • get methos default faster limited data • Information can see in URL • post method  after urlnot with url as separate • Cannot see, unlimited data

  22. SERVLET • the request will sent to servlet container • The service() in GericServlet • Is overrided in HttpServlet • With request and responds • getMethod()  returns the getrequest • postMethod() return the postrequest

  23. SERVLET • Prog • Specify What type of data sent back to the client (content type) If a text text/plane img image/jpg Html text/html

  24. SERVLET Tomcat Webapps http://localhost:8080/

  25. Prog • import java.io.*; • import javax.servlet.*; • import javax.servlet.http.*; • public class HelloWorld extends HttpServlet { • public void doGet(HttpServletRequest request, HttpServletResponse response) • throws IOException, ServletException • { • response.setContentType("text/html"); • PrintWriter out = response.getWriter(); • out.println("<html>"); • out.println("<head>"); • out.println("<title>Hello World!</title>"); • out.println("</head>"); • out.println("<body>"); • out.println("<h1>Hello World!</h1>"); • out.println("</body>"); • out.println("</html>"); • } • }

  26. Prog 2 html file • <html><body> • <form method="GET" action="HelloServlet"> • Please enter your name: • <input type="text" name="user_name"> • <input type="submit" value="OK"> • </form>

  27. Prog 2 java • import java.io.*; • import javax.servlet.*; • import javax.servlet.http.*; • public class HelloServlet extends HttpServlet • { • public void doGet(HttpServletRequest request, • HttpServletResponseresponse) • throws ServletException, IOException • { • 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>"); • } • }

  28. Session Tracking HTTP is a stateless protocol, where information is not automatically saved between HTTP requests. There are many situations in which we need to shared data between the servlets which are successively invoked during a session. iethere is a need to maintain the conversational state. This is done by session tracking

  29. Session Tracking The different methods to achieve session tracking are • Hidden fields •  URL rewriting •  Cookies •  Session tracking API

  30. COOKIES • Cookies are the mostly used technology for session tracking.

  31. Session Tracking Java Servlets send cookies to clients by adding fields to their HTTP response headers. Similarly, clients return cookies to servers by adding fields to HTTP request headers. When a client application (e.g., a Web browser) receives a cookie from a web server, the client application stores the cookie locally. A single browser can store 300 cookies where size of each cookie is limited to 4KB

  32. Session Tracking • THE COOKIE CLASS • A Java cookie is an object of the javax.servlet.http.Cookie class. • Cookies are created with the Cookie constructor. • Cookie(String name, String value) • Here, the name and value of the cookie are • Eg • Cookie cookie = new Cookie("CName","CookieValue");

  33. Session Tracking

  34. Session Tracking

  35. Session Tracking

  36. Session Tracking

  37. Session Tracking

More Related