Java Servlets Tutorial | Java JSP Tutorial | Java Server-Side Programming For Beginners |Simplilearn

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome everyone to yet another tech enthusiastic video from simply learn today we will learn about servlets in java so without much of add-o let's quickly begin with our session now let's have a quick look on the agenda for today's session first we will understand what exactly is a servlet clearly then we will move ahead into java servlets lifecycle followed by that we will understand the java servlet architecture once we have a good understanding of what exactly is a servlet and its lifecycle along with its architecture we will move ahead into its features after finishing with the features we will now enter into the practical session where we will understand setting up the environment for java servlets now setting up the environment for java servlets is the most tedious task and many fear it but in actual case it is not at all difficult i will show you how to set up the environment for java servlets and execute a sample program for a better understanding of java servers practically after that we will discuss some of the major advantages and disadvantages of using java servlets followed by that we will learn the cgi with a brief introduction then the differences between cgi and servlets next up we will learn how the requests and response work finally we will conclude the session by learning the process of session tracking and cookies hope we are clear with the agenda now let us quickly start the session so the first question what exactly is a servlet now imagine you're searching for some information on the internet you open your local browser and start typing it this question from you to the internet is called as request so further this request is sent to the cloud or in technical terms your browser sends your request to the respective server responsible for storing for that particular information and helps you with it when requested now the server gets triggered with your request and in written it helps you back with the information you have requested for this answer from the server is called the response so this is how the request and response work in a server for this process to happen so smoothly and effectively we need a servlet digging a little deep into the server we find the component responsible for such an effective job it is called the servlet container that holds java based web application and a set of individual components called as servlets now we are close to our answer so this particular java based component takes care of web requests and response to sum it up with the form of a definition we can define it as follows a servlet is a java program that runs on the java enabled web server or application server they are used to handle the request obtained from the web server process the request produce the response and then send a response back to the web server now hoping that it was a clear explanation can we move ahead with the next topic so the next topic we have is so how does the servlet work in reality or more practically or in depth what's the life cycle of a servlet so the life cycle of a servlet in real time has the following stages these stages are the ones that a java servlet actually follows in real life to get our work done so the stages are init service and the crud operations which are the get post put delete and finally destroy now let us understand each one of them in a bit more detail the first one is the init process the init method is called only once it is called only when the servlet is created and not for all the requests afterwards so it is used for the one-time initializations just as with the init method of applets the servlet is normally created when a user first invokes a url corresponding to the servlet but you can also specify the servlet is loaded when the server first started when a user invokes a servlet a single instance of each servlet gets created with each user request resulting in a new thread that is handed over to the do get or do post as appropriate the init method simply creates or loads some data that will be used throughout the lifecycle of a servlet followed by the init method we have the service method so what is service method surface method is the main method to perform the actual task the servlet container that is the web server calls the service method to handle request from the client or the browser and to write the formatted response back to the client each time the server receives a request from the servlet the server spawns a new thread and calls service the service method checks the http service type and accordingly performs the crud operations which are the get post put delete etc and calls do get do post do put do delete etc methods as appropriate the service method is called by the container and service method invokes do get do post do put and do delete etc methods as appropriate so you have nothing to do with service method but you overwrite either with do get do post and other types depending on what type of request you receive from the client so this is how the init and service methods work now digging a little deeper we have the do get do post do delete and do put methods so first we will discuss about do get method so what exactly does a do get method do a get request results from a normal request from a url or an html form that has no method specified and it should be hardly by do get method followed by doget we have do post a do post request results from a html form that specifies list post as the method and it should be handled by do post method similar to to get and do post the do put and do delete options are usually used to update and delete the data when asked by the client so to sum up these do get do post do put do delete we can consider them as crud operations so do get basically gets the information what you're looking for do post method usually post the information or provides you with the response from the server and followed by that we have the do put which is usually used to update the data which is present on your server and finally delete which is used to delete an existing data on your server so these can be considered as crud operations and finally we arrive at the last method which is the destroy method the destroy method is called only once at the end of the life cycle of a servlet this method gives your servlet a chance to close database connections halt background threats right cookie list or hit accounts to the disk and performs other such cleanup activities so with this we finish up the life cycle of a servlet now we shall understand its architecture basically the architecture of a servlet can be understood by reverse engineering the life cycle of a servlet so this is how the servlet execution takes place when a client or a browser makes a request to the web browser so the servlet architecture includes the following first one servlet interface to write a servlet we need to implement the servlet interface servlet interface can be implemented directly or indirectly by extending generic servlet or http servlet class followed by that the next part is request handling methods as we have discussed before there are mainly three methods defined in servlet interface which are init service and destroy and inside service we have do put do post do get and do delete methods the first time a servlet is invoked the init method is called it is called only once during the life cycle of a servlet so we can put all your initialization code here the service method is used for handling the client request as the client request reaches the container it creates a thread of servlet object and request and response object are also created these request and response objects are then passed as parameter to the service method which then processes the client request the service method in turn calls the do get do post methods if the user has extended the class from http servlet and lastly the creation of instances this is based on the load of the servlet so with this we have finished the architecture of servlet moving further we shall quickly discuss the features of servlets and then enter into the practical mode so the following are the features of java servlets the first feature is java servlets are portable in nature as i have mentioned above the servlets use java as programming language since java is a platform independent programming language the same holds true for servlets for example you can create a servlet on windows operating system that uses glassfish as a web server and then later run it on any operating system like unix linux and apache tomcat web server this feature makes servlet portable and this is the main advantage of servlet over cgi we shall discuss all the differences of cgi versus servlet in the next section for now we will discuss the features followed by the first feature we will now enter into the second feature which says java servlets are efficient and scalable once a server is deployed and loaded onto a web server it can instantly start fulfilling the request of the clients the web server invokes servlet using lightweight thread so multiple client requests can be fulfilled by servlet at the same time using the multi-threading feature of java compared to cgi where the server has to initiate a new process for every client request the servlet is truly efficient and scalable the next and the final feature of java servlets is they are robust and highly secure by inheriting the top features of java such as garbage collector exception handling java security manager etc the servlet is less prone to memory management issues and memory leaks this makes the development of web applications and servlets secure and less error prone so these were the important features of java servlets now let us enter the practical mode so we are in the practical mode right now there were two parts in the practical mode the first one was to set up the environment for java servlets and the second one was to execute a basic program so this document has the following steps to follow that is the prerequisites before executing a java servlet program so we have these prerequisites where we have to set up java home and java path followed by that we need to set up the home and path for jre which is the java runtime environment and finally catalina home and path so catalina home and path is nothing but the tomcat server home and path now we will set up these things by using the environment variables now i'm using a windows operating system so i'll be using the control panel to enter the environment variables there you go you have edit environment variables so inside that just select environment variables and inside this you can see that i have already set up the environment variables for catalina home java home and jre home so now let us just select java home and edit it so that we can see how did i set it so you can see i have named the variable name as java home and inside the variable value i have a location so this location is nothing but the java jdk location in my local system now followed by that we have jre home so similar to java whom we have set up the jre home as variable name is equals to jre home and the variable value is the location of gre folder in my local system so i have my jre folder in my local system at c drive program files java jre and similarly the catalina home which is inside my program files apache software foundation and tomcat 8.5 so similarly we'll also check up the path variables so inside system variables you can see the path just edited so then you can see the path variable set for java jre and tomcat now just select ok ok and finally another ok now your prerequisites have been set up so this is how you set them so in case if you have doubts about the tomcat version to download then i'll help you with that just open google and type in tomcat download so now you have entered apache tomcat official site and since i'm using a windows operating system i'll be selecting the 32-bit or 64-bit windows service installer version and after this gets downloaded you need to set up the home and path variables for catalina server so once after the environment is set up you need to follow four stages to create a servlet program which are creating a dynamic web project create a servlet add servlet api jar file and run the servlet so to create a dynamic web project you need to select new and inside that you have the option of other just select other and inside that you'll be finding web so select web and inside that you need to select the dynamic web project and select next so you just need to name to your dynamic web project so i'll be naming a server then select next and inside that you have a source folder and now you can select finish now there you go java eclipse will ask you for opening this particular in java ee perspective then just select open perspective now basically java will set up everything you need here and there you go you can see server folder here which has already set up everything you need now what you need to do is set up a new servlet so right click this and select new and inside that you can see the option of servlet just select it now you can see a new dialog box and here you need to set up the class name for now let's just type in hello and go to next now again next and in here you have a lot of options so for now we are just executing a basic program so let's just select do get method now let's just finish it and there you go your servlet has been ready so this is the basic template created by eclipse for you now next we need to add in the build path so for that you need to right click the server and then select build path and here configure build path from here you can add the external library select the external libraries option and you can see i have the tomcat libraries here just select all and open and that's how you add all the required libraries of tomcat server now apply and close now the last step is to execute the project now just to save time i have already typed in the program which is over here now let's try to execute this this program will just provide us a simple output which is none other than hello world now the server i have choosed is tomcat server let's finish it so there you go the message has been displayed successfully hello welcome to simply learn now we'll move ahead with our next topic now that we are done with the environment setup and the basic example let us now discuss the advantages and disadvantages of using java servlets so first we will discuss the advantages the following are the advantages of using servlets in java servlet is convenient in modifying regular html we can write the servlet code into jsp servlets include the feature of multi-threading of java we can make use of exception handling of java servlets have a separate layer for business logic in the application and finally it is easy for developers to show and process the information so these were the advantages of using servlets in java moving ahead we will discuss the disadvantages of using servlets so following are the disadvantages of using servlets in java designing a servlet is considered as a little complicated so let's often slow down the application business logic of servlet is difficult to understand we need a jre which is the java runtime environment on the server to run our servlets and finally the data connectivity is a little complicated so these were the major disadvantages of using servlets and java moving ahead we will understand cgi why are we learning cgi because even cgi is similar to servlets and it also does the web interactive job similar to servlets so thought you should know about some basics of it so let's understand its definition and architecture real quick so the cgi or common gateway interface is a standard for writing programs that can interact through a web server with a client running on web browser these programs allow a web developer to deliver dynamic information usually in the form of an html via the browser a cgi program can be written in any language including java that can be executed by your web server cgi programs are commonly used to add search engines guest book applications database query engines interactive user forums and other interactive applications to websites in very basic terms a cgi program must interpret the information sent to it process the information in the same way and generate response that will be sent back to the client most of the input of cgi program is passed into through environment variables so with this we will move ahead into the architecture of common gateway interface so you can see the architecture of common gateway interface is completely similar to servlet normally the request is sent in the form of an http request so this particular http request is divided amongst the cgi shells and it will be processed by the cgi shells based on the program provided and the response is sent to the user now moving ahead we shall understand the differences between the common gateway interfaces and the servlet so the first difference is the common gateway interface scripts are executable codes written in native operating system while on the other hand the servlets are written and run using the java virtual machine the next difference is the common gateway interface performance is considered to be low compared to servlets on the other hand servlets are having high performance compared to cgi's the third difference is for a common gateway interface it is hard to switch between different platforms that is the operating system platforms when we come into servlets the servers are considered to be completely platform independent followed by that the common gateway interface scripts are directly executed so on the other hand servlets are translated into bytecode because the servlets are executed using the java virtual machine and in java virtual machine whatever the code you transfer it will be converted into bytecode first then executed and the last difference is the common gateway interface is not portable and on the other hand we have the java servlets to be completely portable so these are the major differences between the common gateway interface and java servlets now let us understand the java servlets requests and responses so first we will deal with the request phase in request we have various types the first one is being the http request the http 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 http request followed by the http request we have the request parameter the request parameters are the parameters that are sent from the web browser along with the request the request parameters are typically sent as a part of the url or as a part of the body of an http request followed by the request parameter we have the request header the request headers are the name value pairs sent by the browser along with the http request the request headers contain the information about the web browser software being used what type of files that the web browser can receive etc followed by the request header we have the input stream 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 the request parameters that is sent in http request body it could be pretty much as the data like file or soap request the next one is a request session the session object can hold information about a given user between the request so if you have set an object into the session object during one request it will be available for you to read during any subsequent request within the same session time scope followed by the request session we have the servlet context the servlet context contains the metadata related to the application followed by servlet context we will enter the response phase in the response phase we will deal with the first one which is the http response the http response 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 followed by the http response we have the right html to initiate the process of sending the html back to the browser you need to retrieve the print writer from the http response object this can be done using the right html followed by the right html we have the response header similar to http request the http response includes headers as well these are responsible for all the data written in the response followed by response header we have content type the content type header is responsible to tell the browser about the type of content you are sending in the return followed by content type we have the content length the content length header is responsible for telling you about the number of bytes your server is sending you in return followed by content length we have redirection redirecting will help you to redirect yourself to the different web page from the current one so these were the http requests and responses in java servlets now we will enter the next part where we will deal with the session tracking and cookies so first we will understand session tracking session simply means a particular time of interval session tracking is a way to maintain state or data belonging to a user it is also known as session management and servlets http protocol is stateless so we need to maintain state using session tracking techniques each time the user request to the server the server reads the request as a new request so we need to maintain the state of a user to recognize a particular user you can understand this in a better way by looking at the picture shown on the presentation each time you send a request you will be receiving a response from the server as well as this particular request will be recorded as a state in order to recognize you as a particular user now after session tracking we will enter into cookies cookie is a small piece of information that is persisted between multiple client requests a cookie has a name a single value and optional attributes such as comment path and domain qualifiers a maximum age and a version number by default each request is considered as a new request in cookie technique we add a cookie with response from the servlet so cookie is stored in cash of a browser after that if the request is sent by the user the cookie is added with a request by default thus we recognize the user as an old user there are two types of copies they are non-persistent cookie and persistent cooking let's understand non-persistent cookie first a non-persistent cookie is valid for a single session only it is removed each time when the user closes the browser now let us understand the persistent cookie the persistent cookie is valid for multiple sessions it is not removed each time when the user closes the browser it is only removed when the user logs out or signs out from the browser so with this we have come to an end of java servlets tutorial if you have any queries based on java servlets then please feel free to drop them down in the comment section below and we will have your questions answered from our expert team till then thank you until next time hi there if you like this video subscribe to the simply learn youtube channel and click here to watch similar videos to nerd up and get certified click here
Info
Channel: Simplilearn
Views: 13,366
Rating: undefined out of 5
Keywords: java servlets tutorial, java servlets, java servlet, java servlet project, java servlets explained, servlets in java, what are servlets in java, what are servlets, what are servlet containers, how to use servlet in java, java servlet api tutorial, java servlet demo, java servlet session, java servlet architecture, java servlet programming tutorial, java programming, java tutorial for beginners, java, install tomcat, servlet environment setup, tomcat set up, simplilearn
Id: 7AIjcZMo-V4
Channel Id: undefined
Length: 27min 24sec (1644 seconds)
Published: Wed Sep 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.