370 likes | 387 Views
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.
E N D
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
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
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
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..
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
SERVLET • Web server cannot send dynamic page • Helper application servlet
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
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
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
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
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
SERVLET • When implement servlet implement with get and post method • doget() -> takes parameters from HttpServletRequest & HttpServletResponse • dopost() ->takes parameters from HttpServletRequest & HttpServletResponse
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
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
SERVLET • get methos default faster limited data • Information can see in URL • post method after urlnot with url as separate • Cannot see, unlimited data
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
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
SERVLET Tomcat Webapps http://localhost:8080/
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>"); • } • }
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>
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>"); • } • }
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
Session Tracking The different methods to achieve session tracking are • Hidden fields • URL rewriting • Cookies • Session tracking API
COOKIES • Cookies are the mostly used technology for session tracking.
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
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");