500 likes | 828 Views
This presentation on Java Servlets tutorial will help you with the fundementals of Java servlets and will also help you with a practial part to set up the environment servets and execute a practical program. <br><br>About Simplilearn Java certification training course:<br>If youu2019re looking to master web application development for virtually any computing platform, this Java Certification Training course is for you. This all-in-one Java training will give you a firm foundation in Java, the most commonly used programming language in software development.<br><br>This advanced Java Certification Training course is designed to guide you through the concepts of Java from introductory techniques to advanced programming skills. The course will provide you with the knowledge of Core Java 8, operators, arrays, loops, methods, and constructors while giving you hands-on experience in JDBC and JUnit framework.<br><br>Java Certification Course Key Features:<br>1. 70 hours of blended training<br>2. Hands-on coding and implementation of two web-based projects<br>3. Includes Hibernate and Spring frameworks<br>4. 35 coding-related exercises on Core Java 8<br>5. Lifetime access to self-paced learning<br>6. Flexibility to choose classes<br><br>Eligibility:<br>Simplilearnu2019s Java Certification Training course is ideal for software developers, web designers, programming enthusiasts, engineering graduates, and students or professionals who wish to become Java developers.<br><br>Pre-requisites:<br>Prior knowledge of Core Java is a prerequisite to taking this advanced Java Certification training course. Our Core Java online self-paced course is available for free to become familiar with the basics of Java programming.<br><br>ud83dudc49Learn more at: https://bit.ly/3b6SCvp<br><br>
E N D
Agenda What is a Java Servlet? Java Servlet Life-Cycle Java Servlet Architecture Java Servlet Features Java Servlet Advantages Java Servlet Disadvantages CGI CGI v/s Servlet Request and Response Session Tracking and Cookies
What is Java Servlet? Request Response
What is Java Servlet? Web Application Servlet - 1 Servlet - 2 Servlet Container
What is Java Servlet? A Servlet is part of a Java web application. A Java Servlet is a Java object that responds to HTTP requests It runs inside a Servlet container A Servlet container is capable to run multiple web applications at the same time, each having multiple servlets running inside Servlet - 1 Servlet
Java Servlet Life-Cycle • Java servlet life cycle involves the following throughout is lifecycle • We initialize the servlet by calling the init() method. • The servlet calls service() for processing the client's request. • We terminate the servlet by calling the destroy() method. • At the end, servlet is dumped by the garbage collector. Servlet
Java Servlet Life-Cycle • The init() method is called only once in the entire process. It is called only when the servlet is created. • public void init() throwsServletException { • //Statements; • } init()
Java Servlet Life-Cycle The service() method is the main method desfined to perform the actual task required. public void service(ServletRequestRq, ServletResponse Rs) throwsServletException,IOException { //Statements; } service()
Java Servlet Life-Cycle A GET request results from a normal request for a URL or from an HTML form that has no METHOD specified and it should be handled by doGet() method. public void doGet(ServletRequestRq, ServletResponse Rs) throwsServletException,IOException { //Statements; } doGet()
Java Servlet Life-Cycle A POST request results from an HTML form that specifically lists POST as the METHOD and it should be handled by doPost() method. public void doPost(ServletRequestRq, ServletResponse Rs) throwsServletException,IOException { //Statements; } doPost()
Java Servlet Life-Cycle • The destroy() method is called only once in the end of the entire process. It is called only when the servlet is terminated. • public void destroy() { • //Destroy-Statements; • } destroy()
Java Servlet Architecture Web Application Servlet - 1 Request Servlet Container Response Threads destroy() service() init()
Java Servlet Features Java Servlets are Portable in nature 1 2 3 4 Java Servlets are Scalable and Efficient Java Servlets are Robust in nature Java Servlets are Secure
Java Servlet Advantages • Servlet is convenient in modifying regular HTML • We can write the servlet code into the JSP • Servlets includes the feature of multithreading of java • We can make use of exception handling of java • Servlets have a separate layer of business logic in the application • Easy for developers to show and process the information
Java Servlet Disadvantages • Designing in servlet is complicated • Servlets often slow down the application • Business logic of Servlet is difficult to understand • We need a JRE on the server to run servlets • Database Connectivity is complicated
CGI CGI (Common Gateway Interface) allows the web server to call an external program and pass HTTP request to the external program to process the request
CGI Server http CGI Shell-1 CGI Program CGI Shell-2 Request CGI Shell-3 Common Gateway Interface
CGI V/S Servlet CGI Servlet CGI Scripts are executable codes written in native OS Servlets are written and run in JVM Performance is low Performance is high Platform independent Hard to switch between platforms Servlets are translated to bytecode CGI scripts are directly executed Servlets are potable CGI is not portable
Servlet Request HttpRequest The HttpRequest object is used to represent the HTTP request to a browser that the user sends to the web application. Thus, anything the browser sends, is accessible through the HttpRequest.
Servlet Request Request Parameter The request parameters are parameters that are sent from the browser along with the request. Request parameters are typically sent as part of the URL, or as part of the body of an HTTP request.
Servlet Request Request Header The request headers are name, value pairs sent by the browser along with the HTTP request. The request headers contain information about what browser software is being used, what file types the browser can receive.
Servlet Request InputStream If the browser sends an HTTP POST request, request parameters and other potential data is sent to the server in the HTTP request body. It doesn't have to be request parameters that is sent in the HTTP request body. It could be pretty much any data, like a file or a SOAP request.
Servlet Request Request Session The session object can hold information about a given user, between requests. So, if you set an object into the session object during one request, it will be available for you to read during any subsequent requests within the same session time scope.
Servlet Request ServletContext The ServletContext contains the metadata related to the application.
Servlet Response HttpResponse The HttpResponse object is used to represent the HTTP response to your request. A web application sends back a response page to the user as a response to the HTTP request from the browser sent to your web application.
Servlet Response WriteHTML To initiate the process of sending the html back to the browser, you need to retrieve the PrintWriter from the Httpresponse object.
Servlet Response Response Header Similar to Httprequest, the Httpresponse includes headers as well. These are responsible for all the data written in the response.
Servlet Response Content-Type The Content-Type header is responsible to tell the browser about the type of content you are sending in return
Servlet Response Content-Length The Content-Length header is responsible for telling you about the number of bytes your servlet is sending in return
Servlet Response Redirection Redirecting will help you to redirect yourself to a different webpage from the current one.
Session Tracking In servlets, Session Tracking is a simple procedure used to maintain the various types of data related to a specific user. The Data may be is search history, login credentials, etc. It is also known as session management in servlet.
Session Tracking Request (Second) Request Response User Server
Cookies A cookie is a small piece of information that is persisted between the multiple client requests. A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number.
Cookies Request Cookie Request Response Cookie User Server