Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, Responds oriented other.

Similar presentations


Presentation on theme: "CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, Responds oriented other."— Presentation transcript:

1 CSI 3125, Preliminaries, page 1 SERVLET

2 CSI 3125, Preliminaries, page 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 CSI 3125, Preliminaries, page 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 CSI 3125, Preliminaries, page 4 Request & Responds model

5 CSI 3125, Preliminaries, page 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 CSI 3125, Preliminaries, 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 CSI 3125, Preliminaries, page 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 CSI 3125, Preliminaries, page 8 SERVLET Web server cannot send dynamic page Helper application  servlet

9 CSI 3125, Preliminaries, page 9 SERVLET

10 CSI 3125, Preliminaries, page 10 Web container

11 CSI 3125, Preliminaries, page 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 CSI 3125, Preliminaries, page 12 How container Handle the request

13 CSI 3125, Preliminaries, page 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 CSI 3125, Preliminaries, page 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 CSI 3125, Preliminaries, page 15 How container knows which

16 CSI 3125, Preliminaries, page 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 CSI 3125, Preliminaries, page 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 CSI 3125, Preliminaries, page 18 SERVLET When implement servlet implement with get and post method doget() -> takes parameters from HttpServletRequest & HttpServletResponse dopost() ->takes parameters from HttpServletRequest & HttpServletResponse

19 CSI 3125, Preliminaries, page 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 CSI 3125, Preliminaries, page 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 CSI 3125, Preliminaries, page 21 SERVLET get methos  default faster limited data Information can see in URL post method  after url not with url as separate Cannot see, unlimited data

22 CSI 3125, Preliminaries, page 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 CSI 3125, Preliminaries, page 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 CSI 3125, Preliminaries, page 24 SERVLET Tomcat Webapps http://localhost:8080/

25 CSI 3125, Preliminaries, page 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(" "); out.println(" Hello World! "); out.println(" "); out.println(" Hello World! "); out.println(" "); }

26 CSI 3125, Preliminaries, page 26 Prog 2 html file Please enter your name:

27 CSI 3125, Preliminaries, page 27 Prog 2 java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); ServletOutputStream out = response.getOutputStream(); String userName = request.getParameter("user_name"); out.println(" "); out.println("\t Hello Servlet "); out.println(" "); out.println("\t Hello, " + userName + " "); out.println(" "); }

28 CSI 3125, Preliminaries, page 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. ie there is a need to maintain the conversational state. This is done by session tracking

29 CSI 3125, Preliminaries, page 29 Session Tracking The different methods to achieve session tracking are Hidden fields  URL rewriting  Cookies  Session tracking API

30 CSI 3125, Preliminaries, page 30 COOKIES Cookies are the mostly used technology for session tracking.

31 CSI 3125, Preliminaries, page 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 CSI 3125, Preliminaries, page 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 CSI 3125, Preliminaries, page 33 Session Tracking

34 CSI 3125, Preliminaries, page 34 Session Tracking

35 CSI 3125, Preliminaries, page 35 Session Tracking

36 CSI 3125, Preliminaries, page 36 Session Tracking

37 CSI 3125, Preliminaries, page 37 Session Tracking


Download ppt "CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, Responds oriented other."

Similar presentations


Ads by Google