Servlet Tutorial | JSP Tutorial | Advanced Java Tutorial | Java Certification Training | Edureka

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey everyone this is Neha from midoriko I welcome you all to the session on java servlets and JSP let's take a look at the topics to be covered in this session first and foremost I will introduce you the concept of web and then we will see some basics of servlets along with an example next I will talk about generic servlets and how to create it after that we will see some advanced concepts of servlets like session tracking and cookies and moving further we will understand the fundamentals of JSP and also I will tell you how to create JSP along with an example and finally I will wrap up this session by giving you insights about Java being class without any further ado let's get started Before we jump into servlets let's understand few fundamentals of web web consists of billions of clients and servers that are connected through buyers and wireless networks first web clients make requests to web server then what happens web server receives a request finds the response and returns back the response to the client when a server answers a request it usually sends some type of content to the client the client uses web browser to send requests to the server and then server often sends response to the browser with a set of instructions that are written in HTML and all browsers to know how to display the HTML page such as a hypertext markup language page to the client so what is web application a website is a collection of static files that as HTML pages images graphics etc and a web application is a website with dynamic functionality on the server you can say Google Facebook Twitter all these are examples of web applications so what is the link between web and HTTP let's see that now HTTP is nothing but hypertext Transfer Protocol it's a protocol that clients and servers use on the web to communicate with each other it is very similar to other internet protocols like SMTP that the Simple Mail Transfer Protocol and FTP that is File Transfer Protocol and HTTP is said to be stay this protocol that does it suppose only one request per connection so what does it mean it means that with HTTP the clients connect the server to send one request and then they disconnect this mechanism allows more users to connect to a given server over a period of time and then client sends an HTTP request and the server answers with the HTML pages to the client using HTTP simple so what does HTTP request and response HTTP requests can be made using variety of methods but the one which are widely used are get and post the name itself tells the server what kind of a request is being made and how the rest of the message will be formatted that'ss client sends a request to the server and server response back to the client using HTML pages now as you have understood some basics of web let's dive deeper and understand the concept of servlet and what it does so what are servlets it can be described in many ways like it's a technology which is used to create web application servlet is an API that provides many interfaces and classes during documentation it's an interface that must be implemented for creating any servlet also it is a class that extends the capabilities of the servers and responds to the incoming requests that is it can respond to any kind of requests and servlet is a web component that is deployed on the server to create a dynamic web page and it is also robust and scalable in this figure you can see client sends a request to the server and server generates a response and then it analyzes it and sends a response back to the client so basically this is how it works now let's move further and see various methods in life cycle of a servlet first loading a servlet when the web server starts up the servlet container deploy unloads all the servlets then what happens servlet is initialized by calling the init method the init method is called by the servlet container to indicate that this servlet instance is instantiated successfully and is about to put into service next the servlet called service method to process a client's request this method is invoked in form the servlet about the client request and then servlet is dominated by calling the destroy method destroy method runs only once during the lifetime of a servlet and it indicates the end of the servlet instance so basically in it and destroy methods are called only once and finally servlet is garbage collected by the garbage collector of JVM so this is all about the life cycle of a servlet now we will see what are the steps involved to create servlet first you have to create a dynamic web project and create a servlet code next you have to add map thanks to web dot XML file then start your server that is Apache Tomcat and last you can start the web browser and request the servlet now we'll see how to create a simple servlet and execute it on the web browser to create any servlet you need two important things that this Eclipse for Enterprise Edition and the one more thing is your Tomcat that is Apache Tomcat server the very first thing you have to do here is you have to configure your Apache Tomcat you can open your Google Chrome and just simply type Tomcat download version 9 you can set the first link and you can download this zip file or any other file I prefer zip file so I have already downloaded the zip file and extracted it in my D Drive you can see here Apache Tomcat folder so when you extract this just click on the lip and see the servlet and JSP API should be present these are very important so initially when you open your eclipse you can see here that no servers will be present so you can click on window Shoaib you and click on servers you can right click here and click on new and click on server you can see that there are so many versions of server I have preferred the latest one that is mine you have to just click on next and finish but then you have to configure some environment variables like choose this and apply and close also it will ask for the path like the directory path where you have installed Tomcat so you just have to copy and paste this path over there and then click on finish that's all after that you can just double click on this and check the port numbers you can see here that the Tomcat admin for the HTTP port the a JP port etc usually it runs on localhost and followed by your h0 h0 port number that is your HTTP port and you can see it is using Tomcat installation so I will just right click on this and click on start you can see here at the Tomcat server got started just open the web browser and type localhost:8080 and click on enter you can see the home page of Tomcat this indicates that your Tomcat server is up and running now we'll execute a program now as I have told you the first thing that you have to do here is click on dynamic web project give a project name like login next always choose generate web dot XML deployment descriptor click on finish so this is my login project you can see build web content Java resources etc in that content you have meta-inf and thereby an F and in the lib folder you have web but XML file this file is very essential as you need to add the mappings to web dot XML file for the servlet I'll show you in a later part how to do that so first we will see a basic login example using HTML file so click on web content choose new and choose HTML file I will name this as index dot HTML next finish so I need to input the details and that's the reason I'll be creating a form for login and now I will use a table tag to create our username and password so this will be my index with HTML page we're in table row I have a table definition for username that as input type is equal to text and name will be username and similarly for password as well if you don't worry Donna I like to give it as console us save this and the next step is to create a Java class file so click on Java resources we right click on source and choose a new package I will name my package as a derecho finish now click on the package and create a new class file i'll give the name of the class file as login finish so then you get this error you have to import the package and now I will write the complete program and then explain you so here you can see I have used a do post method and invoking requests and response parameters and then using the printwriter method I'm setting the content pipe and I'm creating string variables and requesting the parameters for username and password I am giving some conditions like if username and password both are equals to a Eureka then it has to print login as success else the login should be failed simple now the last step is to add the map thanks to web dot XML file so here you need to declare the servlet and servlet mappings and in servlet we are going to give the servlet name and the servlet class name and in servlet mappings we will be giving the servlet name followed by URL pattern let's see how so you can see here I have given the servlet name as login you can give it as anything it's fine but the main important thing that you have to focus here is on servlet class and URL pattern for servlet class you have to give your Java class file name that is login I am giving package along with it because package is Eddie Rekha as you can see here so I'm referring it as I do record or cloggin and URL pattern will be slash login that will be the one which you have mentioned and form action in your HTML file both should be same or else our servlet will not work save this file there are so many welcome files which are actually not required you just need to have the index or HTML file again save this now we are all set for execution let's execute the program now just right-click on the project and click on run as run on server finish your server starting as you can see that Sun up here you got the basic login page I have to give username as ed Eureka and password as ed Eureka only then it says login a success or else it says login s field so I'm giving password has a direct con now you can see here at saying login as success correct it was printing login a success for two times because I have mentioned a statement here that's actually not required again safe this from the program again so I'll give both the same it says login a success again if I give user a said Eureka and password something different it says login has failed correct so basically this is how you can create a servlet and you can run the program I hope you got an idea about it now I will tell you what is generic servlet and how it works and how it can be created a gender excel --it is a protocol independent servlet that should always override the service method to handle any client requests and service method accepts two arguments that is servlet request object and servlet response object the request object tells the servlet about the request that was made by client while the response object is used to return the response back to the client genrich's servlet is an abstract class and it has only one abstract method that a service and that's the reason when we create generic serve that by extending generics or blood class we must override service method so what are the advantages of generic servlet it is very easy to write and it has very simple life cycle methods and to write a generic servlet you need to extend Java extra servlet of generic servlet and you have to override the service method now let's see how to create an invocation mix of let so I'll modify the same program here I'll rename the class as generic one I just rename the file type to generic one and then I'll go on writing the code so gender aquatics tend genrich's servlet class correct and as I have already mentioned it should extend jaynewick servlet and override the service method and then again I am passing requests and response and then using print writer I am trying to print all this simple save this and now what will be the changes in index dot HTML file let's see that now so in this index dot HTML file I'm giving a reference and I'm saying click to call genrich's servlet that is on clicking this link it has to print this message thatis genrich's of that example and welcome to Eddie Rica YouTube channel correct let's see whether that will happen or not and the last step is to add the mappings to Everett XML file the only changes will be I do Rica will remain as the package and gender equal B the class name instead of servlet name as login I'll give it as genrich's servlet and the URL pattern should be welcome because that is what is being mentioned in the H ref of index dot HTML correct so again save this now let's run the program and see what will be the output so I told you it will say click-to-call genrich's servlet and on clicking that it will redirect to this page that is the second welcome page you can see the URL pattern that we have mentioned and it says genrich's of that example and welcome to Eureka YouTube channel correct simple I hope you understood the concept of generic servlet now let's dive into a few concepts of advanced servlets so I have listed down few classes and interfaces of servlet first servlet this is used to declare lifecycle methods of servlet next servlet conflict it allows the servlet to get initialization methods servlet context using this it enables the salat to log the access and access the information next request you can read the data from client requests and you can write the data to client responses in servlet response and genrich's of let it implement servlet and servlet config interface input stream this provides input stream to read request from the client and serve let output stream provides the output stream to write responses to the client next servlet exception it indicate that servlet error has occurred and when the servlet is not available it throws unavailable exception and it should EP request that as it provides methods to handle request and response similarly httpservletrequest enables a servlet to read data from HTTP request and the response enables the servlet to write data to HTTP response that's all now let's dive into few concepts of advanced servlets so here we will understand what the session tracking session simply means a particular interval of time that is it is the way to maintain the state or data of a user it is also known as session management and servlet as you all know that HTTP protocol is stateless so for that reason we need to maintain the state using session tracking techniques so as depicted in the picture you can see so each time when user request the server server treats the request as a new request so we need to maintain the state of an user to recognize a particular user so as HTTP stateless each request is considered as new request so in order to recognize a particular user we need session tracking so these are the few techniques of session tracking butters cookies URL rewriting hidden form field and HTTP session among these let's talk about cookies now cookie is a small piece of information that is persisted between the multiple client requests a cookie has a name a single value an optional attributes such as comment path domain qualifiers age and version number so how does cookie work again as it is a technique of session tracking by default each request is considered as new request in cookies technique we add cookie with the response from the servlet so that's the reason cookie is stored in the cache of the browser after that if the request is sent by the user cookie is added with request by default thus we recognize the user as an old user so these are the useful methods of cookie class that is public wide set max age this sets the maximum age of the cookies in seconds and you can use the getname method that returns the name of the cookie and name cannot be changed after creation and you can get the value that does it returns a value and you can set the name and set the value for cookie as well we'll see how to do that so basically this is how you can create and delete a cookie that is you can create a new cookie object and once you create the new cookie object you have to add the response for that that's the usual technique how we do and similarly while deleting the cookie you have to just specify user name and specify the blank instead of specifying a dorieke and then you can change some Max age to zero and then again add the response simple now let's see an example of creating a cookie adding the response and retrieving the results for this I'll create our new project called cookies next generate this and click on finish so in this first let's write the class files and create a new class file called my servlet one because I am creating two class files and that's a reason click on finish now let's write the code and again even this should extend HTTP servlet so here you can see I have created a cookie class that is my servlet 1 I'm just setting the content type and trying to retrieve the name and password that is a request or get parameter that is a usual thing I'm trying to print hello followed by your name and your password will be so and so you can see here I am creating two cookies that is cookie 1 and cookie 2 that the C 1 and C 2 that'll be your user name followed by the name whatever you give in the browser and the password that will be your user password and then the next step is we have to add the response for that simple and I'm using H ref to link the cookies okay save this I create one more class file that will be my servlet 2 finish and now let's see what all we have to write in this so in this I am trying to read the cookies that is request or get cookies and then displaying the username and value from cookies using the name and value that will be your get value methods and then trying to catch the exception if focus any in the try block save this and now we will write HTML file in the web content that will be your again in next one HTML here I'm creating a simple form for user name and password and now what is the last step you have to add the mappings to web or XML file let's see that so in this first we are again giving the servlet name for first servlet that is my servlet one and then giving the pattern that will be login and next for servlet - that is my servlet - and giving the URL pattern as welcome very simple now we are all set for execution let's right-click on cookies that is your project and click on run on server save everything next choose cookies and finish and now let's run the cookies and see what will be the output your server is starting now so you have to enter the username whatever you want I'll give like and you Rica only I'll give password something submit when you submit it says hello to Rica your password is QWERTY correct so it's displaying though username and password for you again when you click on View details it will redirect you to the second piece that is the Welcome page and seis name as a do Rica and password will be so-and-so so this is how you can request the cookie and get the response back so that's all about cookies and how it works so that was all about servlets now that you have gained some insights on self let's let's move ahead and understand what is Java server pages that is JSP it's a technology used to create web application just like servlet technology it can be thought of as an extension to servlet because it provides more functionality than servlet such as expression language as deal etcetera it has features like fast development tracking user easy to maintain flexible powerful and portable a JSP page consists of HTML tags and JSP tags the JSP pages are easier to maintain than servlet because we can separate designing and development it also provides some additional features such as expression language custom tags etcetera so what are the advantages of JSP over servlets JSP is an extension to servlet we can use all the features of the surf lesson JSP in addition to that we can also use some implicit objects predefined tags expression language and custom tags that makes JSP development very easy next it is easy to maintain JSP can be easily managed because we can easily separate our business logic with presentation logic and solve the technology it is complicated because we mix up with business logic along with presentation logic next fast development that is we don't have to recompile and redeploy if JSP page is modified we don't need to recompile and redeploy the project but the servlet code needs to be updated and recompile if we have to change the look and feel of the application next it is having less code than servlet that is very true because in JSP we can use many packs like shantaks custom tax jaya steel etc and that reduces the code moreover we can also use some implicit objects as well now let's move further and see the life cycle of JSP JSP pages involve these following phases that is translation of JSP page compilation class loading instantiation initialization request processing and destroy as depicted in the above diagram JSP page is translated into servlet by the help of JSP translator and JSP translator as a part of web server which is responsible for translating the JSP page into servlet after that servlet page is compiled by the compiler and gets converted into class file moreover all the processes that happen in servlet are performed on JSP like initialization committing response to the browser and destroy so that's how the lifecycle of JSP is next we'll see the steps to create JSP first again create dynamic web project you have to create a JSP file start Tomcat and deploy the project and get the output simple I'll show you how to do that before that we'll see some scripting and directive elements so what are scripting elements the scripting elements provides the ability to insert Java code inside the JSP there are three types at the script let expression and declaration a scriptlet tag is used to execute Java source code in JSP and the syntax is as shown in the figure now we'll see how to print the user name using scriptlet tag so I'll close all this and create a new project I will name it as JSP and click on next generate this and click on finish so in this what I have to do is I'll configure the build path and our JSP API also I will add sublet API as well apply and close now we'll create two files here one will be HTML file and the other one will be JSP file first let's create HTML file and web content I'll simply create a form for username so here what I did I created a simple form and that will be a reference to welcome by GSP and then input type will be 'text I am giving a parameter for username and then submit and go save this again click on web content and choose new and click JSP file so here will give welcome dot JSP because that is a reference which we have given so that goes to be welcome dot JSP click on finish so this is how your JSP file appears so in this what we'll do is we'll get the parameter to print the name simple I'll show you how so that's nothing but your java source code so you can enclose the java source code between these things so I am simply using string name is equal to request or get parameter and I'm printing welcome and followed by your name simple save this let's run the program on server so it is asking you to enter the name I'll give it a recap click on go it's saying welcome at Eureka very simple so what basically happened here is the HTML file get the username from the user and the Welcome file of JSP prints username with the welcome message simple so all that we used was two files at as HTML and welcome so that's how you can make use of scriptlet pack next we'll see what is expression tag so in this the code that is written within the expression tag is written to the output stream of the response so you need not write out dot print to write the data it is mainly used to print the values of method or variable so you can use the expression and declaration statement to declare the methods so one important thing to note here inside the declaration tag the code that displays this outside the service method of auto-generated servlet so that it does not get any memory at each request now let's move further and see some implicit objects of JSP that is these are the objects and these are the types of objects that is outers of 5 JSP writer and requester response or of 5 httpservletrequest and response and config of types of the config application of types of lick context session obviously of type HTTP session page context object and through having understood these things let's move further and see the directive elements so there are three types one is page directive one is include directive and last one is tag lib directive so in case of page directive it defines attributes that applies to an entire JSP page and the syntax goes very simple for page directive that will be page directive is equal to value simple next the include directive is used to include the contents of any resource like it may be JSP file HTML file or text file the include directive includes the original content of the included resource at each page translation time and the advantage of this is code reusability and coming to tag Lib it is used to define a tag library that defines many tags and now we'll see some of the implicit objects like I talked about request and response let's see how it will work so the JSP request is an implicit object of type HTTP request that is created for each Lea spirit quest by the web container it can be used to get request information such as parameter header information remote address server name server port content type character encoding etc it can also be used to set get and removed attributes from the JSP request scope let's see a simple example of implicit object where we are printing the name of the user along with the welcome message it's very simple as you have already seen the example of scripted tag it will be same like this there will be no change in the code nor anything you have to just create a form action an HTML file and in the Welcome dot JSP you have to pass the statements for getting the parameters that is username and just use out dot print like you can say welcome and followed by your name so this will be same like the one which we have created before in script like that that is an index or HTML will give a reference to welcome dot JSP and followed by the username and again in welcome dot JSP we are writing the java source code to get the parameter and just print welcome followed by your name very simple and the output remains the same like this ok now let's see the response implicit of in JSP response is an implicit object of type httpservletresponse correct the instance of the httpservletresponse is created by the web container for each JSP request it can be used to add or manipulate response such as redirect the response to another resource send the errors etc let's see the example of response object where we are redirecting the response to the Google page let's see how and I did the same thing an index dot HTML this remains the same that is it remains welcome dot JSP and this will be text followed by username and this remains the same in case of welcomed or JSP I did this and I will write like response dot sent redirect this to Google page correct so what I did here is so in HTML page I'm giving the reference to JSP file that is welcome dot JSP and Here I am passing the response to Google page let's see how we'll execute this and check the output so whatever you type here at redirects to Google page okay I'll give at Eureka again so you can see here it redirected to the Google page so again give our Eureka here you will get the I'd Rica website simple so all this happen with one click on response object so it's nothing but it's redirecting the response to Google page okay I hope you understood the request and response implicit objects now let's see what is javabean class it's a Java class that should follow following conventions that is it should have a no argument constructor it should be serializable it should provide methods to set and get the values of the properties known as getter and setter methods so why do we have to use javabeans according to the white paper it is a reusable software component a bean encapsulate smen e objects into one object from multiple places moreover it provides the easy maintenance so let's see how to access the Java Bean class so in order to access this we need to have the setter and getter methods let's see how for now I will be deleting these two files I'll show you how to create a Java Bean class again create a new package called add eureka finish new class file that will be your anything i am going to create our employee file first so let's see how that will be finish so let's write the pour now as you can see i have used the set ID and get ID method and set name and getname method and I'm returning these things so this is how you can implement set and get methods for your Java Bean class now what we have to do is we have to access this bin class so for that we again need to invoke the method that is we have to create an object of employee I'll create one more class file and I'll name it as something demo so in this what I will do is I'll create a main method and in this main method I will create object of this let's see how so I am creating an object of employee and setting the name to a Eureka and then I am trying to get the name simple save this and run the program so on set in the named add Eureka you can set the value of the object so basically this is how you can do that next we'll use use bean which is action pack to locate or instantiate of bean class if the bean object of the bean class is already created it doesn't create the bean depending on the scope but if object of bean is not created at instantiates the bean simple I'll edit the demo dot Java first I'll delete this I don't want that and this what I will do is simple in this I am trying to print a cube of a number and then what I will do I will create a JSP file in the web content folder and name it as indexed or JSP so in this I will call the reference to Java class so this is how you can use the beam action that is use the beam action ID will be your object and class will be your package dot class name that is any record or demo and then you can declare the statements here like I own a cube of five and I'll print the result simple so again click on the project click on run as run on server simple so here you can see it is printing the cube of fibers 125 simple so this is how you can use the beam action to invoke the class save this and that was all about JSP and for today's session I hope you learned something out of this thank you and have a nice day I hope you have enjoyed listening to this video please be kind enough to like it and you can comment any of your doubts and queries and we will reply them at the earliest do look out for more videos in our playlist and subscribe to Ed Eureka channel to learn more happy learning
Info
Channel: edureka!
Views: 58,261
Rating: undefined out of 5
Keywords: yt:cc=on, Java Servlets Tutorial, java servlets and jsp tutorial, Java Servlets, Servlets, Introduction to Servlets, Servlet Life Cycle, Servlet Examples, Generic Servlet, Cookies in servlet, Servlet request, Servlet Response, Servlet Architecture, Servlet Interface, Servlet Classes, servlet in java, servlet example in java, servlet example in eclipse, Java Http Request, Java Http Response, Jsp request and response, Java Bean Class, Java Certification Training Edureka, edureka
Id: TrfqKn6vzAI
Channel Id: undefined
Length: 34min 49sec (2089 seconds)
Published: Sun Dec 23 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.