[Music] so welcome back aliens this is n r from learnings in this video we'll talk about how saet works so just imagine we have a client machine there and we have a server right so when you talk about this website world or this web world so a client sends the request to the server expecting a page right again you can you can ask or anything on internet but let's say you're asking for a page now Now this page can be a static page it can be a dynamic page when you say static it means it
is already made and when you say Dynamic it means it will be built at one time so let's say a client sends the request which is a static request so it goes to a server on your server you already have a file right so that file goes to the client request done and so we got the request and response finished right but let's say if you're asking for a page which is Different which is from the which is which will be built at one time so a client sends the request to the server now server
says hold on I don't have any page with this with this link so you have to make that page right you have to build that page so that request goes to a helper application can you see that we we got an helper application on This Server now this helper application is also called as web container okay so in this web container you'll be having Svets now what is subet is basically a Java file which can take the request from the client on the internet and it can process that request it can provide you a response
in the format of HTML page right so this solet will convert your request and it will take your request it will fetch the values it will process the information and it will send the HTML page right so let's let me just go back once again so from client side request goes to server on the server Side since it is a dynamic request it will go to your web container which is so whatever web container we have so we have Tomcat we can also use glassfish we can use Joss we can use web spear in fact
all these other servers those are called as app servers but they also have a feature of web server so basically we always use Tomcat in fact in my in my subsequent videos you will find all the things ins using Tomcat right so that Tomcat is a web container so on your Server if you want to achieve web the Dynamic web web pages you need to install Tomcat okay so you can see that is Tomcat there so that web content is a tomcat so when request goes to your Tomcat Tomcat says hold on I got a
request now but that request is for abc. HTM and you don't have any page as AB ab. HTM right so we have to build that page so that request goes to a sulet hold on seret will be having a Java name right example when you create Java Classes let's say I want to add two numbers again a basic example if I want to add two numbers numers I will be having uh so I will be having the subet name as add seret that means when I request for abc. HTM the page which should be getting
called is your serlet how can we do that so what what we have is with in your inside your inside your container you have a special file which is called as deployment descriptor this is how it looks so you you have a file Which is deployment descriptor in which you mention for which request which server should be called okay so let's say in uh in I mean in your Tomcat server you have maybe 50 to 60 servlets so every subet will be mentioned I mean every request will be mapped with one subet it may happen
that you you have multiple requests trying to access the same sovet right everything need to be configured in a file which is called as deployment descriptor and the file name Is web.xml so this file here is your web.xml file so let me repeat when when client send a request it goes to the server now server thinks okay this is the dynamic page let's send the request to the web container now web container will check okay this is this request will be mapped by this sofl that can be done with the help of web.xml file so
you can see in the webml file we have two tags we have subet tag and we have subet mapping tag so in the subet tag You have to mention the class name and in your subet mapping tag you have to mention the URL pattern so for this URL we need to call that particular svet but hold on how do we get a serlet now and that's very simple you can say we have a file here so this is a this is your file structure so when you want to create a serlet it's a normal class
okay which extends HTTP serlet since we want this serlet features right we cannot create normal pojo which is the normal class we Have to create a special class which extends HTTP solet because this HTTP server will provide you all the features which you need okay now when I say all the features it means it should be able to take from the request from the server it should be processing the request and it should be sending the response how it will be done that we'll see in the next video but it takes the request it process
the information and it will send the response now that response will be In HTML format again you can send the response in Json format or normal spring you can send the response in XML format you can also send the uh response in the HTML format now that HTML page this which is a dynamic page goes from your server to your client machine in the format of response object right so now we have one more thing right we have web web XML file here do we have a replacement for that because the problem is we are
Java Programmers right and we don't like XML okay that's not the case but we but let's say we try to award XML now how do we award XML so in in this sub 3.0 we got a new feature which is called as annotations so we can use annotations on your subet so you don't have to use XML files or webml file you can you can do your mapping using annotation again how that done that is done that will say in the subsequent tutorials but this is how your serlet basically works with the Tomcat servers okay
so quick recap we have a client machine request goes to server from the server request goes to your web container web container uses a file called as web.xml to detect which seret to call and it calls a serlet that serlet will process the information and it will send the response to the client machine that that that's how it works in the next video we'll talk about how does how exactly your web container searches for the sablet and how it's get loaded And the life cycle of the subet now let's start with the actual work now
where do we code to work on a project you need to use one of the app or the software which is called IDE now IDE basically stands for integrated development environment it means it's a it's a software where you will code where you will compile the project and where where you will run the project now there are different IDs available for cets in fact for Java Field we have certain certain IDs available so one of them is net beans we have Eclipse uh we have intellig J so that if these are the three topmost softwares
available for Java developers now which is the best one I would say intell is the one of the best IDE available for Java but the only thing is it has two versions one is community version and second is the ultimate version and if you want to work on serers side coding you need to use Ultimate one and unfortunately you have to pay for that otherwise you can go with eclipse this is the next best IDE available and the awesome thing is it is free to use and then we have net beans normally in academic purpose
they use net beans a lot but I've never seen a development environment where we are using net beans but then people may be using it but I I prefer eclipse in fact the industry which I visited for training they all use Eclipse now we Will see how do you get eclipse in your machine now the first thing would be open your browser so just go to Google and search for Eclipse download now if you search for Eclipse download you can see it will show you this option which is Eclipse IDE for Java e just click
on that and now you can see we have for different OS we have different setups here now you can go for any version uh I mean depending upon your OS example in my machine I'm using Mac so I will be Going for Mac OS X if you're using Windows depending upon which bit you're using maybe 32bit Os or 64bit Os you will choose one of them and if you're using Linux flavors you can choose this one now since I'm using Mac I will be going for the third option here click on this and then you
will click on download and after in some time it will get downloaded and you can see it is getting downloaded here now it will take some time based on your internet speed now we Don't have to actually wait for this I already have a setup in my machine so if I go to finder you can see I already downloaded this software on my machine and the only thing is since it is start. gz you can simply double click it and it will give you the unzipped version right and the unzi version is eclipse and the
moment I double click on this you will get your IDE so the version which I'm using is Eclipse Mia you can also use neon that works fine now it will ask you For a workspace now workspace is basically a folder where you will put all your project so what your all your working projects will be there in this folder you can choose whichever folder you want and I would choose uh a saet I would say a subet workspace I'll click on okay and as you can see this is the eclipse ID so let me just
make it full frame and I will close the welcome window again you can enjoy this welcome window where you have some tutorials uh You you can see some sample code but then let me just close it and this is how your Eclipse will look like on the left hand side you have your project Explorer so all your project will be explored here on the right hand side you have some task list you have some outline to work with so we don't we don't need that as at this point now this is the area where you
will get your code window as you can see we don't have anything here yet but then this is where You will code and this Tabs are very important now this is one of the tab which will be helpful for debugging your application whatever wrong with your project you can see that here then this is the tab which is important the server tab now you might have guessed till now since you are working with seret you need a server to run it on right and in the theory video we have talked about it we need a
server which is a serlet container where you will run the Servlets and the seret container which we are going to use in the course is tomat so of course you need to link your Tomcat here but how do you do that that's a that's a different thing then this is for data source if you want to explore your database connections now this is what you have on the on this side now let's talk about this one this is a perspective so if I if I click on this perspective these are the perspectives which are available
now you Might wondering what this perspective means now if you are working on cets or any Enterprise level code let's say s JSP ejbs you will be selecting Java e now Java e offers you those features where you can configure Java Java e stuff example the server tab which you're getting here thanks to the Java e perspective because if I change ref perspective let's say Java which is used for normal code Java development if I click on okay you will see this Something change and the server tab is gone now so these are the perspective
you have so you can see if I click on Java AE perspective you will get servers if I click on Java perspective here you will not see servers there and if I again open the perspective there are G perspectives so if you want to see the repositories uh there is a jpa perspective if you want to work with jpa now time being you want to work with Java e so let's take to it click on okay And you got Java perspective now the next thing would be you need a project and you need a tomcat
server now once you have your Eclipse up and running it's time to configure Tomcat so before configuring Tomcat first you have to make sure that you have downloaded tomcat and we don't have it yet right so let's go back to the browser and search for Tomcat download now it it will take you the official website of Tomcat so let's go for the versions now you can See we have getting option of Apache Tomcat 7 Apache Tomcat 8 I would choose eight here and you can see we have certain versions now this is 8.5.2 4 and
the below one is 8.0 I prefer the complete version so 8.0 is something which I will using here you can also use 8.5 nothing wrong with that now in this which one to select again depend upon your OS I would select uh t. ZZ because it it works on Mac perfectly so I will click on this one and it will download The software and then you also need documentation now why we need source code here U that we'll see later but time being we got these two softwares one is the the tomcat and the documentation
oh we have to click on that as well so it will download a documentation as well now I have those things here so if you can see I have the setup for Tomcat and the source code as well time will let's not think about this source code think about this Tomcat Double click this one and it will unzip and it will give you the unzipped version of Tomcat can see that we got Tomcat here but but how do we use it of course we can use it on command line or terminal but then we want
to configure that to Eclipse so go back to eclipse and now you can see in the server tab now maybe you will not see a tab there on your machine how do we get it so in case if you don't find this tab here just go to window and say Show view Click on servers it will show you the tab there now we have a tomcat downloader we just need to connect it now how do we do that just click on this one now it will ask you which software you want to connect or which
server you want to connect so click on Apache and you can see it shows lot of versions this is the old version let's click 80 click on next and then it will ask you for the location where you have your Tomcat so click on browse and move to The location there so I came to a folder where I have my Tomcat can see that this folder double click that and click on open so there's no error click on finish and this is how you link your Eclipse with Tomcat but you're still not sure will it
work now how do you really ify if your Tom is perfectly working right click here and say start now if it is getting started perfectly you can see it says started that means your Tomcat is properly configured and it is working But maybe sometime it will not work now when I say sometime there might be different issues uh one of the most common issue is the port number which Tomcat is running on is used by someone else maybe you already have a HTTP server in your machine let's say Zam server let's say some some other
server which is used using the same port number now which port number I'm talking about so if I double click here this is the port which is used by tomat which is 8080 which is uh the HTTP port number now if you have any software which is using this port number then your Tomcat will not start now in that scenario what you will do is you will double click here and you will change your port number you can change your port number you can make it 8081 so now your Tomcat will run on 081 if
you save this but I I don't want to do that I will say I'm happy with 080 so since it is working on my machine I will not play with that now How do we access or how do we use Tomcat here so what we can do is you can just go to Local Host and type colon 880 now before hitting enter what I will do is I will go back to my eclipse and I will I will stop the server just to see if I if I don't have this server there so if I
stop it and after stopping it if I go back to browser and if I say enter it will give you an error it says the site cannot be reached because of course you don't have this port number running here Now if I say start let's see what we get so if I start here and go back to the browser and say enter now can see that we got a different error it says the page not found but that's fine is it your Tomcat working yes can you see that the response is you got it from
Tomcat that means this URL is hitting the Tomcat that's what we wanted to check but you can also get the amazing homepage of Tomcat if you do one more setting uh you can see in this server Location you can also choose use Tomcat installation it will show you the uh server page if I go go back here okay we have to restart the server right click and restart this is how you restart the server and go back to your browser I hope it will show you the home page of Tom K I can see that
we got the page now in which you can you can use this options as well if you want deploy any application but this this is for only uh seeing stuff because since we are using Eclipse we'll be deploying everything from the eclipse you don't have to use this page anymore but it's your choice you can use it so your tomit is working your tomit is up and running now the next step would be how to create a project and in this video we will see how to install eclipse and how do we configure Tom Cat
on eclipse on windows so step one would be we need to install eclipse of course you need jdk as well so I hope you have jdk installed on your Machine in this machine I have Java 9 but that's fine you can also install Java 8 or Java 7 okay so step one would be you have to download Eclipse so I will search for Eclipse download to be specific we want Eclipse Java e version so I would say Eclipse e download and you can see this is the website you will get and if you click on
this okay now the version is bit different this is a version Indigo but we want the latest version so I would select oxygen Packages here and from this we have to select Java e developers and in that also we will be going for the different versions as it is Windows 64-bit machine I'll be using 64-bit here once you click on this it will give you option of download and you can go ahead and you can download this the moment you click on download it will get downloaded by the time it gets downloaded let's go back
to the page and you can see that's why so famous you know there are Different flavors available so if you're working on Spring framework you can also go for spring which is based on eclipse or for different reason they have different IDs available now once you got Eclipse downloaded the second software we need here is Tomcat Tomcat being a server for your salet I would say Apache Tomcat you can go for any version I will go for Tomcat 9 and let's click on this one in the earlier video which we have configured on Mac I
have installed Apache Tomcat 8 and mass is just to give you a different flavor that you can work with any version we are installing nine now once you get this page you have to search for so first of all this is in beta version but that's fine it will work you might be facing some issues but then we can we can handle that this you can see we have so many options now we will be going for the zip version because we don't want to install it we just want to load your Tomcat in Eclipse
Right so we'll click on the zip version this will download the Tomcat 9 and we also need a source code distribution so that if you can see the source code as well so click on zip again how to configure this that we see later but you can see we got these three softwares this three softwares are there in my folder here so if I go to softwares you can see we have the three files and now let's unzip the this version so I will right click and say extract here and by The time we can
also unzip the Tomcat version so we can right click on tomcat and then we will say extract here so once we got these two softwares you can see we have this two folder and now I will open Eclipse now inside this folder you can see we have a software called as eclipse.exe double click on that and this will open Eclipse now now we it will ask you for the workspace now workspace is basically a folder where you can put all your projects I would Say the workspace here is my Sid workspace and I will click
on launch it does take some time to open because this is the first time opening eclipse on this machine so once we got this IDE available you can see we got the welcome page let's not touch it you now and click on this welcome page now you can see we have we got different areas this is where you will on the left hand side you will get all your projects uh we don't need this outline and task as of Now now this is important the server tab here now if you are working with salet this
is very important you need a server tab now sometime you will not find these tabs here again you can see we don't have console tab here because that is that is where you will get your uh system. out. printer and messages now if you want to see this thing you can click on window there's option of Show view in Show view you can select console if you want console there let me just click on Console you can see we got console here if you want server you can also click on server now since we already
have it here we don't have to do that otherwise you can select that and it will show you the tab now you can see it says there's no server available now how do we connect our server here so we'll click on this one now it will ask you for which server you want to connect you can see it gives you so many options here now we have to expand Apache in Apache we have you can See we have different version now since we are working with Tomcat 9 let's select this one and click on next
in this you have to select the path where you have your fold let me just go to that path so as you can see I have selected that path here I will click on okay so this is the path where I have my aparture Tomcat which is which we have downloaded and unzip now once once you got it let's click on next and nothing to configure here click on finish this Is how you connect your Eclipse with Tomcat so this is your Tomcat here but we have to also verify right if it's working so whenever
you before creating a project it's always better to test your tom cat so I would say start and you can see uh it is something is happening behind the scene we have to make sure that it is starting at some point and it started now how do you verify if it started now first of all you need to know which port number your Tomat is using so if I double click here on this this is the port number you are running your Tom at on which is 8080 to verify this let's go back to your
browser and here to verify this we'll type Local Host colon 880 so we'll type Local Host colon 880 and if I enter you can see we got the response from Tomcat now since your tomat is up and running that's why you got this response now if you want the awesome Tomcat web page you can also also select this option the Second option which will show you the Tomcat homepage if you want to see that and once you do that configuration you can also restart your machine now again sometime you know your machine will give you
some issue if you already have a service running with this port number maybe you have a Zam server or some other server which is using 8080 port number you can change it from here uh you can change your Tomcat port number to 8081 or 8082 your choice and save it And restart the server everything will work uh once I've done that configuration let me go back to my browser and I will say enter you can see that we got the awesome Apache Tomcat homepage so this is how you install eclipse on Windows and this is
how you configure Tomcat now once you have your eclipse and Tomcat configured it's time to start with our first project now how do you build a project or how do you create a Project in Eclipse it's very simple you can just go to file there's option of new and click on a project so it's very easy just go to file and click on new now it will give you certain options like jpa project Enterprise application project now which one to select we will be selecting Dynamic web project now this is one option you can go
for or the other option would be just right click on the project Explorer and send new now there's option of dynamic web project Let's choose it from here and now it will ask you for a project name we can give any project name here I would say the project name is demo app now that's the first thing we have to mention the second thing is you have to mention the target run time now Target run time is basically a run time where you will run your project if you remember we have install Tomcat 8 that's
why you're getting this option you can also choose a new run time you can configure Multiple times in your machine but one project will use only one runtime at a time next is the dynamic web module version so this is the version of seret API so basically seret is a API using which you can build web application the first probably Reas was 2.2 but now we are using 3.1 let's choose that and nothing to nothing specific here everything will be default click on next nothing to specify here click on next now it will ask you
for the context that That we'll discuss that later now this is important generate web.xml deployment descriptor now this file is important here we'll check mark this and we'll click on finish Now by doing that it will give you your first project and see that we got your project here let's try to understand what are the folders we have in this project so the first folder we will be looking at is web content expand it you will see there are two folders One is metf and one is web INF metf is not that important as of
now once you start working on the Enterprise things we we can see that but now the most important part is this web I folder now it's actually very important because this folder has this file which is web.xml what if I say this is the one of the most important file for your Serv application this is where you will configure most of the stuff now we'll not be looking in deep in this file yet We close this and minimize the web content the next important folder which will be going to use is the Java resources now
you might guessed it right we are working on Java so of course we have to write Java code now if you're thinking Ser it will be something very different not exactly whatever you have seen in in your code Java so it will look on the same page now this is where you will create all your Java codes so whatever files you will write you will You will do that here in the in the resources but time being again will not do that let's go back to web content and let's create a simple page because that's
what we wanted right we wanted to create a page which will print hello world so what I will do is I will right click here and I would say create a HTML page and that's the beauty it's not just about Java it's about making a web application and HTML Remains the default default design language I would say the Page name is index.html that's the page I want to create and in this page uh you can see the page went to the web content folder this is where you'll be having all your public Pages now in
this it will give you certain tags like head tag title tag we don't we will not be using that okay I should be increasing the font size here so you can see we got our page here we got the text so you can see we have head tag we don't need those tags yet Now we have a body tag and we'll be using so yeah we have a body tag here and in this body tag I will simply print hello world okay we got hello world here and then Simply Save this but how will you
see the output it's very simple just right click on your project that's one way or you can right click on the page itself and you will say run as run on server if you have a server it will of course run on that I will click on run on server it will ask you for which Server as I mentioned you can have multiple servers will mention that I want to work on a on Tomcat 8.0 click on next now it will say Okay configur a app which is we are working here click on finish it
will restart the server the server which you are running here Tomcat server click on okay it will restart the server it will deploy your application on the Tomcat then that's why we said deployment you it will shift your project into your tomcat and you can see That we got the output we got the output as hello world is as simple as that so yes we can create a static page in your Java web project as well this is the address which we are using and we got hello world now you might be wondering this should
be getting open in a browser right why it is getting opened in Eclipse the amazing thing about Eclipse it it has a internal browser so you can see the the browser here this is the internal browser using this browser you You can actually go to any website uh you can say I want to go for I want to go to google.com even it will take you to google.com yeah this is the internal browser so in case if your external browser not working someday you can use Eclipse browser but that's not the main reason why you
use this so yes we can we can write about code we can test our code here we can see the output but I'm not a big fan of internal browsers I love external browsers because let's if You are also using some designing tools or designing things like maybe jQuery JavaScript CSS in those terms it's better to test your app on the external browser so what I will do is I will I will switch from internal browser to external browser the way you do that is by going to a window and say web browser and you
can see that it gives you option of internal web browser we don't want that I want to go for the default or we can go for default one or we can go for Firefox so let me select Firefox here so that I will see the output on the Firefox and let me just close this window now and let's read on this so I will simply click on this and okay I know that we have to select Tom CAD why you asking me every time so you can say always use this server and click on next
and click on finish the moment you do that it will take some time and it will open Firefox can see that you got the output on the Firefox browser now so This is how you create your first project in web uh in Java but then we are not yet started with solet why do we need solet here so let's create a scenario where you will need solet let's say you want to add two numbers how do will how will you do that uh you might be saying hey we can use JavaScript and we can add
two numbers but then hold on uh adding two numbers can be done with JavaScript but what if you want to do some server side coding you want to Fetch data from database you want to uh perform some operations which is which requires some server L processing not just uh on the client side in that terms you need to use server side language which is Ser in this case so just to explain that I will go for a normal code which is form action so this is how you send a request to server right we use
form action so you say form action and here you will ask for two values so let's say I want to I want to ask for uh The first value the second value that's what you do on the page so I would say enter the first number and I will give a colon and here I would say input type equal to text I will say name equal to uh num one because this is where you will enter the first number so I will say num one and after that I want a break tag and I would
say this is the second number and I will change to number two I got two numbers I inan two text field I will insert the numbers I I Want a submit button here so I would say input type equal to submit now this is where you will submit your submit your request but the question arise where I mean who will handle the request once I've done with the coding here let's go back to Firefox and let's verify if we got the design and yes we got it design here and if I say 7 uh maybe
seven and 8 it should print 15 right so if I click on submit qu you can see nothing is happening it's because we have not Specified anything in the action you will say hey add those two values so this this is the URL which you are using which is ADD and now if I go back and if I say the same request first of all we have to refresh the page and I will click on submit query you can see that now it is sending a request to add and see the in the address bar
the request is going for the add request but unfortunately we don't have any page called as ADD page so how it will work And that's where you need to create a seret so in the last video we have seen how do we create a project in Eclipse which is a web project in this case and we have also created one page which is index.html so what we are trying to do here is we are taking two values the moment you click on submit we want to call add but what exactly this add is the purpose
here is just to add these two numbers that's it we can do that with JavaScript but here I want to use a Server site technology and we want to use Java and the only option you have is servlet so what we will do here is let's create a selet but how do we do that it's very simple actually just right click on your project and say new here I will select a class but you might be wondering we are making a seret right it should be something new or not exactly we can create a simple
class and that will behave like a solet I know you don't trust me I will click on class Here I will create a simple class which will add two numbers so we will call this as our ad solet again you can use any name I love this name which is ad solet I will go with that and here in the package I will mention the package name I would say com. Tesco you can use any package name but then you have to make sure that your package name is unique and that's why we always use
a reverse of your domain name because your domain names are unique so of course Your package name will also be unique and here I will click on finish a very simple class so you can see we have a very simple class which is doing nothing now in order to make a solet you just need to do one thing there are different ways of creating a serlet we will talk about the life cycle of serlet and what are the complete hierarchy time being just to make it simple I will use a class name which is so
we just have to extend a class which is HTP salet so you Just need to extend HTTP seret and your job is done the moment you extend a servet class your class becomes a seret so you just have to extend HTTP solet okay now this sounds cool the next problem is when when you click on this submit button it will call add right and we are expecting when you call add it will call add seret will it be happening automatically that's different thing we will talk about that later but imagine if this class is working
if you want to Perform any operation you need methods that's what we have learn in Java if you want to perform if you want to do any any task you use methods now here as well I have to use a method but which one now salet says if you want to use a a method name you can use a service method because serlet is a server component right and what server does it provides service so we can use a method which service can I use some other method name the answer is no if you want
To make it working you need to use a service method name now service is a method which belongs to a Ser life cycle so of course you have to go with this method name so I would say service and here I want to accept the request from the user and I want to do some task now the question would be how a user will send a request I mean that's okay so what let's say if you got a request here how a user will able to send data and this svet is able to receive data
as we Have seen in the video the theory video which we have done whenever you work with service method or any server you have to work with two different object one is the request object and second is the response object so you have to pass a request object here so I will say HTTP Ser request object and rdq the thing is you don't have to actually create this object by yourself this object will be created by Tomcat you just need to get a reference of it so you don't have to say New keyword or nothing
something like that you simply say HP Ser request and you say RQ and then we'll say HP Ser response so basically we need these two objects here now using the request object you can fetch data from the client and using the response object you can send the response to the client so it's all about request and response right now in this method service once I got this object what I will do is I will try to get data from the user now a user Is sending two values right so I would say in I where
you will have the first value so I would say R EQ now this request object will have your data so I can say get parameter because when a user is passing values a user is passing parameters and I would say I want to accept the first value but what to mention here now as a user you are sending two values right you are sending values in Num one and num two the first value is there num one so I would say I Want to fetch value from num one okay now there's one simple problem get
parameter will give you a string what we are expecting here is an integer so we have to convert that right and the way you do that is by using parent our favorite method which is parcent and this string will be getting converted into integer the same thing can be done for the second variable I would say I I mean J here so this is J in J integer. parent and here I will say num two now You can see we got both the values we got I and we got J the only thing we have
to do now is add them and the way you do that is by saying int K = to I + j a simple code Java concept okay now the question would be we have done with the addition how do I print it so normally in code Java what we do is we say system.out.print and I would say uh result is and I will print the value of K so I'm expecting that this should work Now will this work work let's try I will run this application I will go back to index I will say run
as run on server so you can see we got this page here and in this page I will I will say 34 and 21 or 12 so the answer should be 46 let me just click on submit oh it's not working it says 44 our favorite error right and there's one thing which you have to observe look at the address bar we are sending a add request that is perfect and we are also passing the values so we Are passing the value of num one and num two in the address bar so this is called
as a query string so quy string is a part where you can send your data from a client to a server so let's go back to the server now the question would be why you're not able to call this ad servet for the obvious reason right if I if I look at this ad you I mean when you click on the submit you you're calling add right so you are calling add and you have a Serv name which is ad subet don't You think it will get confused between this two we have a add subet
here and we have ADD here to solve this problem let's try to understand the flow the moment you click on submit now since you are using Tomcat Tomcat will say okay you are requesting for a page the request will go to Tomcat but Tomcat don't know that this this is the class you have to call now it will ask you hey programmer you a user is trying to call AD Sol you must have defined the flow Right so whenever user call add it should call AD solet where you have configured it and unfortunately we have
not done that yet so Tomcat will look for this file which is web.xml web.xml is also called as a deployment descriptor this is where you will have all the configuration so if I open this you can see we don't have any configuration for this suret this thing we don't need as of now I will I can remove all this stuff that that was a Welcome page what I want here is I want to configure my svet so I want to say whenever a user request for add I want to call add selet and the way
you do that is by putting two tags here one is a subet tag and the next one is subet mapping tag you just need to use these two tags that's it a subet tag and a subet mapping tag now in inside subet tag you have two subtags here so one is a subet name and the second one is suet class in the subet mapping again you Have to Define two TP one is the Sate name and the next one is URL pattern now this is unique right we have a Ser class name that makes sense
we have a URL pattern because you are sending a request right so if you look at the address bar this is a basically a URL so that makes sense but why we have sovet name it's because in your application you might have multiple suets may not be 1 maybe 10 12 50 100 you're not sure now for every sub you need to create a Serlet tag and a seret mapping tag so there will be a confusion which serlet mapping is linked with which subet and that's why we use the subet name so these two are
linked so they should have the same name and I would say the name is ABC again you can go with any name we time when we are going for ABC you have to make sure that if one pad which is Ser and ser mapping they should have the same name if you have another subet you'll be having another pair of this Now what about the subet name or the subet class the subet class is the Ser class name you have to mention which is ADD seret and the URL pattern is your add so slash add
because you're requesting for a add request so add will call add seret but then we have to provide a fully qualified class name and when I say fully qualified class name it means with the package name so the package name is com. telescope dot so you have to mention fully qualified name So a class name with a package so com. telescope. addet now everything seems CCH it should work now now how to verify let's right click on your Ser right click on your server say start go back to your browser let's refresh this page I
will say submit oh something is happened you can see there's no error okay so we have done with the first step at least we have not getting any error if I go back to the browser or the eclipse you can see we got the answer But the answer is coming in the console but why on the console uh for a very simple reason you in the code if you can see we are printing the data on the console so whenever you say system.out.println you always print data on a console I don't want to print it
on a console I want to print on the page the the client page you can see we got a page here but this page doesn't have the data now to understand this question arise who is sending this page it's Actually a serlet sulet is sending that page in the format of response object now in order to write the data on response object you cannot say system.out.print in you have to say it is response. get writer. println so basically you are taking the object of response and then you are fetching a writer object of it and
you're saying print Len I know this looks confusing so what I'll do is I will try to create this object outside so I will say Response. get writer so if you can observe this get writer get get writer returns an object of print writer so I will say print writer this is what it returns so I will say print writer out equal to rs. getet writer we have to import the package for print writer first of all and here instead of using get response. get writer I will say out so what you're doing is you
are printing data on a response object this is the object which goes from the client from The server to client now it is throwing an exception it says it might throw IO exception uh so you can say throws that's one of the best option you can go for so I would say add throws declaration our job is done and it should work now the data should be printed on the client machine I go back here right click on the server and say restart it's doing something let me just go back to my P browser and
I will say refresh click on submit can you see that We got the output on the client page this is what we expecting so this is how you create a simple s which will accept two values and it will add those and it will print data on the web page just to repeat we have Creed a Class A simple class in the in any package you want you just need to extend HP solet class in which you just have to define a meth service you cannot go for any other name other than service you cannot
say ABC pqr yes we do have some two extra Methods we can use but time in let's focus on service it takes two objects request and response in the same sequence and then you need to uh fetch those two values add those two values and you can print it you can do you can do anything you want but this is what we have done this example so I hope you understood how to get a how to get a salet and how to send a response so let's make a quick recap whatever we have done till
now so if you can see we Have an HTML page here so when you run this code let's run this code first I will say run as on server so it's opening a browser and you can see we got the output page which is it is asking for two numbers I will say three and four or 34 and four just to test what is happening so as you can see we got the answer as result is 38 now the point is when you talk about this protocol which we are using here which is htd protocol
now this protocol has certain methods to Work with now if you're coming from networking background if you read about H protocol we have five methods we have get post put delete and options uh but then we don't use all in the web page normally when you want to send data to the server we use a post request and when you want to fet data from the server we use a get request now here by default it is using a get request now get request is something which we use when you want to fetch data and
post is Something which we use when you want to submit data now here you can see we are expecting an answer but then we are also sending values right now whenever you use a get request the values goes into address bar but then hold down where did I mention that I'm using a get request Now by default if you don't mention anything in the form It Is by default get method which means if I say a method by default is get I don't want to work with get I want to work with post now in
That scenario what will happen is the the data which is you are passing from the address bar will not be using the address URL to send data example uh if I remove this get and if I use post because by default is get I want to use post by doing that I will go back to my page I will refresh it and I will send the same data which 34 and4 now this time you can see we got the same answer but look at the URL it's not there the query string which you're passing is
not There so that's the beauty about a post request now when to use get and when to use post so whenever you want to fetch data from the server go for get when you want to submit data to server use post now when you submitting data it can be also your username password so when you want to verify your credentials you use username and password right so in that scenario use a post request now here as well I mean in this case we don't want to maintain any secrecy we can actually Use get request but
then just to understand how post works we are doing this so post will not put your data into a URL now doesn't matter you're sending a get request or a post request request will go to service me will execute now I have one more Point here the point is I don't want to work with get request I mean as a as a programmer of of this suret I want to say I want to accept only the post request I don't want to accept a get request now can we control That you know you can once
you get the request object you can actually fetch what type of request you are getting is it a get or a post request but then why you have to manage all those things sofl says especially HTP s says Hey programmer don't work I will give you two extra methods one to work with post and one to work with get so instead of using service we can also use do get or do Post now in this case I want to work with post request so I can simply use do Post Now by saying do Post what
we are making sure is this will only work with a post request it will not work with a get request so let's run this code and let's verify it so right click here and say or we can simply restart the server that was that should work and if I run the if I restart the server let's go back to our browser and here if I refresh this page say submit and you can see it's is working so with the post request do post is working but how about If I go to a client page and
say Hey I want to work with a get request in fact you might be thinking uh as a client why they will change now I don't know if you know about this uh if you know the browsing tool but then we in the in the browser you have a way to change it you can see that the get request not working with this so what I was saying is you know even if you mentioned the post request on this sub I mean in the code if you mention post request still as a User you can
change it from the post to get request so you just go to inspect element and there's a way you can just go here and say hey uh the programmer who is designed this web application said you will be working with post but as a user I want to do some experiment I'll be going for get request so yes you can change it and you can the moment you say submit you can see it will not work because a programmer says Hey user if you want to call this software the only Way is to send a
PO post request you it we will not accept get request so yes that's the control you have so it will only you work with a post request not a get request so refresh and submit this is a post request it is working so yes we have two methods one is do get and one is do post so we have two options do get and do post but how what is happening behind the scene when you click on this submit button it will check what type of data you're passing So you are sending a post request
so it will go to the web XML file and say say hey I I'm searching for a URL which is ADD so your web ex says hey go to add salet but your Tom Cat internally has a service method because as you can see we are extending HTTP solet so request will go to service method for sure request always goes to service method so in the SV solet we have a service method so if I search for service here and this this is where you have service now depend Upon your request it will call a
do get method or a do Post method so even if you are mentioning do get or do post it will be called through service so that doesn't mean service is not in the picture now because we have started this series talking about service method right so yes service method is still there but what is doing is it is running behind the scene so it's that cool now how about if you want to work with both the methods you can do that actually you Can Define both the methods here one is uh dug get and one
is Dupo so you can copy this code and you can say this is do get so I want to have a get request and a post request both uh and both will perform the same operation you can you can do that it's it's an option for you so you can use a get request or you can use a post request with the help of do get or do Post now don't you think we are repeating the same code so do we have a solution so that we can write it Only once yes you can you
can just create another method and in that method you can just write all the statements and what you will do in do get and do post is just call that method particular that's it your job will be done so so that's how you work with do get and do post for this example we only need do post so let's remove this and it will work so once we have talked about those two beautiful methods which is do get and do Post in fact I just want to do One change here let's continue our discussion with
get method and here also I will say do get not do post and let's verify this if it is working so I will go back to my server as usual let's restart the server and open your Firefox here we go let's refresh and say submit it works can you see that it is a get request it works perfectly now what I want is let's say I want to add two numbers which is happening in this case and I also want to find a square of this Number now maybe we have a scenario where you want
to add two numbers and you want to find a square of that it's very easy actually you can you can say k equal to K into K and then you can find a square but this is a very simple logic right let's say you want to build a very complex logic and the logic which is finding a square imagine this is a very complex logic and then you are already doing that in some other solet now in general what I'm Talking about is let's say if you have two seret one is a square seret and
one is ADD serlet can I call a seret from a suret it may not make sense in this example why do you call a Serv from a seret but have patience in in the in the further videos we'll discuss why do we call a Ser from a S because we are also going to see JSP and we'll see how all this works in the real life but time being let's say you have scenario where you want to call a solet from a solet Because in this case what we are doing is we are calling a
s from a page right what I want is I want to get one more salet the way you the way you do that is by very simple just right on resources in fact I on your project and say next I want to create a new class and I want to put this class in com. telescope I hope that's the same package we have maybe the class name I want here is a square svet and this Square salet will be extending a class which is HTTP solet Okay let me just browse it and it's here finish
and you can see we got a class which is which extending HB Sol instead of doing that you can also do it here now in this I want a method which is public Vo do get so that I can send a request and of course this do get need to except two things I will simply copy this code I'm damn lazy to type it so I will just uh do that okay can that we got do get method now what I want is I want to find a square of a number okay First of all
I want to call this Ser later we'll try to find a square how do you know that this server is getting called it's very simple just print something I would say print writer out equal to re S.G writer this is how you get the writer object so that you can print you can write something on the page once I got the right object and I would say out. print Len and I would Sprint hello to square that's it I just want to print this message now in this Page index.xml page I'm calling I'm I'm sending
a submit request which will call add this ad will call add solet but I want to see the output which is hello to sq how do you call that particular solet it's very simple actually just after this line in fact you don't need to print anything now because we are not going to print this data here I want to print that on the second saet to call that subet basically we have two options one is request dispatcher and the second One is redirect again I will talk about this in detail in the 3D video the
two difference between these two time let's focus only on dispatcher request dispatcher so how do we use a request dispatcher because we are dispatching the request from one Ser to the seret the way you do that is by creating object of request dispatcher you will say request dispatcher and I would say Rd again you can use any name but then how will you get the instance of request Dispatcher because this is a interface unfortunately so the way you do that is by saying req which is request object dot there's a me call as get request
dispatcher this will give you the instance of reest dispatcher here you just have to mention the subject name in this case not the subject name basically you have to mention the URL I would say I want to find a square but the question arise how will you use this Square okay when you send a square request how will You call this one and we have one more thing we have to also say rd. forward by passing those two beautiful objects which is request and response so you have to send a request object and a response
object and you can see there's exception so I would simply say add a throw there okay everything looks fine and it need to call this saet but it will not work why because if you remember if you want to call any salet we have to make Sure that in the web XML file you mention that salet mapping right so we need to create the serlet and Serv mapping for the SQ serlet so the request we are sending is for SQ and we have to change the name if you remember we say pqr here again it
should be logical name but then just to show you that it can be any name I'm using this pqr pqr ABC ABC and this Square the sub is square subet right so when you send a request for SQ it will call Square subet so let's go For this uh flow so when you click on submit it will call add seret from the ad seret I'm dispatching the request to sq but we don't know what is SQ is and for that in the webml file you mentioned when you to get a request for SQ you have
to call SQ suet and this is your SQ here now will this work that's the question just let's restart the server or maybe I will just run this here so I will say run as on server and let's enter some values I will say 34 and this Time let's say two submit and you can see that we got hello to sq so yes we are able to call a s from a seret the next thing would be I just don't want to print hello to sq what I want to do is so this seret should
be able to pass the data the K value to the SQ salet how how will you do that because see we are forwarding the request right now there's one way you can do that is by using a concept of session management so normally when you want to send data from One server to the seret or maybe you want to share data between different cets we use a concept of session management now we'll talk about session management later the easiest way to send data from a seret from ad Ser to sq seret is think about this
this request dispatcher is forwarding request object and response object right and the same two objects are being used here so don't you think if you can put some data in the request object anyway you're passing That object right so can you just put the value K in request object something like this can you say req. set attribute so yes you can set an attribute by saying hey this is a k value and the value itself is K now what is two k's here this is a key and this is a value so you're passing a
k value which is maybe whatever value you're passing from the user and then you are naming it K so that when you want to refer this on the another subet you can refer that with K So basically we are able to send this K value from add seret to sq seret but how will you fetch it here now since you are setting the attribute in the request object here you will fetch it from the request object I would say intk equal to R eq. get attribute so we got get attribute and here you have to
mention K so we are passing the value from K okay now it returns an object of an object what you to do is you have to convert that into integer format so you can Simply typ cast it so whenever you have an object format you can typ cast it to int and that will perfectly work and here I just want to print oh first of all we have to find a square of a number so we say k equal to K into into K and here I would say I don't want to print the value
I I don't want to print hello to sq I want to print uh result is K so now it will so when you when you send a request now it will first get added those two values and then you will Get the square as well will this work let's verify let's go to our index page or maybe I will just simply restart this server and then go back to the browser here I will say refresh and I will send a value so that we can see if it is if you're getting the square square of
a number 6 and 2 which is eight addition and then when you square find the square of it it should be 64 and we got 64 right is that simple this is how you call a salet from a salet so in the ad Seret you use equ dispatcher now there's another way using which you can do that that is redirect and we'll see redirect in the in the next video so in the last video we have talked about what is salet how saet works right so we have also seen the life cycle of Sate so
the first method in the S life cycle is in it then we talk about service and then we talk about destroy now if you observed in the Practical implementation when we have Implemented that service method right we we have provided two objects one is request object and response object so those two those two objects are here so in this video we'll talk about those two objects one is HTTP request object and HTTP response object now why we need do these two objects so let's say we have a client machine here and then we have a
solet now when I say solet it means server when I say server it means solet because we are learning a server Technology so we have a subet here and then we have a client so let's say this client want to add two numbers so if you want to add two numbers that means you have to first send a request and you have to also pass those two values I mean let's say let's add two and three so if you want to add these two values you have to pass those two values right of course now
how do you pass those two values maybe not just this values I want to also talk about what is the client Address I want to also send about what I mean which browser client use so if you want to send all those information you have to pass it in some format right and since we are working in Java technology we will work with objects so what we can do is we can put this values in in an object right so we can put those values in an object now this object which is going from the
client page to the server will be called as your request object so request object is an object which will Hold your values now once this Ser got the request you have to send a response right so what we have to do is when you're sending a response okay when you send a response what you what you send so B basically sometime we want to send a data we want to send a HTML tags we want to send a video we want to send an image so you can send all those information in one particular object
and that's your response object so while sending the data we'll be sending a Response object now this response object will have all the data which a client need right so request object will have all the data which a Serv needs and response object will have all the data which a client needs right again it's depend upon Ser what it what what it wants to send right so let's say in this scenario a Ser want to send a value five right so this response object will have a value five now it can be of type this
data can be of type text this data can Be of type HTML this data can be of type let's say a video so you can have all this format in your response object again you can specifically I mean you can you can specify what format you want to go for right again how to do that that was in the P implementation but that's that's possible you can specify what type of data you sending you know in terms when you talk about the web services world where you return Json value you don't return a page or
a text Value what to return a Json value in that scenario you can specify what data you want to send from the server machine to the client machine with the help of response object okay so we have request object here and then we got a response object now where do we use it so if you remember in service method we use two objects one is request object and the next one is response object again when you say request object is a part of HTTP Ser request and response object is the Object of HTTP server response
but then question arise can I do we do we need to create object of it and the answer is no we don't have to create object of HTTP server request your tom cat says Hey you want an object I will give you the object so Tom Cat will give you those two objects request object and response object you have to specify what you want and Tomcat says I will give you that okay so in the service method you have to specify the references so that tomat Will give you those objects to you make sense so
yeah so these two are actually interfaces okay HTTP server request is an interface HTTP server response is also an interface not a class but the implementation is done by the Tom Cat and object will be given by the tomcat and that's why when you want to run a web application you need a tomcat server right so yeah so that's how your request and response object works this is the theory part of it we'll see the Implementation of it as well in the next video we'll talk about how do you call a Serv from a seret
is it possible let's see how how to do that in this video we'll see how do you call a saet from a seret so let's say we have a seret one here which is S1 and then we have S2 so these are two different svets now in the first scenario let's imagine you have a client here and client want to send a request to S1 normally we do that by creating a Button on the page or you can submit form so when you click on submit it will call S1 that's when you configure it so
it will call S1 and S1 will send a response to client now basically there are two objects which comes into picture one is the request object and other is the response object so a client will send a request to S1 so S1 will use a request object to fetch the data from the client and S1 will response will send a response to the L time let's say That that object is request one and response one because we have sered one here but let's say I have a scenario where subet one should be calling subet 2
so instead of sending a response here a subet one is supposed to call a subet 2 how do we do that so S1 can call S2 with the help of a special feature called as request dispatcher so we have a concept called request dispatcher using which you can do this let's name that object as our D so requ dispatcher Object is Rd here so using this Rd you can call S2 from S1 so you just have to create object of requ dispatcher and you have to mention the name for the S2 which is Serv 2
it will call it but normally we also forward these two objects so when you call S2 you send the same two object which is request one and response one to the S2 so that if you want to send some data from S1 to S2 let's say if you have a variable called as X in that X variable we have a value Which is five and that is there in the servit one if you want to send that to Serv 2 what you do is you add this value X in the request object so that you
can access that data in S2 as well because anyway the second subject will be using same two objects now if you want to send a response to the client a client will send I mean the response which we get from the client is same rs1 so re1 goes to the client page because the same Object is getting forward here right so client don't even know that response is getting from where is it from S1 or S2 in fact in the URL so in the URL the address is still for S1 so a client as a
client okay when I say client I'm talking about the browsers okay so a browser don't even know that from where getting the response is it from S2 or S1 because in the address bar you still have S1 that is dispatcher now this works fine when you have the same Website let's say the all the software we have S1 we have S1 and S2 on the same website uh let's say the website name is tesco.com so if if this on the same website it will perfectly work but what if you have a scenario where S1 belongs
to the.com but S2 belongs to some payment Gateway let's say PayPal so let's say if you want to donate to pay tesco.com you will be using PayPal for the service now in this scenario tesco.com S1 will send a request to S2 Which is Paypal now in this scenario client should know that something is happening behind the scene so this is another scenario where we so if you want to do this a client so the request to PayPal should be done from the browser so when let's say if you if you click on donate button on
this on the is.com the is.com will inform browser hey you are getting redirected to PayPal and that's what happens when you buy something online so let's say when you go to Amazon.com to buy something when you click on check out it takes you to payment Gateway it let you know that you are getting redirected to payment Gateway now in that case you cannot use request dispatcher right because a client don't know in the dispatch that you you're getting you're getting redirected in this case you will be using a special meth called as send redirect uh
so we have a method which is send redirect in servlet using which you Can actually know your client will know that you are getting redirected to the to the next salet so in the other scenario S1 will call S2 but in this scenario S1 will inform in redirect S1 S1 will inform your browser hey you are getting redirected to S2 that means when a browser so let me just do it once again here so imagine we have a browser so we have a browser here now browser send a request to S1 now S1 let's say
S1 want to call S2 but we cannot do Directly right so S1 will inform browser hey just go to S2 now so now in the first scenario it is your request one for the next request for S2 a client will send a new request which is req and a client will get a new response which is re S2 in redirect you basically send two different requests this is important to remember yeah so you are sending two different request because in dispatcher we were using the same object to send data like example if you want to
send This X if you want to send this X in this S2 you can simply do that with the help of request object but in redirect you cannot do that because in redirect you are using two different objects two different request objects and that's where if you want to maintain the data if you want to send data from uh if you want to send X from S1 to S2 if you want to use that X here then we have to use a concept of session management again you will understand this once you start Working on
the Practical implementation this was just a theory just to get the idea how this dispatcher and send redirect works so I hope you understood something from this video you will get more clear idea when once you see the implementation of it so that's it once we have seen the theory of request dispatcher and send redirect let's Implement them in fact we in the last video we have implemented request dispatcher right the Practical video now Let's Implement r so the idea what we are going to do here is we have a page when in which you
will be asking for two values the moment you click on submit it will send a request to add and when you send a request to add it will call this add solet that's what we have configured in the XML file and once you call this it will call do get method because we are sending a get request then you'll be passing this two values I mean you you're fing this two Values you adding it and then you are setting the data in the request object so that when you f a request or when you send
a request to a next next to which is SQ by using a request dispatcher you can simply forward by passing request and using the same request object on the second solet what we are doing is we are fetching it from the request object we are storing in our variable and then we are performing the operation and printed now in this video we'll try to do the Same thing but with the help of some other thing which is send redirect now how do we do that so what I will do is I will come in this
section the thing is I how do I call a second servet because without this thing you cannot call them so that's a question how do we do that so what we'll do here is now since uh we are not sending a request object let's remove this because in send redirect we don't do that so we have a response object so we'll say response dot send Redirect because in this scenario what you're doing is this object which is at solet is sending a response to a client by saying uh response do send we by saying hey
client just go to square servlet that's what you have to do go to a square servlet now a client will send a request to the square Ser let's see what is happening behind the scene so I right click here I will say run as run on server let's get back to Firefox and here I would say any number doesn't Matter so I would say eight 7 and five click on submit you can see we got an error it says null Point exception do get what's that in the SQ server we have the issue the issue
is not with the ad server the issue with the SQ Q seret now if You observe the URL it is SQ because what is happening is you are sending a request to add but your add solet says hey go to sq so your client is sending a request to sq and if you see so send redact is a method belongs to response Object and you simply say redact to sq now the issue is you can see in the request dispatch the in the request object you're trying to fetch the object of course we are not
sending request object then how can you use the same object here that's why you got you got that another Point exception but basically let's not try to do all these things we'll try we'll we'll do this once more with the another example but then here let me just try to print Something so that you know I will get some output at least on the console yeah nothing somewhere maybe on the console I would say SQ called just to see if if this Ser is getting called or not uh I will restart my server once I
do did that change and I will go back to my browser and refresh send and you can see it is able to call SQ by doing nothing you can see we're not we're not getting anything on the page but if you see the console in the console we got SQ called So yes we are able to call the second solet from the first salet and the one there's one way one way you can verify this the request is going to sq that means this is the second request not the first request so in total a
client is sending two request add request and uh SQ request now the point is how will you send a data because that's important for me right I want to send this data okay now what if I will go back there and I will Uncommanded object so I cannot say get get attribute but how about think about this if I say get parameter will this work so I would say get parameter and I would say data is coming from let's say k itself okay I mean of course we are not sending data but uh let's try
let's try let's do some experiment here and I would say request object. get parameter I'm expecting that the data is coming in the K format normally when you say get parameter the parameter comes from in a Request you know is it a get request then if is a get request it will coming from the browser address URL remember if it is a post request then a different way of fetching data but the data should be coming from the client let's try so what I will do is I would say uh I have to convert that
of course I have to say integer do pass int and I will convert whatever whatever coming on the way so the value of K will be converted into int and your calculating it now let's Verify I will go back to my server as usual restart it go back to the browser and refresh the moment you click on submit again we got the same problem right number format exception because the data which you're getting is null and you cannot format it but what if just after the question mark after the SQ if I say question mark
and K equal to 9 case that it worked the moment you pass a value in the URL it worked because the second subet is asking for a Value from the parameter and normally when you say parameter it's it's a do get request so of course the data is coming from the from the URL and that's why it is working but what if I want to make it work without even changing the SQ me the address bar or the URL in that case what if in the ad servit itself I can give leave a question mark
here and I will say k is equal to plus k i me think about it the the response which a client will get is a new URL which is SQ Question mark k equal to whatever the value you're passing let's try with this I will restart the server and I will I will submit the same value 7 and5 click on submit can it worked and in the address bar if you can see SQ question mark k equal to 12 so that's one way you can pass a value from one subet to another subet with redirect
because in redirect what is happening is this s which is ADD subet is responding to the client by saying hey client go to sq and The URL mention k equal to 12 because the value of K is 12 and the same thing is requested by the client so this is requ requested by the client thanks to ad seret and this is what you're getting now this type this technique comes under session management again we'll discuss session Management in a theory video but then we have a concept of session management management where you can transfer data
from one server to another Ser with the help of three concept one Of them is session one of them is cookies and the thing which we have used here is called as URL rewriting because you're changing your URL and that's why it is called as URL rewriting so that's that's one of the way again we have two more ways one is session one is Cookie so we'll discuss that in one of the video in session management but here we have worked with rud and that's how you forward the request so yeah that's it from this
Video so what we have done till now is simply get to this page which is index.html where I I'm asking for two values num one and num two when you click on submit the request goes to add where add will call your ad serlet where you are accepting these two values you're trying to add them what we also trying to do here is we are trying to call another subject or we are sending a request which is SQ which is handled by the SQ solet now there's one thing we Have to remember is we are
using a response. send redirect which means the ad solet will send or it will respond to a client by sending a signal by by sending a signal which says hey just go to the SQ salet now SQ subet will try to find a square of a number but which number we have to send this K right and we have done that in the earlier video we have we have talked about this that we can pass the value by saying SQ question mark and then we can pass a Value so the way we do with the
query string but do we have another option let's say what if you don't want to use url3 writing which we have done in the earlier video now you might be thinking what's a drawback and doing that because it works right what if you want to pass multiple values what if you want to use the same value in multiple subet not just this subet maybe you have ADD subet which calls SQ subet then SQ seret which call some other subet in that scenario If you want to maintain data throughout the session oh that's a good word
now there's a concept of session which means whenever you go to a web application it will try to maintain a session for you and that will be done by your Tomcat of course how will you use a session here because session is there throughout the throughout your visit there right if you visit One S second Sol third solet it will be there for you so what if you can put your data into session so once you Put your K into session here you can use that session value in the next salet how do we do
that as I mentioned session will be given by Tomcat we just have to get the hold on it and the way you do that is by using HTTP session so HTTP session is an interface we just have to say HTTP session session and as I mentioned the object will be given by Tom itself you simply have to say request. get session we just have to get the hold on it and once we got a session Object with us we'll simply say session dot set attribute because session will be there throughout your visit to the website
right if you go to ad Serv if you go to sq servit even if after some time if you go to some other page which is of the same application the session will be available for you provid you don't close the session provided you don't remove the value from this session we can do that by the way so we'll say session. set attribute and I would say The value is going in a format of K and the value itself is K so I'm sending a attribute which is K and the value is K how do
we how do we access is that here how do we fetch the value now of course we are not sending data into request object we are sending data in in a session so the way you fetch the value now is by saying so we remove that we'll say session. o we have not instant we have not given the reference for session here we have not declared it here so I Would say HTTP session HTTP session session equal to the same thing a request do get session and once we got an hold on it once we
got the hold on the session object I would say session dot get inside ad subet we have said set attribute here we have to say get attribute it's that simple and here I would mention I want K okay now there's one issue when you say attribute it means it will return return you the object we don't want object what we want Is the int format right so we'll simply type c we'll say int and we are done so this will give you object and we have to convert it into int we just simply type casting
it again we can we can say integer presentant as well we just have to convert it into string but multiple steps right this is simple we see we can simply type cost it with int now this should work let's verify I will go back to my HTML I will say right click run as the on server so it is opening my Firefox and here I would say two and three which is 5 into 22 which is 25 answer should be 25 and we got it can you see that we got the answer so first it
is adding those two values and then it is it is finding a square of a number quite simple right so we can use session as well now as I mentioned the data will be available in session for you throughout provided you don't delete this data from this session can we really do that yes we can actually we Can delete data from the session uh so we can once you got the hold on it we can say session dot uh there are there's a way you can say is it remot attribute yes we can say that
we have remot attribute and you can remove K here now once you remove K of course you cannot fetch the value so you can fetch this value only when you have the value in the in the session so using this you can remove it that's how you can use session normally we can use session to maintain The login so let's say if you log in on your website and if you want to verify if you if if a user goes to another page and if you want to verify the user is already logged in you
can you can maintain the data in the session that's what we do this looks cool right so session looks cool do we have some other way in fact we'll be working on session more in the future topics once we start with JSP but can we do something more here and the answer is yes what we can Also do is we can use cookies as well so the way we have session we can also use use cookies now how that that works so what I will do is I will not use session anymore let me remove
session let me remove session from everywhere I want to use cookies here so I would say intk again and I would say zero how do we do the same thing with the help of cookies let's get back to add and here I would say I want to work with cookie now what is Cookie basically so whenever you send A request to the server and a server will send you a response now in that response object so the response object which is coming from the server will have a cookie and then when you when you send
the request to sq sub you can send the same cookie again so difference would be the cookie is coming on the client side and then you're again sending the cookie to the server now if you want to imagine this imagine you're going to a shop to buy something or Let's say if you are going to a restaurant or if you're going to a restaurant where which provides you Self Service where you have to buy the product first you have to pay the amount and then you will get the get the uh dish or whatever you
want to eat so let's say you you want to buy something which uh which cost you around let's say 50 bucks and you are giving 100 bucks so you don't have a change with you so the the product cost you around 50 bucks but You you're paying 100 bucks and the shopkeeper says or the restaurant person says hey he don't have a change now of course you don't even you don't have a change he don't have a change what you will do he will say okay wait for some time once I got the change I
will give it to you you go back to your seat and then after half an hour you go back to the uh Casher and say Hey I want a 50 50 bucks back now will he remember you of course not right after half he may Forgot who you are so in the earlier scenario what you should have done is the moment he says hey wait for 30 minutes he will give you a token on that token it will it will write he will write 50 which means when you go back to him after some time
you can show that token which says 50 that's a cookie when you go for the first time he will give you a token so that when you go back to him after some time you can use a token I I hope you understood something about That example here I will try to implement that with the Java code so I would say cookie that this is how you create a cookie you say cookie uh cookie basically belongs to javax package Java x. ser. HTTP and I would say cookie equal to how do we create a cookie
it's a class basically so we can simply say new cookie okay we got a cookie which ask you for two parameters the first parameter is the name of it I will say K and the value of it which is K as well So I'm have a name which is K and the value is K we got a cookie house we got our cookie we cannot simply pass K here it should be string how do we convert this K into string we can simply append a double quotes right so integer append double Cotes is string now
once we got this cookie we just have to add this cookie to the response object so that it will go to the client I would say response. add cookie and I will pass cookie that's done we are sending this Cookie to the client now this means this add s will send the response to the client and client again will send a request to sq seret now when a client send a request to sq seret again it will send the cookie as well oh that means we have to first accept the cookies and the way you
do that is by saying oh how do we do that so when a serlet send a response to the client it is response object right and next time when a client send a request to the SQ seret that Means the the cookie will be coming in the request object right so I will say req dot do we have a get cookie method so I will try to get guess that oh we don't have get cookie we have get cookies that's weird why get cookies why not get cookie it is possible that on the client side
you don't have one cookie you have multiple cookies quite possible right we don't want okay so it will give you all the cookies because client even don't know which cookie you Need so client says hey I will give you all the cookies you figure it out what which cookie you want okay so since client is sending all the cookies I have to use an array of cookies so I would say cookies array equal to let me import the package as I mentioned we are not getting one cookie we are getting cookies so that's why we
have an array and from this array I want to fetch the cookie which I want how do we know which we which I want for that I will use an Anounced for loop I would say for in fact you know I will just declare this variable up here so that I can use it later I would say for enhance enhance for Works something like this I would say cookie c equal colon cookies it will give you it will run a loop through through all the cookies and then I would say what how do we we
fetch a cookie now so I would say first of all I want to know which cookie I want so I would say if the cookie name so I would say cookie C do get name is it the uh yeah so we have get name if the name equals K that's what we want to compare right if name equals K this is our cookie I would say k equal to C.G value so we don't want all the values we want only value of that cookie which has a value which is the name of with the name
with K okay now it says get value will give you object I get value give you what string we have to convert that into integer so I would say integer. parent okay so this Is how we can fetch cookies and let's verify if it is working I'm not sure let's run this code let's verify I would say restart so here it is asking for two values I would say 8 and six and if I say submit you can see we got 196 which is the square of one of square of 14 so it works so
yes you have multiple options in the last video we have seen URL rewriting in this video we have seen how do we use session I know how do we use cookies to send this data uh let me Know if you have not understood the example here of cookie I will try to create a theory video where I will explain how that cookie flows from a client to the server so that's it from this video we have talked about session we have talked about cookies I would also recommend you to practice this code try some try
different examples not just addition of two numbers you can also try login username password uh you can also try to use maybe you can you can do some Other operations as well so try it out and in this video we're going to talk about Sate config and seret context now when we talk about servit config and servit context we use these two things to get the initial values for a suret or for the application let's say example if you want to specify a username password if you want to do any initial setup let's say if
you want to specify the file path where you will do it so normally what we do is we have this XML File which is web.xml in which you can mention some initial parameters and then you can use those parameters in your code example let's say I want to specify my name and which phone I'm using so the way you can do that is by specifying a tag called as context param and in this context param you can specify two things now this will be a a name and a value pair so you can see we
have param name I would say name is let's say my name I would say name name and then the name For the the value for that name is my name is naven so I would say naven here and then I will use one more context param which is for which phone I use so I will say copy and paste here and I would say uh which phone I have so I would say I have a Samsung phone so I specified two things here one is the name and second is the phone which I'm using so
these are my initial values but how will I use it so first of all let's let's see how will I use it so so if I If I go back to my Sol file again this is a simple solid file here and in this in this do get what I will do is I want to get that name right I want to get the phone or name so first of all I will create object of print writer so that I can print something I would say print writer outon dog writer now once I have a
print object with me let me import the package for that and then I will try to print something would say out. print and I will print hi that's it I will say hi And I will give a new line so I would say BR I just want to print a new line and then once I have done this I will go for the next thing which is I want to print the value now the way you can fetch the value of my name or the value of a phone which is Samsung in this case we
need to use a special object so we have to create an object of subet context so basically sub context and sub config the both are interfaces and the way you can see if I go hold on that you Can see it says I which is the interface in this case so when I say subet config or subet context in this case if I subet context I would say con equal to I would say CTX let me UT the package for that now if I want to instantiate it or if I want to get the object
how would I do that now luckily your Tomcat says hey don't worry I will give you the object of Serv context so the moment you run the application it will give you the Ser context object for free you don't have To create it and the way you get the access to it is by saying get serlet context there are multiple ways of getting this object uh one is by using get Serv context second by using request do get Serv context if you want to use it because when you say get subet context you are getting
this method from HTP seret because hpet extends genri subet so you're getting this object from there or this method from there ultimately we are getting object of ser Context Now using this object I can say CTX do get init parameter now get init parameter is a method which will give you the value of an attribute which which you specify here example if I specify name here it will give you the value of the name which is navini in this case but how would I know it so I would say print St Str equal to so
I would say string s Str equal to CDX doget init parameter and then I will print the SD there so I will say print Ln I'll print SDR so it should print high naven so because I'm Printing High and then I'm printing my name as well let's try let's try this so I will say run as run on server and you can see that we got the output which is hi BR no okay the the BR tag is not working yet because I'm not using HTML tags and I'm using internal browser maybe that is the
case here it's not working or maybe I will use a space maybe I'm using internal browser does the issue but Ultimately we can see we should be getting output which is hi naven right okay now that's your subet context object that's how you can fetch the value in fact you can do the same thing for uh for phone if I say if I remove this if I say phone of course you can fetch both phone is p Capital so we have to say p capital and it will print it will print the phone now so
if I refresh this you can see we got high Samsung that's how you can fetch the initial Values okay that's that's best thing normally we can use file path the way the location of your database maybe your username path for database you can set all those things here but we also have Ser Ser config right why is why it is useful now seret config says okay first of all Serv config in Serv config as well we can spef we can specify the initial values the way you are doing with Serv context the same thing can
be done with Sub sub config as well the Only difference is in your application you might have five to six subet maybe 10 subet how maybe multiple subet the subet context is an object which will be shared by all the svets which means if you specify this values here this will be shared by all the svets what if you want to have a different configuration for different servlets example when I say name which is naven for this particular Serv which is my Serv maybe I have multiple servlets here for this Specific selet I want to
specify the name as let's say sukes I don't want nain there so if I fetch name it should print Suk and the way you do that in the Sur tag itself because subet config works with particular subet so if you have five suets in total you will get five subet config objects but you'll be having only one subet context object uh so I would say init parameter here init param and in this init Prime you have again the same stuff we have to say Pam Name I would say Navi uh I would say name here
I would say value which is in this case I would say Suk so the init Pam here is suc so when I run this code now oh first of all to CH make some changes I've have changed my web EX file which is here and in this case what I will do is instead of fetching value from Ser context I will do that from Ser config this time so I would say Ser config and then on this side also we'll say servit config let's change it to CG Let's import the package for servit config and
here as well I say CG do get init pram now you can see the code remains same the things which I've changed is from servit Context I went to servit config and here also I'm saving Ser config now if I run this code if I restart the server and you can see it says hi Suk even if you have the context value as naven but subet config value is sukes right so it will give you the priority to that particular seret now if You create a next let's say if I create one more servet as
second servet or maybe addition servet in that subet if I try to do the same code but not with Serv context but let's say Serv context so if I Ser context here and if I say name name because we don't have we don't we have not specified the config there so it will print naine so if you have multiple svets which will share the same value of course go for subet context and if you have if you have a different Value for different cets good to go for servit config so I hope you got some
idea between servit config and ser context and in this video we'll see how do we configure svets with the help of annotation to understand that let's go with a flow so what we have done till now is we got this page which is index.html where we are asking for two values the moment you click on submit it will send a add request now when you send this add Request it should call add seret but question arise how your Tomcat knows for which request you have to send what request and which Ser to call and that's
where your deployment descriptor comes into picture so this is your web.xml right so this is where you mention Hey whenever you get a request for add you need to call add sovet whenever you send a request for SQ you have to call SQ sovet so this is where you do all the configuration right and it's okay you Know people do love XML but not all not everyone loves XML you know some people find XML blit clumsy to work with they want to work with annotations because annotation will make your work much easier so what I
will do is whatever you write in web. XML I will just simply comment it because I don't want to use XML anymore in fact you can delete this code code but just to show you this code for reference I'm just keeping it here but that is commented so this code will Not be coming to coming into action when you run this code so now if I comment this part and if I run this code one again let me do that I would run as R server and you can see in the Firefox I'm asking for
two values I would say 4 five moment I click on submit you can see we got an error error which is 44 now why 44 because when you send ad request you don't know or your Tom Cat don't know which Ser to call right because we have commented because we Have commented this portion here the another way of doing this configuration is with the help of annotation so what you can do is on top of your seret you can write web seret so you can say web seret annotation which is this one and here you
have to mention for which URL you want to call this suret so I can say whenever anyone sent a request for add just call this suret the same thing can be done for your SQ subet now so we will go back to sq subet and paste but this Time to call SQ Ser we have to use SQ and the same SQ we are using here you see that so we have the the same configuration what we have done in XML so in XML we have to do so many tags but using annotations it will reduce
lot of your work right you don't even need this XML anymore so you can delete the entire part so you can just simply remove this so you can see we have blank XML file and if I go back here let's restart the app restart the server something went Wrong you can see that we are getting error okay so I guess we are missing a slash so we have to put a slash request you have to mention slash before the request URL because that's what we have done in XML file remember so let's do that and
restart the server and you should be getting your output so you can see that no problem in the in the server now and let's go back here let's refresh and click on submit you can see we got the answer so there are two ways of Configuring one is XML second is annotations and annotation will make your work easy in fact if you have Lear hibernate or any spring framework or any other framework we have two choice we can go for XML or annotations and recently we are using annotation for all the projects so instead of
using XML you can go for this option as well this is how you use annotations so that's it from this video in the next video we'll see why do we need JSP and in this video we'll see why do we need JSP till this point what we have done is we got this form so when you click on this submit button it will call add and we have a sofl who is responsible to handle your request and it is working perfectly right so you can see we are asking for two values we are adding those
values and then we are printing on the page so just to see the output while what I will do is I right click here I would say run as on server And on the Firefox that's what that's the browser I'm using so if I say four and if I enter the value let's say six so it will add those two values that's it so if I click on submit you can see we got 10 so so we are simply adding two values now let me just add some twist The Twist is see ultimately on the
client page we I mean on the client side we are getting a page right so if I right click and if I say view Source you can see a view Source itself is this Data there's no HTML tag and nothing so we don't have HTML tags right because we are not building anything we are not creating any HTML tag here and that's fine even if you want to make a web page you just have to make sure that your response is of type HTML you don't have to write any tag but what if I want
to customize something what if you want to change the background color maybe I want the background color to be blue here or Canan or maybe red how do I change a background color even if I want to do that where should I do it now if you want to do any if you want to send any tags because see ultimately this client page or the client side will receive HTML tags right so if you want to send HTML tags you have to do it from here so that means with your text you have to also
send the HTML tag so you have to say HTML open and you have to say head open and then you have to say in fact We'll we can skip that part and we can simply say body BG color equal to in single code I'll mention the color as and that's my favorite color so once you are opening everything and inside this body oh we have we forgot to close the body tag and that's very difficult you know to track the tags and if you make one mistake you will not get errors you know in HTML
you don't get errors you don't get output so that's one thing which is scary and how do we close all The tags again we have to say print Len In fact print will do and here I would say body close first you have to that's that's important right you have to remember the sequence as well so you have to close the body tag and then you can close the HTML tag now you will say that's okay right we can do that so we can simply pass the HTML tags in the format of out. printer and
as a user a user will receive all these tags in a proper format let me show you the Outputs of restart the server I'm using a shortcut to restart the server you can try it by right click and restart let's go back to file Fox and now if I say refresh and submit you can see we got a page with a different color and the output is output 10 so if I say view Source you can see we got the HTML tags as well and everything is working fine the thing is to create a simple
page we have to write all these tags right and it is possible for us normally we write Each tag in different out. printer l so that we can debu later here I'm just doing some Shortcut is working what if you want to create a beautiful page example if I go to website let me just go go to Wikipedia and inside Wikipedia if I go for any text let's say if I go for any title doesn't matter uh so let's say if I search for this earthquake okay we got this page right and if I right
click and if I say view Source you can see that we have so many text here and We have to write everything inside out.println so just to create a beautiful page you have to write all those things in out. print Len do you think is it flexible of course not right and that's where the problem starts because designing is done by designers and the backend work is done by developers and so developers sometime don't like designing and designing I mean designer people don't they they don't like development how they work Together because the Java developers
they will write Java code and they will ask designers hey designers just put your tags inside outo print Len and they will not like it right so all these designers they went on strike they said hey Sun microsystem we don't want to use Java technology because in Java we are supposed to write HTML tag inside Java code that that's not right so Java says okay don't worry we will give you a new feature which is called as JSP in JSP We'll do reverse you know we we will write the Java code inside HTML oh I
know you don't trust me let's do it so what I will do is I will do the same thing I want to add two numbers but this time I will use JSP now what is JSP JSP stands for Java server Pages now instead of using serlet we can also use JSP to do the same thing a serlet here is simply accepting these two values adding it and sending the response the same thing can be done with the help of jsb Let me show you how so I click on your project and say Hey I want
a JSP p and I will name this JSP as add. JSP we can have any name doesn't matter and this JSP you can see that we have HTML tags here and then inside this HTML we will write Java code oh first of all I want to change the body color right that's important so I will say BG color equal to I would say Canan that's the color I want and here I want to add to numbers right so of course we have to write the Same code so I will just simply copy this code fetch
the data and add it okay there's one thing missing now the request object right in serlet we are creating the creating the reference here in GSP we don't have to do that JSP will give you this object by default so JSP will give you this uh reference variable by default now these things are also called as implicit objects in JSP again we'll see that later so once we got this Java code now I want to print the data On the client page right so I would simply say out. print Len and your job is done
you don't even have to create object of out or print writer because that's by default in JSP I would say output uh coar I would say plus and that's it so what we do in salet we write HTML Cod inside Java in JSP we do reverse we write Java code inside HTML and this is much simpler right because you are focusing on HTML here and here you're focusing on Java you're not Mixing it up they are separated with oh we have we are not separating them how do we separate them because if I run this
code as it is it will display this content on the page I don't want to display this content on a page I just want to run this code in that case you will use a Del meter so you you will use angular bracket percentage and you have to close this tag as well so here you have to say percentage angular bracket so whatever code you write between these Two things it is it is it will be Treet as Java code so that's simple right so we will not be using a subet now let me just
close this I will only use JSP so you you don't have to send a request for add you have to send a request for JSP name so whatever JSP name is in this case the JSP name is add. JSP just mention that so simply say add. JSP it will call this add. JSP so no need for any annotation nothing this should work let's verify so I would right click here I would say run as on server you can see that we got the output here and I mean we got the page I would say 8
and 9 you can see we got 17 and oh is it really working okay what I will do is I will change the color I doubt there something I think there's something wrong going on let meart the server I guess server have not restarted that's the problem so let me just go back here refresh and submit oh it's working so I was waiting for this thing which is add. JSP I know we Hate red color it is very it's giving some trouble to the eyes I will go back to C I was just verifying everything
is working okay you can see we can see we got C is a pleasant color right and you can see we got add. JSP on in the address bar so yes this is we are calling JSP here so which is better JSP right because in s we have to do so many things uh we have to get a class we have to extend a class we have to get a method which takes two parameters and Then create the object of print writer out out and then you can do other thing in fact in that also
if you want to design something we have to write HTML tag inside J inside Java code but in in J in JSP it's very simple right you write HTML code you write Java code separately and everything works fine so yes JSP is a good option but then why to learn sovet if JSP is so beautiful why to learn sovet so we have some uh reason for that I will uh so in the next video We'll see that why we still use sovet when you have JSP can we work with JSP and sovet together I hope
you enjoyed this video where we have seen why JSP came into picture and how we can use JSP that we'll see later on seret we have to create a seret we have to define a method which is mean which is service method and then we have to create object of print writer then you can print hello world in fact when you want to add two numbers we have to Create all those Stuff Plus we have to create uh we have to fesh the data from the request object right and for that we have to define
those two objects request and response in case of JSP you get all those objects by default right that means JSP is much simpler to Cate and from today we'll be want we'll be working only on JSP we will not go for cets right but that's not the case if that was the case then why would why would anyone will learn saet why Directly we can start with JSP so what if I say what if I say JSP I mean you cannot run JSP right you can only you can only write code in JSP you cannot
run JSP it's because if you remember when you work with Tom Cat we have suet right so sulet runs on Tomcat because Tomcat is also called as a Sate container right that means in Tomcat you can only run suets you cannot run JSP but then we have seen the output right we have created a JSP page and we can Run JSP page on Tomcat how that is happening then Sate cannot run JSP so what happens is as a programmer even if you write a JSP code that JSP code gets converted into serlet right so it
doesn't matter if you even if you write a JSP page that is getting converted into seret the only advantage of writing GSP is it is is it is easier for developers to write JSP files instead of working on servers right so question arise how can you Convert so it's actually not serve to JSP we are moving from servey to JSP topic but when you run it it goes in this direction so you write JSP page and that's get converted into a serlet but then how exactly is possible so let me create so on the left
hand side I will create a serlet class and then on right hand side we'll compare that with JSP how exactly or or we can write a JSP page here okay okay let me just just let me just go for JSP here and S light here So how exactly it's get converted so we have a JSP right so in JSP we have all the HTML tags right so JSP is all about HTML and then somewhere in between we have Java code right so let's say we have HTML tag we have body tag and all those tags
let's also write body tag here so body open and then somewhere here we have to close body tag and then we have to close HTML tag right we have done that now what here so here s here what we will do what we will be Doing is if you want to write a Java code let's say if I want to add two numbers so what I will do is I will say in I equal to and we know that we can use a request object. get parameter right and let me let's say this is a
get parameter method in fact let me just make it some smaller because I have to write sub code there as well so I will say this is uh T1 right so I got this value from T1 again you will say this will give you String you have to convert that into integer so let's say I'm doing that here so I'm saying integer. pass in and everything is here okay so I'm doing that I'm doing that stuff I'm converting everything right and then the same line here we'll say int J equal to the same thing now
we got these two values I want to add these two values we can say int K = to I + J and then we can say out do print and we can print the value of K it's that simple right so we can we can Assign those two values and we can add those two values and then we can print the output the same thing if you want to do in seret how how do you normally do it right so let's let's ignore this topic I mean Let's ignore this logic if I ask you to
create a saet I mean it doesn't matter what you want to do in the saet if I ask you to create a serate what is something which which comes to your mind step one we have to create a class right you should provide the name Of the class you have to extend that class with HTTP serlet then you have to create a method a service or maybe do get or do Post you have to pass two parameters request and response you have to create object of print writer and then you have to close everything that
means it doesn't matter what is the logic of the seret you have to do all these things and I forgot one thing import the package right so you have to do all those stuff here it doesn't Matter what is the logic you have to do all those things right so let's say if I if I create a subet here I will say public class and I will name this subet as demo subet which extends of course which extends HTTP subet right we have that thing there if I open this you have to define a method
which is which is a service method right so I will say a method service this service will take two parameters request and response so we Got these two objects if I open this in this we have to create object of print writer which is out and then we have to do something we have to complete something so this is object of this is the your print object so it doesn't matter matter what subject you want to create this is something which is which is there right your JSP says hey don't worry if you want to
write some logic here write the logic right in JSP we are not doing any of those stuff so what Happens is when you when you run GSP page it will convert itself into a salet so what it will do this is fixed right but hold on there's something which we have to Define here the class name so what happens is your JSP name let's say if this is demo. JSP your class name will become it is demo I mean demo JSP do JSP so that's your that's your class name right so whatever file you have
so it is demo I mean not JSP it is demo JSP okay so Demo. JSP will become demo JSP there so whatever file name you mention here that that will become on your subet name then that will be extended by HTTP subet you don't have to do it right it is is which is standard this is something which your which your tomat will give you it will say okay we will give give you a service method do get a do post this two object will will be provided by your tomat so whatever you write inside
this code whatever you whatever you write inside This tag will I mean this will go into the service method so it will it will directly copy paste so this will be copied here and that's why you get this out out object you don't have to create an out object in JS page because your sub will give you that object you don't have to create a request object because your Ser will give you that request object you don't have to create a response object because your Ser will give you that response object not just These three
objects even your session object your application object everything will be given by JSP I mean your toat server basically and those objects are called as implicit objects in JSP again we'll be seeing that in the session in the future sessions okay so yeah that's how it works so it gets converted from JSP to Ser it's that awesome right now what else uh I just have one more query here what what what do you call this what do you call this Seret I mean this tag not s this tag so this tag here in JSP is
called as scriptlet so it is script let so this tag which is angular bracket percentage sign will be called as I mean this sign will be called as scriptlet okay now what else we what other what other thing we have see whatever you write in this tag will go to your service method right so so this code will be coming here so what you write in this script light will be Coming in service method what if you want something here I want to declare a variable I will say int uh let's say some other variable
let's say value I want to create a variable something like this see whatever you write inside scriptlet will go to this service method right I want something here in the Declaration section I want to declare some variables there how can you do that if you write that inside this tab it will go inside service now What the choice so what we will do is let's let me just expand this imagine we have we do have all those things up we have HTML tag we have body tag somewhere there so we have one more type of
one more tag here see if I use this tag again it will go to service so what it will say see I want to put some data which is not in service okay I want to put some data or some declaration or some statement which is not that inside service it is out outside the service so We can use a not operator there so we can say not right so it is a service but not service right so it is declaration so this section will be called as declaration so we can so what you write
inside this so if I say int value equal to one so this code will go inside declar section right so when you want to Define any variable which is which should be here which is a instance variable if you want to create any method let's say with this service I Want one more method as show so you can you can create a show method inside this tag in the Declaration tag so if you if you create a method here that method will be going in the class so whatever you want inside the class and outside
the service method that will go into declaration section Perfect Right do we have one something more we have one more in fact we have two mes the first one is uh let's say you want to import a package in your file somewhere somewhere Here you want to import a package maybe date API maybe date class maybe uh database driver class if you want to import any package so what you do in Ser we simply say import and then we write it there right so normally we import the packages outside the class what about JSP where
do I mention I want to import the package so for JSP we have one more tag in fact in fact I will remove this Ser it to JSP so we have one more which is percentage at the Rate right so if you write in at theat you have to say at theate page then we have to say UT and then we'll say equal to double in double codes you have to mention java. u.d and percentage close so whatever we write here we we go into your import package so since you are mentioning import attribute so
that's that's something you have to remember this import is an attribute we have to assign the value to it we say equal to and then You have to mention the package name what if you have multiple package you just have to give comma so it is java. u.d comma java.sql do prepare statement comma java.sql do statement we can have multiple commas and we can assign everything there right so that's three tags so this is scriplet this is declaration now this tag is called as declarative oh this is not declarative this is directive my mistake so
it is Directive so this is this is directive this is declaration this is scriptlet now do we have one more yes we have so let's say if you want to print something so instead of using print out. print we can also use angular bracket equal to so we have to use angular bracket percentage equal to K so whatever you write inside this tag which is percentage equal to will go inside out do print K so whatever you write inside this this Tag which is percentage equal to will go inside your out. print right it's done
awesome right so this tag here is called as expression so we have four types of tags in JSP we have directive so directive goes as so if you want to import the package if you want to do any any other extra stuff that is directive if you want to write something inside the class outside the service method example method definition variable declaration You can do that in declaration section so we have to use percentage not if you want to write something in the service method we have to use angular bracket percentage percentage angular bracket that
is your scriptlet tag if you want to write something in out. printer and directly we can use the Expression tag right so this is JSP for you so whatever whenever you create a JSP page it's get converted into a sulet right so even if you're writing a JSP it will be into Sulet so again which is better depends if you want to show data on the page okay if you if you if your intention is to create a page GSP is better right but if your intention is just to process it for that we have
Sate again which one to use when that we'll see in the next video in the last video we have seen how powerful JSP is and how easy it is to work with JSP and then we have also seen that even if you have JSP which is so wonderful we still use solet and then we Have seen the theory video where we have discussed that whenever you work with JSP it gets converted into suret and that's why suets are better when you want to write logic so in this video I want to show you the practical
way of seeing how can you convert a JSP into Serv or even if your Tom Cat doeses that for you what code it generates to understand that I'll be using a different IDE this time because last time we were using eclipse this time Just to show that I'll be using net beans because net beans have this cool tool to do that now if you don't have net beans in your machine that's fine you can simply search for n Beans and you can go for the latest version which is 8.2 and download it and you can
install it it's very easy you can just click on download and you can simply install it there's one important thing when you say download you have to make sure you are going for Java e version Because Java is basically for code Java uh if you want to go for the Enterprise version which is which supports uh Java ja e then HTML and then the servers of course you have to go for Java e version now once you download it you have to install it now once you have done with the installation and if you open
that means this is how it will look like just close this start page we don't want the start page what I want is I want to create a simple project so what I will Do is I will right click here I will say new project now while selecting a project you have to make sure that you're going for Java web and say we application click on next we'll give a simple name here I would say to do JSP okay you can give any name doesn't matter and then click on next this is important you have
to select your server now while installing I have not checked Tomcat I'm only installing glassfish so it's there so I have a Glassfish in my machine uh if you have Tomcat that's fine Tomcat will also work and then click on next I don't want to select any of this thing here because we are not working on Frameworks click on finish and you'll be getting your first web project in net beans and it will give you HTML page which is awesome now we don't need HTML we want a JSP page so let's do that let's create
a simple jsv page very simple where you can add two numbers or whatever code you want to Write so what I will do is I will click on these web pages I will say new hey I want a JSP page and your net means will say okay I will give you JSP page just name it I would say this is home.jsp you can give any name doesn't matter right uh we don't have to say JSP it will be by your net beans that's what it was saying in the info info here and then you will
click on finish you got your page you got home.jsp and it doesn't uh it doesn't have any Java code Now what I will do is I will just increase the font size here okay that's done I have increased the font SI going to settings and now I'll be adding some Java code here now what what Java code we can simply say add two numbers and just just print it there so I'll be using this tags which we have done earlier so I'll be using this scrier tags and here I will type some code I would
say out.println and here I would say 7 + 5 so when when You get the output it would be hello world and 12 right that will that will be the output let's see and to run this code it's very simple just right click here and say run file it does take some time and I guess it was trying to open a browser for me so is it yeah it's there you can see that we got a page and it prints hello world and we got 12 as well so that means the Java code is working
but how can you convert this into solet and of course it is done by Tomcat or Glassfish you don't have to worry about it but if you want to see the subet file how it will look like so you just have to right click on your home.jsp and click on view solet now when you click on view solet it will give you a backend solet file it's simple to understand think with the go with the class itself now class name is homeor JSP this is derived from your from your JSP file name which is home.jsp
and it extends this class here It implements this thing doesn't matter what is important for us is the code which you have written and there's one more thing if you if you remember solet in solet we used to create out object we used to say print writer out this one so first you have to create the object and you then you can use it but in JSP it gives you implicit objects which simply means you don't need to create those objects it will give you by default and how it is how it is doing that
so if you Can see this subet file here all the objects which is implicit objects have been created here example we have page context object we have session we have application we have config out page uh then we have jspx out I don't know when when we use it and we have this page context object once more I don't know we I don't use it much but it gives you all those objects and it it is giving you all the prerequisite code now the code which we have returned so if you can see Our jsv
file if you see this file this is the code which you are writing right of course you're not writing it but it is done by your net beans but that's fine it is our code because we can change it and we have WR our Java code where it belongs to it belongs to out. write so in servit as well this is what you do right you write all the HTML tag inside out. print and or WR it is doing the same thing and look at your code here this is your Java code so as I
Mentioned one of the video what whatever you write inside that scriptlet tag or inside this bracket belongs to belongs to the service method or uh maybe do get or do Post depending upon what they have implementing so a service method here so whatever you mention in that tag belongs to service and that's how you can see what is going on here quite simple right so it's hence Pro what happens behind the scen so when we write a JSP code it gets converted into Sate and whatever You mention in those brackets belongs to a service method
now there's a question here and the question is normally when you talk about Java classes you know there is also a Java class right because it has a class it is it has certain methods and then variables now whatever I write inside this JS pin those tags belongs to the service method right but what if I want to write something outside maybe uh I want to declare a variable which will be used by multiple Methods so of course I'll be I will be making a instance variable and how do we do that how do I
create a v because of course you cannot change this file how do I change this if I want to create a variable here can we do it in JSP if I write if I create a variable if I say in I is equal to 9 now this variable will be created inside your service method I want to create this variable outside the service method that's the first thing we want to do the second thing I do is About the packages let's say if I want to import a package because here you can see we have
import package what if I want to import a jdbc package what if I want to import uh some other package maybe log file package how will I mention that in JSP because if you write import here the import statement will go inside your your service method which which we don't want I want to import the package which should be the first line of the code how I how do I do That so those things two things we have to do and that will be done in the next video in the last video we have seen
how this JSP gets converted into salet and we can see the salet code right and then we were having this issue now now the issue is what if I want to write this code which we have written here in some other tags example maybe in some other section uh so in sub edits I want to declare a variable as a instance variable how do We do that and how do we import the package so let's achieve that so in this video we'll talk about those four beautiful tags available in JSP now let me just name
those things for you first so I will write it here so the first one we'll be talking about is declaration the next one is directive then we'll we'll talk about scriptlet and we'll talk about expression so we'll talk about this Four Tags again we'll go in random order but then at the end we'll Be covering this Four Tags here now by default the tag which we use here it is called as a scriptlet tag so whatever tag which we are using here it is basically a scriptlet tag now why is script tag because whatever you
mention in this tag or in this section will be a part of service method so by default it doesn't matter how many tags which you write it's not that you you cannot you can only write one or you can have multiple tags here maybe before hello World also we can write those tag and if you want to perform some operation there so if I do this here if I say okay will uh so in this section I will say out. print and and I will type let's say hi so if I say hi now and
if I go back to my page and if I refresh uh you can see we got high and of course we got this text because I have written there but I got high right and if I see this GSP if I see the subet of this now if I say view suet we will be having that high Here you can see that it's working so thing is you can have multiple scripted tag but all this code will go into your service method what if you want something outside service and that's where your declaration tag comes
into picture example say I want to I want to create a new variable so if I do that here so instead of say hi I will remove high now and I will type int I want to create a variable called as a a coefficient and I would say coefficient Is three so if I say Co is three and I want this variable to be created outside service that is a instance variable the thing is if I if I just run this code and if I say view solet you can see if I say view salet
the variable is still not here the variable is inside service if you can see the variable is inside service method because by default that's what that's what happened what I want is I want it to be outside and the way you can do That is by saying hey this tag is not a scriptlet tag it's a declaration and the way you mention that this is declaration is by putting a exclamation sign there so as you can see we have declaration and we have scriplet so this is scriptlet this is declaration so we are saying hey
whatever code we have here don't put those things inside service so when you say don't in programming we say not right that's that's a not symbol but basically this is a declaration or you Can also say this is not scriptlet okay this is two tags we have we do we have something more example if you want to import a package how do we do that uh to use that package what we will do is let's use something which belongs to our packet example something like scanner so if I say scanner again scanner doesn't make any
sense in this code because we are not going to accept any value from the from the console but let's say if you want to use any class which belongs To our package in this case we are using scanner and if I say control space you can see it belongs to java. and by default java. is not the default package which you get you have to import it the moment I say enter you can see something has changed here we got we got a tag now this tag is not this tag is not simply uh angular
bracket and percentage we have one more symbol here which is add rate now whenever you have a add rate symbol or add rate symbol when with this Angular bracket and percentage it is called as a directive tag so this is directive so basically if you want to import a package or if you want to use some external tags that's where you'll be using dtive again we'll talk about tags later how can we import external tags and then uh how can we use different features of of dtive but time bin when you say added it that
is dtive tag so if you want to import a package this is what you do what if you want to Import multiple packages if you want to use start you can do that you can put start here now there's one thing you have to observe when you say import it becomes an attribute so we have to use a equal to symbol so if I say view solet for this file now and if I go up oh it is not compiled properly let just do that here I'll go back to this code I will say refresh
one so that or run file and if I go back oh it's not working there's some error okay scanner is Giving an issue of course because we have not written the code properly I will remove scanner there because we don't need that we only need to import the package so once I have done that if I go back to my uh Sol I hope yeah you can see that in s we call java.util dostar so this is how we use those three tags we have declaration we have directive which is added it and we have
scriptlet now we have one more which is expression and this this is very special Now what is special let's say I have this variable which is coefficient which is three what if I want to print the value of it maybe I want to print the value by saying my favorite number is so if I want to print this print this line and at the end of this line I want to print three which is the coefficient value how do I do that so of course I cannot simply type Co if because that's a string it
has to be part of java code so what we do is we simply use those Scriptlet tag we use angular bracket percentage and then we use percentage that will make it a Java code but then we can cannot simply type c we have to say out. print Len right that's what we do we say out. print Ln and then we can we can just put that in a bracket now if you run this code of course with semicolon and if you run this code now it will print my favorite number is and then it will
print the value of Co which is three but your JSP says hey just to Print just to fetch the value of variable you don't have to do all these things what you can do is you can take your variable and put a equal to symbol there the moment you put a equal to symbol which means you're trying to fetch the value of it so JSP is done and I mean in the back end JSP will convert this to out. print Len so whenever you have a equal to symbol with this percentage is called as expression
tag so that's your expression so basically We have this Four Tags here declaration directive scet and expression and I will again run this code once just to show you the just just to show you the serlet of it so code is running you can see the output is my favorite number is three that's working but let me let me see the solet of it so if I if I say view solet we got the input package we got the coefficient which is working we got in the as instance variable but if I go down you
can see it says out. print and We got CF so whatever you have you're writing in that expression tag becomes a part of out. print and that's the beauty about about this in this video we're going to talk about JSP detectives now as we have seen in JSP we can use different tags like we have script then we have directives we have declaration now in directive itself we have three types the first one is ad page we have ADD include and we have ADD tag lab in fact we have seen an example Of page so
when you want to import a package when you want to do when you want to specify the language we can use add page so if you want to specify something for the entire page we don't we don't do that in the scriptlet we do that in directive now example importing a package or maybe if you want to mention that this page handles the exception or not you can use add page there now if you want to use some other JSC file in your file example if you Already have a JSC page and if you want
to include that page in your current page you can use add include and then we have tag lib now as name suggest it has something to do with tags and the library of tags now what happens is when you talk about HTML awesome language right but HTML has limited set of tags which you use what if you want to use something very specific something extra now we can use external tags as well example when you work with spring Framework spring framework provides its own tags now if you want to use those tags we use add
tag Library again we'll see a a syntax for that now let's focus on the first one which is ad page again we'll be doing a example but let's focus on the theory first now if we talk about ad page page which is the page tag now page tag has a particular syntax so we have to use ad symbol and that's very important for directives we have to use ad symbol and then we use page now page Mentioned that we want to use we want to specify something for the entire page and the way you can
specify that is by using attribute and a value attribute and a value and we can have multiple attributes and and those values but what this attributes are this is a list of attributes you can see now this is not the entire list we have some more attributes but these are commonly used attributes which we use example we have the first one which is specifying the Language uh the scripting language in your JSP and of course by default it will be Java uh you don't have to even mention that and why why we'll be using different
languages in JSP so we have Java there the next one is extends now as we know whatever JSP which we use gets converted into seret right now what if you want to extend a particular class in your as in your servet so the way you can do that in JSP is by mentioning the extend attribute and then the class name What if you want to import some packages then we can use import statement so we have to say import and then we can specify the list in fact you can specify one package or you can
give a comma for multiple packages then we can also mention if you want to use sessions in your JSP so if you don't want session you can specify false then we have a concept of buffer now sometime you have a buffer concept so if if you are sending a data to a client and if you Have a buffer in place it will not send data till your buffer is full the moment your buffer is full then it will send data what if you don't want your buffer to get full I mean what if you want
to send data before you get before your buffer gets full we can use autof flush then we can specify the content type example when you send a response to a client it can be HTML page it can be a it can be a image it can be a PDF file so you can mention the content type in This content type attribute then you can mention the error page now example when we talk when we be talking about exception handling in future but let's say if you want to specify the page which will be handling the
exceptions you can do that in other page you can mention the page name here and if you want to specify the current page error page itself again you can use e error page you can set it through again we'll be doing that in detail later so let's Not waste more time here and if you want to specify some information about the page you can do that here we also have a concept of eel which is expression language again we'll talk about that later but once we use eel if you want to ignore e you can
specify that here and then we have thread save concept so if you want to make your JS page thread save you can just make it through so these are the attributes which you have for ad page now if you want me to show The example for that it's very simple just go to your JSP so I already have a JSP page you created which is home.jsp and if you want to use ad page you can see that we already have it in fact when you create a new j page in Eclipse uh it will give
you by default displ attribute you don't you don't have to mention that so when you say page you can specify the language again this is optional by default it is Java so don't have to mention that and you can see if I go Here it says default value is Java so you don't don't have to mention that then we have content type which specifies it is text or HTML and we have page encoding as well again which is not not needed now if you want to specify some more attributes you can do that here so
you can say hey I want to import a package you can do that so you can say import and you can say equal to in double codes you can mention the package which you want to import maybe I want to Import java. q. star you can simply do that in fact not just entire package you can you can pick one interface if you want so let's say if I want to use statement now again it's not a good practice to use SQL queries in JC page but just for the example it will work so you
can see I'm saying import java.sql do statement what if you want to import more classes it's very simple just say comma and you can mention java. random so you can mention these things Here and it will work what if you want to specify a mode import and you know sometime it doesn't look good to import all the thing in one line so you can you can actually write more import statement so you can create another page tag and we can do that we can have multiple page tags and then you can specify import and whatever
you package you want to import maybe I want to import Java do uh util do add maybe I want to use it so you can do that so you Can have multiple import now import is the only attribute which you can use multiple times in your in your directive otherwise for for for content type for language you have to set only once now once we have done with the import uh let's we can we can explore some more example if I go back here uh we can specify autof flush we can specify eror page in
fact we'll be doing that datat in detail so let's not focus here so we got our we we know how to use page and We have to we know how to use input now if I go back to this uh we after page we have so this okay this we also have an example here you can see that now if I go for include so the next one is include now what is include Lo normally when you create when you design a page we have this habit right we create components example every page will be
having header every page will be having a footer so instead of writing code for header and footer for in every page what You can do is you can just write header code in one JSP and you can include that JSP in our current JSP example in in this example we have home.jsp right and then we have head. JSP let's say in head. JSP if I write some code uh let's say if I use equal to symbol and if I say 7 + 6 this this is a Java code basically I'm doing that here just to
print on number 13 here and then this head up page I want to include that in our current page you can simply do that You can just say at you can say percentage at and you can say include and you can mention the page name which is header. JSP and that's done so it will include your page here oh syntax it's file so include is a tag and then you have to use a file attribute and it is done and now if I run this code let's let's try this so if I will say run
as an oner I already have a Tom C in place let's say next let's click on finish and it will open my page okay you can see we Got the output and the output is 13 so this is not the code which which we have written in home we have written that code in header so your header is getting included in the homepage so that's the beauty you can use include tag and then we have one more which is tag lib now tag Li is something if you want to use external tags again we'll not
be discussing this example here we'll be talking this once we go with extra advanced stuff in JSP but let's say if You want to use external tags what you can do is uh we have to mention the URI because when you say external tags of course it will be having a library and that Library will have a URI you have to mention that URI here and then you can mention the prefix now why prefix let's say if I want to use a tag here which is let's say uh maybe naven tag and and we know
that nin tag does not exist now if if your browser tried to interpret this n tag it will give you error so for that You have to mention so we have to mention the prefix you have to say FX colon Navin which means this Navin tag belongs to FX which is this FX here again this prefix can be anything it can be a b or pqr doesn't matter so whatever prefix you mention here you have to mention that here so whenever I use Navin it means this Navin belongs to this Library which is this URI
and in fact if if you want to use H1 tag which belongs to HTML but sometime let's say If you want to customize H1 tag you want to have your own H1 tag you can do that so you can say H1 belongs to FX so don't use H1 of HTML don't just use HTML just just use H1 of my own Library so this is what you can do with tag lips so these are the three directives we have in JSP and in this video we're going to talk about implicit objects in JSP now if you
have seen solet in solet what we do is if you want to use any object like we have we have request object response Object those objects will be created by Tomcat but then still we have to refer them right example in service method we mentioned request object and response object and then if you want to print something we need to create object of print writer and then if you want to use session we need to instant we need to say HTP session then session and then say we get session uh in the in one of
the video we have talked about Sol context and config so yes we are Responsible to create at least the reference of it now some people do call it as creating object but normally we create reference so that it will be referred the thing is in JSP because JSP says Hey programmer I'm trying to make your work easy so it gives you a lot of features right example you can write a Java code inside HTML but JSP also gives you some objects to use now those are the seven beautiful objects we have request object response object
page Context now there is something new which we have not seen in zlet other than that we have seen request object we have seen response out session application now okay now this application is same as what you use in Ser context So when you say Ser context object that is referred as application in JSP and then we have a config object now technically this all this things should be in small data because those are objects right so we have response we have page context uh we Have out session application config so these are your seven
objects now we don't need to go into details here because we know this things right we are doing this from from session one we are talking about request object we are talking about response object now the beautiful thing about this is you don't have to instantiate them uh so if I have a JSP here first of all let me show you a svet so let's say if you have a svet you need to create this request object And response object uh also the out one then config context all those stuff but in JSP what we
can do is we can directly refer them example if I if I want to fetch a value so I can simply say request I would say request. get parameter and if you can observe there's nowhere I have I have mentioned about this request object so JSP gives you this object so you can you can easily use it now the same way we can use response object now there's something New here which we have not seen in s which is paste context so let's let's let's spend some time here now basically page context refer to the
page example let's say if you are working on jsb and if you want to work with any of the scope or if you want to set the attribute normally we set the values we get the values in fact in session management also we have talked about it we can in one sub we can assign the value in second sub we can fetch the Value that can be done with the help of request object that can be done with the help of session object or we can do that with the serlet context as well but let's
say if you don't want to go in detail on that if you just want to work with a page then JSP offers you a object of page context now page context is an object which belongs which is an object of page context now using this page context you can set the value or you can get the value so you can say hey I want To set the attribute and you can set any attribute here so that so that you can use this attribute later example if I say hey my name is naven so I have
assigned the attribute now so whenever I want to fetch this value naven I can fetch it with the help of name so you can do that with the help of get attribute Now by default it stays in a page so the scope of this page context is the same page now when I say scope what it means uh so when you have a Session scope which it means you can use the value anywhere in this session so let's say if you open five pages in that particular session you can access that value in those five
pages when you say request scope it means the current page and the next page so the page you requesting even that page can have the data in the request scope but if you talk about page context it is available only in the current page but what if you want to specify some some scope and we Can do that so we can specify the name then the value and then we can specify the scope as well so if you don't want this to be only in the page scope you can mention that it goes into session
scope as well so you can use a constant so we can say page context dot so we have a constant called as session scope basically all this are int value so session scope itself has a integer value we can also use request scope if you want so there is something new which we Which we can use in JSP uh let me just recap if we talk about Scopes we have different Scopes available we have request session sub context or we can refer sub context AS application scope but if you want to refer to the current
scope the only the page scope you can use page context Now using page context object we can actually set the value in different Scopes the way we have done here so that you can refer that in some other scope Some other pages and then we know how to use session we have seen that in in zlet then we have application which is nothing but your Ser context and then we have Ser config so we don't have to actually instantiate Serv config the way we have done in salet if you remember this example we said subet
subet config CG equal to get subet config you don't have to do this in JSP we can simply use config and it will work so if I say config dot so you can access all the Methods here remember this get and get get init parameter so you can directly use config and it will work so that's how you work with the implicit object so the best thing is s JSP gives you all this object for free you don't have to instantiate them again in subsequent tutorials will be using all this objects so let's let's not
spend more time here we we will see that in further sessions and in this video we'll talk about how do we handle exceptions in JSP Of course we have done that in Java right so when you work on Co Java when you work on suets it's easy to handle exception because we all know how to use TR CCH that's the general way right so if you have learned exceptional handling in Java you you know we have throws we have a TR catch now can we really use those things in JSP in fact if you have
S what we can do is let's say if you have an exception here you have a very common line uh we know about these Things right we have checked exceptions we have unchecked exceptions so if you say you have a line which is k equal to uh 9 0 now for sure we know this line will generate an exception but that is an unchecked exception so your compiler is not giving you any error for that so it's your choice if you want to handle this and it's always good this right now how do we handle
this so what we do is we write try and catch but can we do that in JSP uh the answer is yes so if I Go to this JSP and if let's say if you have some lines here but I want to ignore all the lines I just want to focus on one thing here which is int K is equal to 9 0 that's one line I want to write here of course there will be some other lines as well now how do we handle the exception here so if I run this code of course
you will get an exception or you will it will stop the execution it will generate an exception so I would say run as run on server and Let's let's see what happens on JSP so you can see we got an exception here which is error 500 we all love this errors so you can see that it is showing the exact code where you are getting the error which is uh line number 13 and this line line of exception now the problem is it is stopping the execution so of course it is this thing is viewed
so a client can see this code and the second problem is it is stopping the execution we don't want to do that we Don't want to stop the execution and the way you can do that is by using a try catch here so we can say a try let me put this thing in a try block and then we'll say a catch I would say exception e and in the exception I would print something of course not out do not system. print Ln I would say out. print Len and in this I want to print
K or not okay but of course I I want to print there was some error and I also want to print the message of error so I would Say e. get message that's what we do in normal exception handling right in fact there are different ways you can maintain log file if you want but I don't want to do those things here so you can see I'm trying to handle the exception and once I have done this I will go back to my JSP or the output page just refresh and you can see this time
it is not stopping the execution it is just it is showing you the error now still we have an issue here because if We talk about the web World in a web world if you get an exception you should inform your client hey what went wrong there should be a proper proper navigation you can see I'm printing the messages here itself this is not a good practice when it comes to web development of course for Co Java for stand application we can surely use this but when it comes to web worldold what we do is
we create a separate page because in your in your application you Might be having seven to eight pages of course you'll be having more than that so if you have so many pages it's it's better to have a dedicated page which will be showing the error which will have a different color of course you can print you can have the uh entire page in red color how do we do that so what we'll do is we'll create a separate page which is an page so what I will do is I will go back here in
in my web content I will create a new JSP and we'll name That JSP as error. JSP and in this page I want to show the exception whatever exception there was now how do we do that so first of all if you remember when we talked about the implicit objects there will be one object which is exception object but exception is pretty special that was that that's why we have not seen there example if I simply use a thing here which is the expression tag and can simply use exception object that's it but you can
See that it is not working it is showing exception cannot be resolved that's a problem here how do we solve it that's a different thing but I want to call this page uh maybe I I will just use this thing later I just want to print error till this point we'll do this after some time for timeing I would say comment this part okay I want to print error now the way you can call this page is by mentioning so somewhere you have to mention right that in case if there's Any problem in this page
you have to call ER JSP the way you do that is by using this page attribute page tag this is very important tag right we have seen that before I don't want to import import any statement here that was just for the previous examples so here I will mention whatever error goes in this page will be handled by this error page so this error page name is error. JSP so we are mentioning Whatever Whatever Gets wrong on in this page this will be Calling error. JSP and it should work I guess yes let me just
go back to my HTML or the output and I will refresh this time and you can see that it is calling it is printing something it's printing error and this error text is in error. JSP so for sure you're calling this page if you want to make sure you can also do this thing uh you can change your page color to Red because that's how you show the uh something is something is wrong right so if I refresh you can see we got An we got a red color so yes it is calling this er.
JSP but I also want to know what is going wrong the way you can do that is by mentioning is by using this thing so you can you can you can print exception message but there's a problem it it says it is unable to resolve the exception now the reason it is happening is this page should know that this is a error page and how do we do that so we in this thing you have to mention one more attribute which is is Error page we have seen this before right when we talked about the
page tag uh we have seen what is e error page so you can simply mention is it a error page of course it is so we have to say true and now if you have a error page so this page is error page right and that's where you can use the exception so point to remember exception is an object which only works in a page eror page and now using this exception object you can print the message you can say get Message whatever went wrong and now once we have done that go back to this
page refresh and you can see it says error div by Z that's that's your message right of course you can print whatever you want you can say get message or you can just whatever data you want to pick up from the exception object so this is this is one way or this is the best way of handling the exception in JSP of course if you are implementing MVC you'll be having a Different way of implementing it but if you are using simply JSP or sofl you can use this thing so that's how you handle the
exception there's no compulsion that you should be using this you can also go with try catch that's your wish but as we have seen it's always to follow convention that's how you handle the exceptions in JSP in this video we'll see how to connect a JSP page with database with the help of jdbc of course so let's say You want to create a web application where you have some pages and those pages need to fet the data from the database so what you will do so we have multiple ways right what you can do is
you can create a web project again you can create a normal web project like this this one or you can create a mavin project so when you create a mavin project the advantage what you get is you know you can add the dependencies I mean you can add the jar files by itself Right so that is one way the another way is you can create a web project then you have to add the jar files by yourself let's do it by web project if you know mavin you can go with go with that as well
okay so let's give a project name as demo JSP and we will select our Tomcat server again you can use any server which you have glassfish or any of this everything will work we click on next nothing to specify here just click on finish you can see we got our project Here and in this project first of all let me create a JSP page so we'll say a new JSP and we'll name this JSP as let's say uh profile. JSP so we just want to print the profile of a student click on finish and you
can see we got our JC page here just to show you something what I want here is to print the rule number 101 I want to print a name and then I want to print the marks let's say 88 I want to print this details and of course when You want to print this you have to use a br tag in between right and we'll say BR tag let's run this let's see if it is working or not so that we can go ahead let's click on the Run button click on next and finish and
run the server I hope it will not show any weird stuff on my screen okay so you can see we got a data which is 101 naven and 88 so this is what the page we are getting right but I want to Fresh this data from the database now do We have a database here so yes I'm using a post G database you can use any datab database which you want you can work with Oracle you can work with Mas you can work with uh any anyone in fact uh soon I mean later I'm trying
to explore more on post so I'm using post here but that's fine you can use any dbms which you want so I I already have a table here which is student and in this student table I have some data if I say view all sources you can see we have This data here so I want to fetch the last row so which is RO number 103 uh marks 50 and name is n so I want to fetch this one how can you do that how can you fetch the data from the database it's very simple
you can simply you have to use the jtbc steps now I'm assuming that you know jdbc steps if you don't know you can watch my video completely dedicated for for jdbc okay where you have to use seven steps still I will try to go with the steps the first step of Jdbc is let me open my suppl where I will well I will specify the steps so the first step we have to do is import the package or we'll say import package and the package we need to import here is java.sql dostar which which we
which has all the classes and interfaces which we need so let's import that package so normally when you want to import something in JSP what we do is we use this tag which is atate and then we specify not atate but percentage Atate right and then we say page then we say import that's the attribute and then you have to mention the package name which is java.sql dostar I want all the classes and interfaces once we have done with that the next step is you have to load and register the driver so you have to
load and register the driver now when you say you want to register the driver which driver I'm talking about so since see we have uh different dbms right and every Dbms has its own driver so when you say mascul it has its own driver post G has it own driver in fact Oracle has its own driver so if I go here for post gr the driver name which we use is this one which is org do post GSL do driver in case if you're using MySQL you just have to specify your driver as this com.
MySQL do. driver okay now since we are using post GIS here I will use a post GIS driver now since I want to add a Java code Inside my jdbc or inside my JSP you have to use we have to use a scriptlet tag and which you have to specify the first one which is class which is class. for name in this you have to mention the driver name which is org. postgressql do driver okay again if you are using myc then you have to use com. my. d. driver which we have mentioned here you
see that not here this one is For myql is for Port I just hope I have entered the right text yes the next step is oh hold on we are missing something when you say you want this driver this will be so your your machine will try to get this driver right but unfortunately this is a third party library and in your machine you don't have that can you see that in your web content you don't have that Library you have to make sure that you have your library in this in this folder So let
me just do that let me just uh add that here so again from where to get it you just go to Google and search for MAV reposit that's my you know this is the place I get all my repositories so just search for post post I mean post gql post click on search button then go to this thing and you can select any version I'm going for the first version I don't know it will if it work work or not but let's try uh so you click on the download Thing here click on download it
will it will download the thing for you okay so let me just add that in my folder just you have just have to paste it here okay so I have pasted that here can you see that we got our post driver here and it should work now let's try uh then so we have added that jar file The Next Step you have to do is you have to uh go for you have to create a connection so we'll say connection con Equal to for connection we got the package connection con equal to driver manager that's
the next step right so driver manager. get connection now in this get connection you have to specify three parameters the first parameter is the URL so let's create a string URL here we'll say string string URL and in this double codes you have to mention the URL that's your database URL again which URL to go For so if you're using post chis this is the URL so it is your so what you're saying here is you are connecting with jdbc so your JSP code is connecting with jdbc jdbc is connected with the post and post
is connected to this database now database can be on the network so you have to specify the network add address and the database name for post chis the driver name and the uh the port number is 5432 for MySQL the driver name and the the port number is 3306 and then This is this is one change you have to do in MySQL you have to mention MySQL okay so let me just copy this and we'll paste it there paste now once we got this the the next thing you have to mention is the username so
we'll say string username we'll say equal to for for post for my machine I'm using username as post if you're using MyQ you will get the default username as root so I will paste oh not this one I have to say Postgress Okay and then you have to mention the password now luckily I don't have a password in my I do have a password yeah I do have a password so the password for me is zero okay again uh depend upon what password you set for your dbms I have seted the password which is which
is zero so let me just type those things here we say URL comma username comma password you have to mention these three things now once you got the connection let's file the query Right now to file a query we need a statement object so we'll say statement St equal to con do create statement that's the step you have to follow right create statement then once we got this statement the next thing which we need here is we have to uh file the query so for that we need a query here so we'll say string SQL
equal to that's your query right SQL equal to now what want to fetch is the complete data we say Select star from student where role number is equal to one3 we we are very specific now we want only one record now once we have done that let's F the query the data will be going into result set we'll say result set RS equal to it is St Dot execute query where you have to mention the query which is SQL now once you file the query here you will get all your data in result set what
we have to do is we just have to say rs. next so that we will we will daverse between the Records again to get I mean to get more details about this how all these things works you can you can watch my video on jdbc if you don't know jdbc my main agenda here is not to teach you jdbc my main agenda is how to connect JSP and jdbc okay it's not that difficult is just that some people were having some issues so I'm making this video okay so let's print let's print all the values
so what will do is we'll say RS dots in fact once we got the data in RS what we Can do is we can go here and then we can say role number we can print the role number here we can use an expression tag here we can say percentage equal to R.G string and that will be your first string okay then we can use a BR tag here just to you know get the Break Tag then we can say Name colon we have to use a percentage atate equal to we'll say rs. get string
again uh I know that RO number I mean the the RO number will be an integer but we can fetch that in a String format okay why it's not working uh okay I forgot I mentioned okay RS dot get string what's that get string I can say two right uh we can take this in one line and then we can mention a br tag and then one more which is your marks we say marks colon let's copy paste this the entire stuff copy and paste and in here you can mention thre right we can we
got RO number we got name and we got Marks I think we are done let's run this so this this is the data which we are printing here and after this let's say if I want to have some break tags so that everything will be coming after two lines let's run this let's see if it is working or not it should work I guess and you can see we got the data from the database it's that simple right so you have to write all your jdbc steps inside jsv page so that's it from this video
we have talked about how do you connect a Database using JSP and in this video we'll talk about MVC design pattern using seret and JSP so in the last video we have seen that whenever you work with JSP it gets converted into servlet right so that means even if you're working with JSP you are actually working on cets right so but if if you talk about the coding part if you want to write a code JSP looks much easier compared to Serv right so does that means from today we'll only Work on JSP and not
exactly the problem is uh there's one issue with JSP the issue is whenever you create a JSP and when you when when you run it for the first time it will get converted into subet right and it will take some time to do that right so for the first time you're wasting some time so that means uh serlet Works faster than jsv because in s you run the s in JSP you have to convert this JSP into seret that's one problem second problem it is recommended That don't write your business logic inside JSP never never
do that never write the code the actual Java code inside JSP so if you want to process something always go for sulet so seret is something which will process your information right and then JSP we should be using only for the view now when I say view as a client whatever you get on the page or on the browser that's the view right when you go to google.com What you get a page right that's a view when you go to facebook.com what you get is the page that is view right so JSP is best when
you want to create a view but hold on we have HTML for that right why you use JSP when you can use HTM the thing is using HTML you can only create static pages and GSP generate page it means let's say we have a client here and then we have a jsv page here now this can be a static page or it can be a dynamic page if you want to create A static page you can simply go with HTML form and you will get it but the moment if you want a dynamic page you
have to create a JSP right now this JSP page will be having it will be having some static content and it will be having some Dynamic content example let's say if you go to Facebook right so on Facebook you can see we have we have some text there on the on the menu bar right and then we have some text on right hand side left hand side so there Are some text on that page which is static it doesn't matter who logs into Facebook you will get the same thing right so you can see Hello
I can see Hello same thing uh you can see settings I can see the same thing settings but the moment I click on my news feeds if I click on the home the feeds I'll be getting is different from your feeds that means that content is dynamic so we what we can do is we can create a layout we can create a layout using JSP Now that page will be only layout it will be only the design there will not be any data right so when you request for a page right you are expecting this
JSP in return so what you're expecting is the view okay see as a client we are only concerned about views we don't know what is happening behind the scene right when you go to Amazon you don't know what is happening behind the scene you simply get a page right in the same way as a Client so as a client what you expect from the server is the view but hold on we have to add data here right because this is just a design you want data as well from where you will get the data to
get the data first of all you have to send a request right so let's say this request is for get page okay if you have any any request you can say get student get page or maybe get products so you're requesting you're requesting for this this data which is get page you're Expecting this page now this request will go to something called as controller right now we have a controller let me just write this is a controller not C say this is a controller so whenever you send a request so in Embassy design pattern whenever
a client sends the request it will go to the controller now this controller will say okay I got a request here what is my job is to call this view so this controllers responsibility to Call this View and then this will this view will go to the go to the client but hold on there's something which is missing here the missing thing is controller is calling view but again view don't have a data right so what it will do is this controller will send the data as well so when a controller calls the view it
will also send the data with it example let's say you want so controller want to print something here example let's say this view is actually A student profile it's a student profile okay so we have a design but we don't have our data there right so this controller will send the the student information to this view right but in which format so that format let's say it's an object this object will have student info or student info and we will call this as a model so whenever a client sends the request it goes to the
controller controller will call view by Sending the model object so what is model object it's the object which will have the data now that model object will go to your view now we view will replace all the blank text with the actual data from the model and then what you will get is the is the page right now as a for a client it will look like a static page because we don't know what what happened behind the scene but that's a dynamic page the page is created I mean the page is filled with the
model data And that data is generated by your controller now in this Java technology or ser technology this controller is normally created with the help of servlets right so your controller is your suret your view is your JSP so normally we create views with the help of JSP and then your model are actually normal pojos now when I say pojo I know it's a very different term right so some people know about this some people don't know about this in fact some years back Even I was not knowing about pojo that's fine pojo simply stands
for plain old Java object so if you want to send a data we can send the data in the pojo format you can create an object of a student let's say if you can create a student class that student class can have role number marks name age email ID and you can send all this information in one object one student object is that simple right so yeah so when you want to implement MVC in Ser technology a client Will send a request to the servet and that Serv will be called as a controller that or
the controller will call the JSP but this JSP needs some data right and that will be done with the help of a model object and this model object will be a pojo which is plain old Java object and then we have a view which is your JSP so now question arise where to write the business logic if you want to do any processing it should be done on the Controller never write the business Logic on the view view technology which is JSP JSP should be only for the view viewing things this controller is for your
uh this is this is your processing thing and then this will be this model object will only hold the data I can never do the never perform any operation inside model object right it should it is only for holding data but then uh you know there there's also there's something called as n tier architecture Now what is that uh instead of doing all the processing here because let's say let's say let's say we have a database here okay we have a database and you want to fetch some data from the database so if you want
to generate this model you're fetching data from database how it will work so you have to connect your controller with the database right you have to write this you have to write those databas steps inside this controller which is your serate again That's not the good way of doing that never process anything I mean never actually write the uh what do you say the the uh operations or the per the U the functions or methods inside controller controller is only for accepting the request and then sending the response if if you want to perform any
operation here what you will be doing is you will be getting one more class here right so the controller will call that class now this class is Basically your service classes again this is a normal class this is a normal class it's just that since we are performing some operation we will do that in a service method right I will not do that so when you send a request to the controller controller will will ask hey service class it will be having a ob service class will have some methods and that service class will give
you the object right so service class inside this class you have to write the Datab databas steps makes sense right so in future even even if you change your database you don't have to change your controller right you just have to change your service but then when you work with database actually what happens is we have one more layer here and this is called as a dow layer so you call the controller controller will take the help of service and service will take the help of Da again this thing is not compulsory in MVC okay
MVC is simply This part we have a client we have a controller we have a model we have a view but then if you actually want to implement something we also use these two classes service class and DOW class now what is Dow it is Data access object so if you want to interact with database the best way is using Dow classes again I have a separate video on Dow just go just search for jdbc Dow telescope on YouTube you will get my video there okay so yeah that's how you that's how you Work with
MBC this is this is just a theory of it we'll talk about the Practical implementation in the next session okay yeah that's it uh anything else here let me know your doubts and there's one more question for you in fact I have this habit of asking questions in fact last week I started with a quiz online quiz uh so this is a question for you and the question is uh if you want to make a app okay if you want to make a Android app what are the Options you have so go to Google and
search for different options for making Android applications okay example let me give you one example you can you can do that with the help of Android Studio you can do that with the help of some ionic framework let me know more options so that's it in this video we'll talk about jstl so the prerequisite to learn jstl is suets and JSP so if you have basic knowledge of suret and JSP this video Will be helpful so what is gstl so gstl so let's WR here so jstl stands for JSP standard tag Library okay now why
we use this is because you know when you work with saet so we write HTML code inside Java code and that's not that helpful right and then in JSP uh in JSP we write we write Java code so inside HTML code we write Java code but still as a designer we are not that comfortable to work with Java codes right so if you can convert your Java code into HTML like code it will be much easier for designers to to write the code it will be easier for them to maintain the code it is easier
for them to change the code right so that's why we have this concept of gstl and if you see the output of gstl or the code of gstl it's more like HTML right so let's try to do that so to explain that how gstl works let me go with an example here and I will create one more I will create okay for that I will just simply Uh okay I will create a variable which is string I will say name equal to I will put my name naen and then I will send this name to
the jsv file so let me first call jsv file and how do you call it we say request request dispatcher so it is request dispatcher we say Rd equal to new not new but request do get request dispatcher and we have to mention the name of the JSP Right which is display. JSP right so what we are doing here is we are calling a JSP file from a from a solid file and we can say rd. forward and we can pass those two objects so what we are doing is we are creating a string here
and then we are calling this JSU page now in this JSU page we'll try to fetch that string that value which is name here okay first of all when you're sending it you have to make sure that you add that value somewhere so I can Use a request object or we can also use session object we'll say this is label again we can have any any label here and we can say I want to send name so I'm I'm using a label as label and then I'm sending this name so in this name I will
be having naen right now if I run this code of course it will call your JSC file right but we are not we are doing we are doing nothing here now how do you fetch that value so before fetching the value I will simply print hello World okay and let's let's run this code if I run this code on a server again I have a server here and I'm just calling that uh it will take some time okay yeah so you can see we have we are calling a sub which is demo subet and this
subet will call your JSP now since I'm running this for the first time it will take some time for your GSP to get converted into seret and then showing you the output so you got the output as hello world here but I don't Want Hello World I want to print I want to print navine here now how do we do that so for that we have to use some Java codes right in fact we can write Java code here and to write Java code we use the scriptlet tags and inside this we can write uh
we can fetch that value we can store that value in string we'll say string name equal to request dot get attribute get attribute and you can mention what type of what type of value you are sending so I'm sending a label Which you have to fetch again this will return you an object so let me just convert that into string so I will say two string right now once you got the string we can say out. print Len and we can say name so what we are doing here is we are fetching value from I
mean we are we we have this value nven which is going to the JSP and then we are fetching the value from the request object and then we are printing it again since we are using request object here To set the attribute we will be using request object to get the attribute right let's run this let me just run this once again and you can see we got the output as naen now why we are getting no is because in JSP we are fetching the value which is sent and then we are printing it this
is one way but if we can see as a designer you are comfortable with this lines of code but the moment you see a Java code you might not feel confident about it then Java Says okay chill we can have one more way here we can use uh we can use something called as expression language so expression language was introduced in JSP later and how it is used so we can use a dollar sign here with a curly brackets and inside your curly brackets you can simply mention the label okay whatever label you sending there
so if you are sending label here we have to mention that here okay so if you say dollar and cly backets label it will Fetch that value okay let me show you that let me just say once again and you can see we got two naen the first Navin we got because of this Java code the second Navin you got because of this line here okay now let me just remove this Java code here I don't want Java code so using this one line of code we can get our output which is nen now this
is expression language okay but expression Language was created just to just to I mean just to get used inside jstl so if you are using jstl then you can use this and how it was how it was introduced uh so what we can do is I mean we can we can have one more way of gstl way here so in order to use gstl we need to use some special tags example in gstl we have a Out Tag instead of using out object we can use out tag but the problem is Out Tag is not
available inside HTML right it's a custom tag and When you want to use custom tag we have to use a tag library right so we have to add a library for gstl now which Library we'll be using here so we'll use percentage add it we say tag LA and we need to use our gstl tag okay now question arise which uh how to use gstl Tag here that we'll see in the next video the point to remember here is we are using uh we can use e instead of using your Java tag to use to
print something on the server now again uh It's not compul to have that in request object itself we can also have that in session object we can also have that in page object because you know right in JSP we have different objects so you can have this label in any of the scope it may be in request scope it may be in session scope it may be in page scope or page context it can be anywhere so that's how we use e in JSP in the next video we'll see how to add the tag library
in gstl to use it In the last video what we have done is we have created a suet where you are sending a name and then we are printing that name on the JSP page using this label tag here and this is e right now what we'll do now is let's try to use jstl so we'll do we'll do the same thing we'll print something here but not using e but will be with the help of jstl again we can do that with eel but you know eel was introduced just to have an attribute in
jstl so let's see how to do That in Js so if you want to use this with jstl first of all let me just give a Break Tag here so if you want to use this with JSL we have to use a special tag called as out the problem is out is not a inbuilt tag inside HTML right so this is a custom tag and when you want to use a custom tag we have to import or we have to use that library now how to use that we'll say percentage at the rate and the
Way you can add Library inside your your Java JSP is using T tag lip then you can mention something called as prefix now prefix means whenever you want to use any any gstl tag we'll be using that prefix so let's say if you want to use out we can say out is a gstl tag by mentioning C at the start then you have to mention URI now what is URI URI is a location where you have your gstl okay I mean the the definition of your gstl libraries Now what URL URI we need here so
if I say control space you can see I'm getting lots of options here so we'll be using this one a code one okay so this is the path for jstl code Library so inside inside jstl we have lots of libraries we have jstl we have okay now we not getting options okay so you can see we have code we have a format we have functions we have SQL we have XML out of which we'll be using we'll be using Code function And SQL in this series so we start with code okay so we have to
mention C and then okay yeah so we have to mention URI and then that's it you can whenever you want to use out now we have to mention C and then you can mention a value you which you want to print so let's say I want to print only hello world nothing else if I want to print hello world we can say see out value hello world and save this now but you have to make sure that whenever you want to use J Jstl you use the library here you have to add this Library which
is jstl Library so jstl 1.2 you can download this from Google just go to Google and search for gstl and there will be some website which has this Library you have to also make sure that library is there in your web web app library or you can do that by adding one of the ways by adding that library inside lip folder of web INF okay now once you do that go back to Your browser and say enter you can see we got naven and we got hello world now why we getting nav because we are
using this label why we are getting hello world is because we are writing hello world let's I don't want to print hello world here I want to print NV itself so I will use dollar and the cly packets and we can say label okay so you can see using expression language you can mention we can mention that here so even that will Work but this was introduced in 1.2 JSP I mean JSP second version not the first version okay so in J in the second version of JSP we can use the but not so in
the earlier version we used to work with this okay so that's how you use out uh Out Tag Out Tag in gstl now what else we can do here this is just a basic let's go for some something else uh what what what else what other tags we have here so if I say C colon you can see we have list of tags control space you can see we have catch now catch will be used when you want to do exceptional handling right we can use for each so let's say if you have lots of
uh we have uh we have a list which you want to print we can use for each we can also use import to import some some other website in your project uh example if I say import and if I say tesco.com but not this way you have to mention the whole PA say Hp/ ww. th.com so you can fetch the entire source code of t.com in your code okay let me remove this one okay let's see how to fetch the code if I say enter now uh that's the okay so you can see I'm fetching
my entire website here okay I was not able to fetch everything but we got I got my website here in fact I can if I mention some specific thing if I say online. HTM okay onm and if IUN this code Now okay you can see we got our so that's that's that's my website registration details if you want to register for the course right if I say details it works uh it is work yeah so we have to provide the link as well but you can see we got our we got the page here we
have not got the CSS file on all the Stu but we got the page so we can use import what other other things we have we can say C colon uh we can use let's see what else we have so you can Set the so you can set the variables you can remove the variables let's say if you want to set the variables we can say set and we can use property name we can specify the scope till what point we'll be using this uh then we'll be using value what value you want to store
and the variable name okay so you can use set we can also say remove for removing the elements but in this example what what I passionate about is to use an object let's say any in this I'm not Creating object of string I want to create object of student so what I will do is I will create first of all we need to put that string so that we will not get any error let's create a class here and we'll name we'll say this class as student which belongs to com. telescope in this student class
let me have some variables here okay some properties I will have these properties as private in RO number okay U let let me just remove private Just to show you something if I remove the private and if I say string name just two variables okay and if I go to the subet and if I create object of that student if I say student oh we also need a Constructor using which you can assign the values just to make our work more easy we'll say Source uh Constructor we'll fetch both the variables here we click on
okay and in the in this subet now let's create object of student we Say student s equal to new student by passing those two values we'll say one and name I'll be using is naven I love my name I don't know why but n and instead of sending the label I don't want this string name here I'll be sending a student again you can use any name here any label and here I will be sending s so I'm sending s with a label student now in this display. JSP if you want to print label what
will happen is since you are printing the label if I go Back to my browser if I say enter okay it is printing nothing because it is not label it is student because we have removed label right so now we are using student okay why this why this error anyway ignore that if I say enter now you can see we got something and that's your hash code okay want to print the hash code I want to print the actual data so what you can do is you can say student dot uh you can specify the
property I Want to print name is it name uh what is that yes it is name so if you want to print name you can say st. name and if I run this code we got an error this error says property name not found hey hold on we have a name there right why it is not able see it is also specifying that name property is not inside student why it is so it's because whenever you want to use JSP and when you want to use any object you have to make sure that you're using
beans okay and how to make a bean to Make a bean you need Getters and Setters so that's the only way you can fetch the properties you have to say Source you have to say get and Setters for both the variables we'll say okay you can see we got all the get set here and if I save this if I go back to my browser if I say enter we got n there right it's awesome awesome right so we can we can send the data from your from your seret to the JSP in the object
format you can see I'm I'm sending an object here and then we Are fetching that object with the help of expression language now again you can use also use that g that jstl c Out Tag the main intention here is not to send one value let's say I want to I want to send list of students and I I don't want to print only one thing I want to print the entire object and to print the entire object you have to make sure that you implement a two string method in your code so let's implement
the two string method if you are familiar with Code Java in code Java we whenever you try to print the object it calls the two string method right so I I would I would like to print the two string method of that because if I Le this code now it will print both the stuff role number and name but let's say I don't want to print only one thing I want to print the entire stuff let's say I want to print uh I don't want I just don't want one object I want three objects or
four objects what you can do is you can Create a list of student here so I can say list of student and here we can say studs equal to now we can create new add list and you can do all the stuff but since I know whatever object we are working with how many values we need we can simply use arrays do aslist and inside this we can create multiple object I can say Student First object one and name is naen the second object let's say we have New student uh two comma R3 and then
we have new student this is three comma in double codes we'll say PL so what we are doing is we are creating a we are creating an add list with this all these values I don't want to send one object so instead of sending one stent object we can say students I want I'm sending lots of students and the object name is Studs okay so from this subet I'm not sending one thing I'm sending a list now how do you interpret that list here because if I say students and if I go back to my
browser if I say enter you can see we got all this data here but you know in one particular list I want to print that in iterative format one after the other for that we have to use a for Loop right we we normally use for Loop the normal for Loop in gstl it provides you a special Loop called as For each Loop so we can use for each Loop and inside this Forest Loop you can mention certain parameters the first one is items now this items is the least name so you have to use
dollar okay first of all we need to we need that double Cotes in this you have to mention the least name which is students right let just verify let's go to salet and it is students then you have to mention the variable name now what variable so one I Mean Which object will hold one object so I will say s so when this Loop runs it will so it will it will fetch one one object it will fetch first object then it will fetch second object then the third object and every time you just have
to print s right but that should be inside the curly brackets okay and after everything we'll print a break statement so that it will it will be on new line if I go and run this code now You can see everything we got on a new line right in fact if you want to print only one specific thing you can also do that we can say s do name it will only print name now not the entire object you can see we got the names here so that's how we use gstl so we can use
forish Tag which belongs to gstl and we can mention items we can mention the variable name right and then we can itate using the name there so that's how you use GST code tag in fact we have lots of tags Just try to explore on those tags okay uh in the next video what we'll be doing is we'll be working with the S the functions or yeah functions type of fun functions tags okay and in this video we'll talk about jstl SQL tags so any you want to use SQL tag we cannot use code we
have to use SQL okay now what is the use of SQL tags here so let's say you have a jsv page and you want to connect with database now what you will do so to connect with Database you need to write some tags using which you can connect with database normally what happens is you have to write Java code you have to follow those seven steps to to do that but JS still says hey don't worry we have some tags using which you can connect with it can connect with the database and you can fetch
the data let's see how to do that so what we'll do is first of all let's create a database in the in the in any database You use maybe MySQL Oracle I'm using MySQL here so if I go to mySQL I already have a database which is Mario inside that database I have a table called as gadgets okay and if I if I see what is what is there inside gadgets you can see we have GID G name and price so we have one 2 three we have iPhone as name then laptop and then 1+
3 we have this price here I want to fetch these elements or this values on the page so of course you have to connect with database right how Do you connect that first of all we have to use something called as a data source now data source will Define what is your database name I mean first of all which database you're working with second what is the database name then username and password and you can do that with the help of data source tag so we have to use data source tag in which you have
to first mention the database I mean the variable which will'll be using to refer that we'll say DB next you have to Mention the driver name because every dbms will have a different driver and for me the driver is com. MySQL do jdbc do driver okay and then you have to mention the URL I will say URL is jdbc colon myql again for for different dbms we have different URL I'm just assuming that you know how to connect database with Java and database these are the same steps then I will use my port number which
is 3307 For my machine Mario the database name and then you have to mention the username which is user equal to root and then password which is the next one in fact when you say you don't have to type this you simply say control space it will give you thing the the amazing thing about my machine is I don't have a my password so I don't don't have to specify that if you're doing this for the first time if you have a my password set it that now What next now once you goone to database
we have to use the query so you have to say SQL colon query okay in this query you have to mention uh your SQL query which query you want to execute now once your query get executed it will store the the data somewhere and that data is your Val so we'll say RS so will store the data inside RS now if you have seen my data if if you have seen my video on jdbc we normally use object of result set the same thing is here which is RS Is result set here then we have
to use data source which data source you are working with with and we have to connect with this DB so we'll say uh we'll use expression language here and we'll say DB okay with double codes now once you have done that let's mention okay what else we have to mention here then you have to mention the query so we'll finish this here we'll say end tag and inside this you can mention the query Now so here you can mention select star from gadgets okay now once you specify query so what what will happen is when
you execute this code now it will try to connect with your database now once it is connected then you will you will get your data source by DB and then you will you will execute this query and the results will be will be stored inside RS now question is how to print all these values now to print the values we Have we can use we can we can specify individual values right we can say C coolon out and then we can specify value right and we can we can fet the value from result set and
we can print but let's say I want to print all the values how to do that that will see in the next video but you have to make sure that if you want to execute this thing you need a MySQL driver okay so you have to add your MySQL driver here so again you can connect you can download that from Google or somewhere so you have to make sure that you are adding a Mas connector here so what I will do is before starting with the next video I will be adding the Mas connector here
just go to Google and search for Mas connector for of M connector J so let me just write it here this Mas connector slj download this charart file and save it here in the LI folder now once you do that you can use the maso connector and yeah so in the next Video we'll see how to how to print that in this video what we'll do is we we'll try to complete the last example the example which we are trying to complete so we have connected with the database using data source and then we have
also applied the query here which is Select St from gadgets let's try to FES the data now you have to make sure that when you fresh the data we we use we use this Library which is M connector can you see that I have added that Library here let me expand this so I've added that Library here which is maso connector okay and it should be there inside web app library now once you have done that uh we need to also take one more tag library for for code because we'll be using a for each
Loop here so we'll say tag lip prefix we'll say Okay so this should be SQL right because you're working with SQL and this would be C okay and then we specify the URI equal To in this we have to specify the code tag so this is code and this is SQL and let's use a enhanced F to print all these elements so we'll say contr c C I mean we'll say C which is a code tag we'll say C colon SQL or not SQL we'll use for H and here we'll specify from where you want
to fet that for that you have to say items now this items will be having list of elements right but hold on from where you'll be getting this list of Elements so you'll be getting this list of elements from RS because R is a result set right you have to fetch one more element you have to say RS do rows so this rows will give you the list of elements and then you can store it in you can store one value somewhere you can say you can store that value in let's say a row so
one row will have one row right and now we can print that row we can say colon out uh we'll say value equal to And we can fet we can print the value here we can say I want to print row of uh or maybe not row let's let's use something better we'll say Gadget which will look better right and we'll say here Gadget dot uh we can print what we can print GID then we can also after that we can print okay is it working something is missing here 20 this Stack okay and then
we can also print the we can with a with a colon in between we can print the name name of the gadget so we can print the name here and then with a colon in between we can print the price of it so we'll print the price okay so it will print everything now let's try let's let's see if it is working or not let's go to the browser okay that's weird and if I say enter can you see that we got the values Here but hold on we are not getting the names because it
is not name it is G name and if I say enter you got all the records here it's just that we have not added a break break before so we'll say break here and we'll say enter you can see we got all the records from our database right so that's how we can use SQL data source to Fresh that value okay so this is how we can use SQL data source to fetch the value from Database and you can do that with the help of GST and see how powerful it is now if you look
if you if any Java programmer or any HTML designer can understand this we don't we don't have any Java code there right so the JS is that powerful so that's it from this video we have talked about SQL tags or SQL yeah SQL tags in jstl and in this video we'll talk about jstl function tags now we have seen the code tags right so we have this C where We have we are using code tags but jstl also provides you some function as well example if you want to find this length of the string or
maybe you want to check if the current if the if a string I mean if a word is there in this particular string uh if maybe you want to uh check if the particular string is is matching with the end of the string and all the stuff you know if you want to if you want to do some if you want to perform some operations we can use functions of Gstl so in order to use gstl function we have to use uh again the tag lab but this time we'll say Okay prefix we'll set FN
and we'll go for URI as HTTP colon SL SL let's ask it for functions so this is functions now what are what are the functions available so we'll go for one by one uh the first one we'll use is let's say maybe length which is easier so let's let's try to find the length of the string so I will take a string now Uh okay how to assign a string so let's create a string we'll say set V we'll name the string name as St Str and we'll set a value and then the value would
be naven Ready's or naven ready is a Java trainer okay so we have this string here and I want to I want to find the length of it so I will print length colon I want to print the length here and how do you do that we'll say uh we we'll use this uh percentage I mean The dollar sign and then we have to Prov angular brackets in this you have to mention St strr but if you mention s it will print the string right let's try it out let's run this code let's see how
this goes uh okay is printing the string I don't want the string I want the length so what we'll do is we'll use a function here we'll say function colon and the function name is length and in this length if you pass St strr it will print the length of it if I say enter Now you can see we got length which is 29 I don't know it is 11 uh 13 14 15 maybe it will be 29 29 if you can count it it is 29 right so that's how we use length function that's
one of the function we can use what other function we have uh let's say I want to what else we can do we can use we can use split here I want to split this string based on Space here so whenever you have space we have to we need to break that I want to split it so what we can do is let's Not print this length now see when you split it will create an arror right because if you split instead of having one variable you'll be having an array which will have naven ready
is a Java trainer so we'll be having six words now to have to store to store that six words again we can use a for each Loop here to print everything and we'll set okay what we'll do here is we'll first of all we specify the items now this items would be uh I Cannot say St Str because if I say St Str it will be one string right I want to break the string now how do you break it we'll use a function okay so we we'll put that into a function but that function
name would be so we say function uh the function name is split okay so you have to use split and then you have to pass this St inside a split but hold on on which basis you want to split so if I split with on the basis of space you can see If I'm specifying comma a space in single quote it will split the elements based on their space right and then we can store the element somewhere we can let's say I want to store the element in s and now I can print the element
uh before printing I want a break now how do you print it it's very simple we can say a dollar then a curly brackets and S and now if I go back there if I say enter you can see we got all these words Here it's as simple as that right so we can we can have a string which is n r is a Java trainer and we can split that with the help of double code I mean with the help of space and then we can print all the elements that is that is that
is split method what else we can do we can split based on the a later so that we have a here then we have a here then we have a and a we have lots of a right let's try to do that let's let's see if it is Working with a you can see we got with a as well okay that's how you use split method what else we have uh we can also use another method like a fun a functions like index off now index off will give you the index of that particular string
let's say I want to find this index number of is now how do we do that we'll say index colon uh we we'll use a function here so use to use function again we have to use This thing then we have to use FN colon the method name is index off okay and you have to pass two things first the string which is St strr the second one is the the string which you want to pass I want to pass let's say is so it will print the index of is so it should be 11
12 13 maybe or 12 12 it should be 12 hope so it will be 12 yeah you can see we got 12 so that is the index number of 12 that is the index number of is so that is your index of uh we can also use We can also use contains now can we use contains we'll say uh is there we can print is there okay it will only print is there and because if I want to check if this string contains Java so I can do that we can say a dollar then we
can use a function here function colon contains okay and then we can use a function by passing a string not a zero but a string so we can pass a string the First parameter and the second parameter would be your okay watch uh the string itself let say this is do we have Java there let's try uh yes Java is there so is there true it is there but if I say does it have JSP let's try uh you can see false so you can use contains in fact normally we use contains and if condition
we can also do that we can say we can also do if here we'll say C colon if uh in in Your code Tex we do have if and we can apply test here because when you say test when you want to check you apply test we say equal to and we can put everything in double quotes here which will check and if okay we have to also we have to also end if so this is a if block here and inside we can print uh we can print JSP is there right if this matches
will print is there otherwise it will not print okay we got an error there uh the Error is it should be in single code now because we are using a double cotee there my bad okay so you can see we are printing nothing because it is not there but if I print if I check for Java and if I print Java is there it will say yes Java is there right so so that's how we can use contains function we have more some more functions we can check with ends withth if this function ends with
or if this string ends with Java I mean with Trainer then we'll print you are right buddy okay it should ends with it should end with uh trainer uh okay it's ends withth not ends with ends with and if I say enter uh you're right buddy but if if if if if you say Java trainer is a Java trainer in Mumbai and if I on this now so you can say last word is Mumbai right so it is not matching so you can also check with ends Withth okay what others we can have so yeah
so the so we have all these functions what you can do is you can explore more function you can simply say dollar then cly brackets FN colon and if you say you can see we have lots of methods here we have join uh we have replace with you know try it out try all this all this combinations you can also use two uppercase two lower case the same way we are doing this example we can say St Str and it will it will print In two uppercase if I run this code uh two lower case
right so you can see everything is printed in lower case here if I use uppercase it will print everything in uppercase if I say enter you can say we got no of javat in Mumbai that's do awesome right so the jstl has so much of power so that's about function uh jstl functions in this video we'll talk about filters in serlet now this is the most ignored Topic in seret technology so we We learn about we talk about seret request we talk about response we talk about the life cycle now this is one of the
important stuff which we use in suet I mean it's not important to to write this in your server application but you can use it now why we need filters so let's say uh we have a client here and and then we have a server in fact I should you know for all my videos I should have this static things because in all my Serv videos I'm going for a Client on a server right that's how it works so let's say we have a we have a client on a server and on This Server we have
certain servlets let's say we have a seret we have a serlet and we have a seret now what you want is whenever a client sends the request of course when you send a request request goes to web XML file and then that's how you figure it out which Serv to call right so let's say from this client you're requesting for this particular Serv and after some Time you're requesting for this particular subet and after some time you're requesting this particular subet so we have subet a b and c now whenever you request for any of
the S let's say if you want to maintain some log files you want to maintain the security Now for A and B you want to maintain the log file and for B and C you want to maintain the transaction and for a b and c you want to maintain the security so with this feature of a b and see you Want some extra features like uh maining the log files uh then maining the transaction and maining the security so what you can do is you can write those code so if let's say if you want
to maintain the log file you have to change your a soft and you have to add the log code there right you have to use log 4J and you have to add the lines in AET the same thing you have to do in B Serv as well because here also we want to maintain the log file now let's say you Want to use transaction so you want to do transaction management those things also you have to write in b and c and then you have to write certain codes for security if you are allowed to
so let's say if a client goes to serlet you have to verify if that Ser if that client is allowed to use that serlet so for that you have to check something right again that check will be done in a b and c right so the point is instead of of writing those thing inside your ABC can You just take it out now since A and B has a common feature of log B and C has a common feature of uh a transaction and a b and c has a common feature of security can we
just take these models out and the advantage would be if you can take those common things common thing outside and those common things will will make in such a way that you can plug it anywhere when I say it can be plugged anywhere let's say in future if you have D and if you want to if you Want to achieve transaction here you just have to plug that request and your seret will not even know that you're doing in the back end so what we can do is we can add filters in between so let's
say we have the first filter here this is your first filter which is filter F1 we got a second filter which is F2 and then we'll be getting a third filter which is F3 now you might have guessed it the first filter here is for log so we can write a filters which will So this filter F1 will work for log files this filter F2 will work for transaction and this filter the F3 which will work for security right so using subet filters we can we can create some filters which will be intercepting your request
so whenever you send a request if you're sending a request for a it will go through F1 first so if F1 says okay I got I have maintained the log then it will go to F3 it will check for the Security and then only your a will be called in fact if you let's say if you are sending a request and F1 says everything is good I have maintained the log F3 says no this guy is not allowed to use Ser a F3 can return the request right then a will not be will not be
called right so you are secure now uh let's say for the second request if you're sending a request it will first go for log maintenance then it will go for transaction then it will go for Security right and any of this filter can return the response I mean they can send the response back so they will act like a sub but those those are not actually sub those are filters right so if you want to have something if you have certain certain things which are common like log maintenance transaction security or we have some other
thing as well you can write those things inside filters so the advantage of using filters they are pluggable you can so if You have a new subet in if you have a new subet you can add a filter in between in fact let's say in future in future if you have one more one more filter let's say that is F4 which is let's say for any of the XY Z purpose what you can do is you can add F4 here because those are pluggable right your Ser will not even know that you're using filters and
is that awesome in fact these filters they don't know which filter you're working with example if You're working with F1 F3 don't even know that request is coming from F1 and that that's awesome right in fact your F2 don't even know that after F1 you're getting the response so this filters are also individual so they don't know how I mean how it is working I mean how other filter works right but then your tom cat says if you have multiple filters and if you want to if you want to check for if you want to
go for multiple filters what we have to do is we have to chain them So this is called as filter chaining okay so you have to filter chain I mean which we have to chain all these filters again F1 will not know that it is sending request to F3 it will be done by Tom cat but then where you have to configure this uh so when you write a filter F1 you have to mention once this check is done once this is done go for filter chain so whatever you have in the next thing it
will execute that one but then question arise uh Where to configure all this thing where to mention for which request we want to call which which filter that you have to configure in your favorite file which is called as web.xml file so web.xml file will handle uh so you have to configure in this file which filter you want to go for makes sense right but then question arise how can you create a filter so to create a filter we have to create a class which will extends Serv filter okay and then we have to use
three Methods now if you go if you remember your saet saet sessions we have talked about inate method we have destroy method we have do get a do Post in the same way for filters we have a method like uh init we have a method like do filter okay that's do filter and then we have destroy okay so we have these three methods now in it and Destroy is something which we normally don't use right we never use that thing in fact if you want to do Some initial configuration you can do that but let's
say we don't use that uh in this you have to pass two parameters request and response and that's how you can check right example if someone is sending a request to a saet how would you know if he is allowed or not so that data will be there inside request object but then let's say after executing this filter F4 if something goes wrong filter F4 can return a response right it can send a response with the help of Response object right so that means your filter can I mean they can work as a seret but
then that's not the main purpose of filters so filters are basically to intercept your request right so that's it that's about the theory of filters we have we we have a concept of ser filters in in suets and it it we can also do filter chaining you you can also configure individual filters if you want to configure filters we can use something called as filter Config so it is also available the way we have Serv config the the same way we have filter config for every particular subet we have subet config for every particular filter
we have a filter config right so it's it's that's it's damn simple again we'll see the plal implementation of this in the coming session so that's it that's for this video and in this video we will try to implement subet filters now if you have not seen the theory of this concept Please do that so in the description area you will find a link for salet filter Theory so let's get started with the example here so you can see we already have two pages here in fact one is JSP second is solet and in this
JSP we just have two field one is a ID a name and the moment you click on submit it will call it will send a request for add alien so basically let's say you have a alien and we alien has a name and an ID and the Moment you click on submit the request goes to my salet and this is where you will you want to perform some operation and you can see I'm using annotation based configuration here we have web salet request goes to alien add alien and then this will get EX uted now
just to do something here what I will do is I will just try to print the name of the alien here so I would say first of all I want to fetch the name so I would say string a name equal to request. get Parameter and here I would say a name and it will be assigned to a name and then I will print welcome plus a name or maybe I also want uh you know the ID of it so I would say int a ID is equal to integer passent and here I would say
request. get parameter and here would say a a ID now once we got this thing once we got a ID and a name I just want to print a name here it's very simple stuff right so before applying any filter here let's try to run this Application so I will go back to home and I would say run so you can see we got a page we got the output here I would enter some values I would say let's say one1 and here I would say enter naen the moment you click on submit request goes
to the servet and you can see it's printing welcome n everything is working perfectly but what if I make a mistake here let's say uh if I say one1 it should not be I mean if I enter minus uh 10 when you say ID it Should be positive number which starts with one but if I say submit you can see it is still accepting it now we can do this validation on subet as well but most of the time what you want is you want a separate filters who will Who will check all these values
not just about the initial values maybe you want to check for cookies you want to check for if the user is already logged in where you will do that and of course we can do that in suets but it is always Better to do that in filters because filters are something which is pluggable now some people have this debate you know instead of using filters we can use subet as well yes we can use suets the only thing is Filters provide you those concept of pluggable things you know you can plug filters anytime you want
and one filter is not dependent on another filter the way we have suets because in suet you have to mention which next s you want to call but in filters we don't Have that stuff right we simply say Hey you have done your job now call another s another filter and you don't have to mention which filter let's see how that works here so what I will do is I will do this filtering I will do I will do this checking in a filter now how you create a filter it's actually a very simple step
the way you create svets right click on your project and say new and search for filters you can see we already have a filter here click on that We can put that in the same package of serlet so serlet belongs to package com. teliscope let's put the filter in the same package again you can have your own package doesn't matter but let's have com. telescope and we'll have a class name as my you can have any name doesn't matter you can say first filter second filter I would say ID filter here as you can as
you can create multiple filters here we'll say next now for which request I want to do this so whenever Someone calls add alien this is where I want to call I want to initiate this filter click on okay and click on next nothing fancy here click on finish and you can see we already have a filter here everything is there but we don't need this stuff so let's remove that we don't even need this command let me clean this stuff a bit so what is this thing here so you can see we already have a
filter the name of the filter is ID filter as we have defined in the Creation of it now it implements an interface called as filter now filter is a inbuild interface in in Serv API and you can see okay I don't have a source code but doesn't matter you can see it belongs to uh it is a interface it belongs to Serv API in this filter interface we have destroy method do filter and in it so let's say before before working with this filter if you want to initialize the values you can do that here
and in the theory video we Have also talked about filter config right so every filter will have it its own filter config and then if you want to do something destroying you can do that here maybe you want to uh instantiate a new object you can do that in in it and once the filter has done it to work you can you can destroy the you can release the resources and Destroy but at this point we are not doing all the stuff right so the only method we are concerned here about is do filter Now
do filter is almost same as do get do Post you know we it accept the request and you can process values but it has a special feature which is filter chaining now as we know it's not just you can use only one filter we can have five filters right you can have 10 filters so by default it will C so when you run this application it will call the first filter so let's say for ad alien we have five filters so it will call the first filter once it has done Its own work it will
call the second filter the second filter will call third filter but how will you mention that this filter has to call itself or the second filter that's where we have a concept of filter chain so all these filters will be in a chain and every time you have done with your work you simply say chain. do filter which means it will call the next filter and you can do your work here okay now here's the thing what if this is the last filter And last filter says chain. do filter now since we don't have next
filter it will call a sulet okay that's a sequence it will complete all the filters first and then it will call a suret but as of now we only have one Ser one filter let's work with that so here I want to check first of all I just want to print something just to see if it is working uh where I will print it I will print on Console I would say in filter just to verify you know it's always to go step By step way I just need to verify if this filter is actually
getting called and to do that I will I will run this application again so you can see same steps I just need to click on submit and you can see it is saying welcome Navin because we are not doing any filtering as of now but it is calling in filter that means the sequence is it is going from your page to filter then Ser it okay that's great but now uh in fact how would I know this you can just if you if You want to try you can just print something in this uh do
get it will be getting printed off in filter but now I want to do filtering right I want to check what are the values which are coming now as you know whenever you want to work with request object we need a HTTP server request object right and here we have a subet request object but we need HTTP server request object so we can do that we can say HTTP subet request I can say req is equal to I can Type cast it with HTTP sub request and request so what we are doing is we are
we are basically typ casting the object so we are converting this object from server request to http server request and now once we have this object we can actually fetch the value and I can say in fact I I will just copy paste the value because I don't want to type it again I will fetch this value here and paste now once we got Aid I can actually verify it if I say if Aid is less than One because I don't want this value to be less than one in case it it's less than one
I will not do chain chain. do filter because this thing should be done only when it is greater than one in fact I what I will do is I will say greater than one this will make more sense right so do chaining only when it is a a ID is greater than one so it will go ahead only when it is a ID is greater than one it's that simple now will that work let's verify I will just run the Application here and okay I just refresh this application and this time I would say minus
10 and here I would I will say naven and click on submit you can see the moment I click on submit it is doing something but it's not calling so you can see it is not printing anything here but at least it is not calling a salet how do I how do I know it is not calling a solet let's print something here so that we can verify I would say uh in salet because this will verify that it Is calling a solet or not and I'll go back here run this application again the same
values which is minus 10 now you if I click on submit you can see it is not it's not printing in seret that mean Ser is not getting called for sure okay but I want to print something on a page right because if Serv is not getting call it should print something on a page now in that case I would say else and I will use this response object oh first of all I need out object right so let's Do that I'll say print writer out is equal to response. get writer once we have this
object I will say out. print Len because filter has the power to send a response back to the client I would say in valid input yeah so we can do that in filter as well so let's rerun the application again and and click on submit and you can see it says invalid input it is not calling a salet and it is printing invalid input so that's the power of filter so even before calling a Saet we can do some filtering okay so let's say if I'm putting a right value so let's say if I go
back here I I will enter right value I would say 10 but the name which I'm printing is only of one character which is n and if you click on submit it is still working but I don't want anyone to have only name n right it should be at least more than three uh LS now in that case we can create another filter so we can create another filter and we can do the same stuff so that Will be your assignment so what your assignment is is create one more filter and almost the same code
and make it work the only URL so make sure that you have the same URL and let me know if it is not working for you so that I can make another video I will paste a code for you so this is this is your assignment create another filter and if you have able to do that mention the comment section that you have done it and if you have not if it is difficult For you just let me know so that's it that's your solet Filter Works today we'll talk about how to do login for
seret and JSP applications you know when you build a website so there are lots of things you have to consider you have to consider about user requirements you have to consider about uh your requirements you have to consider about server space and the most important thing about this is a login application right or a login module so Let's say You're Building any e-commerce website if you're building any social networking website if you're building a website for your own college or for your own company you require this module which is login right now is it that
easy to build a login application for a website and the answer is yes it is very easy but if you consider some uh twixs if you if you consider some uh some topics in seret it it makes it bit difficult because uh if you are creating A login application for a normal software it's bit different than what what is when you do it for website because when you talk about websites we have web servers right and web server works with HTTP protocol which is stateless so let's say you are accessing a page and after loging
you're accessing that page so you got the access now when you go for the second page you need the same login access right so again you have to login what we want we want to Maintain the state of the login and then for that we have to use some session management okay now so in this video we'll see how to manage the login throughout throughout three to four pages or maybe the whole website using sessions okay now so let's imagine you're working with application where you have three pages so what is website basically when you
talk about websites it's a collection of web pages so let's say you want to build a big website we Require web pages right so let's imagine the first page you have here is login page so we have a login page here we also have a page which is let's say about us okay then we have a page which is welcome page or we can say homepage and the last page we have we have let's say one more we have videos page right so when you have all these pages in your website if you combine all
these Pages you will get a website right Now from this web from this web pages there are some web pages which requires which requires a login access other other Pages like about us don't needs a login or don't need a login page or login access so anyone can go to about us without login but if you want to go to welcome page you require a login if you want to go to videos page you require login but not for login page which is itself a login page right so once you give your credentials inside Login
page so let's say you will it will ask you for username password so let's say we have un name and password here now once you enter this detail of username and password now it will it will verify your username password right it it will verify your credentials now if you are a uh what do you say a a normal or Au authenticated user then it will provide you a login access otherwise it will reject you right but let's say you got The login now the page you can see is welcome page right and in welcome
page it will show you some data about you uh so like welcome naen and it will show you all the last access you have done then from that page you'll be having one more link let's say for videos page and to access videos we require again login example when you go to ud.com if you go to my own website tesco.com to access some pages we have to log in right and uh so in my website I don't have the Login form videos that's good right but there are web pages where you have to log in
first so that you can access the particular web page now from this page videos If you are going to this page now if you say you want to access this so you require a login right so from login you're going to welcome and from welcome to you're going to videos sounds perfect right but let's say if a user is not logined okay suddenly his he types a URL which is let's say uh for me let's say My website name is ABC and if I say slash if I say login.jsp so what which page I will
get is this page right which login page now after login automatically it will go to welcome page and from welcome page if I click on a button I will go to videos so the the URL for videos is this it is videos. JSP now anyone can type abc.com videos. JSP and they will get the access to videos page right and where where to ask for login there if They know the URL they can directly access that right and that's what we want to stop them that you should not you're not allowed to access this page
without login and that we can check with the help of session okay so there are two ways of doing this the first way is session the second way is cookies now it's a big debate which one is good either we can use cookies or we can use sessions now Cookies are much easier to work with because what happens uh your server will give you the cookie which you will you have to store inside on the client machine right so your client is responsible to save that cookie now since client has the cookie client has the
access to cookies so they can modify it they can hack the server or they can play with your server so inste of that go for session where you save everything on the server side which is safe for the Server right of course and why a client will need your data or server data right so of course session is much better than cookies if if you talk about login if you're talking about maybe e-commerce e-commerce module then cookies will be better but time B for login session management is important now how to achieve session so if
we talk about uh suets so we have a we have something called as sttp it's HTTP session in solet so whenever you whenever you log In so once you verify your credentials of course right before so once you enter this detail when when you will click on this button but which is login button so once you click on this button it will call a serlet right so let's say let me Define a serlet here so let me remove this and we'll be having a subet so let's say we have uh a subet we'll name this
as verify. HT verify do Java that's your subet right so once you click on this button you Have to call this subet and inside this subet you will verify this two details right of course you know uh how to fetch values from the client side using request.get parameter then you will apply if else by checking if the values are correct now once the values are correct you will send a request to welcome again you can use request dispatcher or you can use uh one more thing uh redirect so let's say if I'm using redirect I
got the page which Is welcome right so from this from this seret code I'm sending request to welcome but before doing that we need to do one thing we need to put data into this session so what is session so let's create object of HTTP session so I got the object here which is called as session right so that that's my object how to create this object we have HTTP session session equal to request. get session again we'll see that when we start with the Practical so this is just The theory theory of this after
this we'll be talking about the Practical implementation so in that we'll see how to achieve this okay so once we got the object of session now we have to set the Valu so you have to say set attribute so that's the method we have to use so that's attribute attribute and then we have to mention two things the label and the data so label I will say this is UN name comma the data is let's say Whatever data you got from username so if username is naen so it will be naven here if username is
admin so you have to send admin here so let's say I have stored that data in un itself so the label is also uname and your data is also uname now once you sent this data to to the welcome now what you have to do is on this page which is JSP page so so this will be a JSP page inside this so let me remove this now so once we got Into this page which is welcome. JSP let me write that page here now so in this you have to check for this session again
so before executing any statement from this JSP we have to first verify now how to verify so we have to apply if here and we can say if session dot get attribute because of course right once you set it we have to say get attribute if this un name is equal to equal to null now when you will get this null is so let's say If user is not logined directly he is trying to access welcome. JSP so it is abc.com welcome. JSP that means this page was not visited user has not provided the credentials
directly is trying to access welcome. JSP in this scenario this uname will give you empty results that means session. attribute will be null and if this is null that means user has not login what you can do is you can again call login page how to do that we can simply mention here Response which is response do send redirect and we can mention which page to call we say login dot JSP okay so once you call welcome. JSP without logging it it will check if your name is null then it will directly call login.jsp that
means if anyone's try to access a web page which is a secure web page without login it will he will be redirected to login page if this is not The case then you can continue with the example and you can show welcome. JSP right now let's say uh in this page you have a button for video so we are providing a link let's say ahre so we have a HF for videos now once you click on this button once you click on this uh what do you say uh anchor tag once you click on this
videos here it will take you to videos page again you will you can see all the Videos on that page now you'll be having a log out button maybe on welcome also you'll be having log out and here also we have log out now what to do once you enter log out or once you click on logout button so let's say we have a button here in in this web page itself or let's say in videos we have log out button so we have a log out button now once you click on this button what
should happen it should also call a subet okay so let's create that subet Here so we'll say this is log out. Java now in this what you have to do so when you login so when you when you log in for the first time what we have done is we have seted the set the attribute for session right which is UN name and guess what you have to do in this in logging when you set the data in log out you have to remove the data right so you have to use something called as session
do we have to say remove attribute and then You have to mention which is UN name simple right now once you once you remove this and if you go back to welcome page the thing is in welcome page you will get this as null and it will redirect you to login.jsp so you have to do this for every secure page okay let's say in your website you have maybe 15 pages and after 15 pages five pages are secure pages and 10 pages are normal pages so for those five pages you have to write this thing
on every page On every JSP page you have to verify if this is null then log uh redirect it to the login page and for every log out button it will call this thing which is session. remove attribute okay so that's how you provide the what you say the login feature but there's one more thing here so let's say uh whenever you access a web page so what happens is on that web page we have your browser has the feature of cash in Pages now what will happen let's say you You're you're on the welcome
page you click you click on a log on you click on log log out button now once you click on log out it will take you to login page right you have to write here we have to say this is after removing the attribute we have to say redirect me to login page right so once you write this statement here it will take you to login page now what will happen when you click on back button now once you click on back button it will again take you to Welcome page and you're hacked so let's
say if you're using a website and you you have entered your details there and then once you say log out you think you're safe now but yeah hackers comes to on take your machine they say back they got the access to your website right so that's not the case so what we to do is we have to remove the cache Pages or we have to stop that cache of the pages and we have to do that with the help of some meta data or meta tags Again how to do that but that we see in
the practical implementation so if you want to include login in your web page or in your website you have to follow all these things so that's a general overview how to use sessions instead of cookies and we'll see the implementation in Practical itself this video we'll see how to implement login module in subet and JSP so we have already seen the theory of this right so what we need we have a Login page here which is login.jsp we have welcome. JSP we have videos. JSP so if I go to login.jsp it should give me two
uh it will give it should give me two text field where I will enter my credentials username password and if when I click on the button if the username password is correct it should call a page which is welcome. JSP in which we'll welcome the user and we'll print welcome space username and will provide a button or a link for videos Page and once you click on the videos link it will take you to videos. JSP page and here we'll try to uh show you some I will will try to show some uh video something
like that so uh we have this three pages and what I want I want this welcome. JSP to be a secure page and this videos. jsp2 secure page which means if you are login then only you can access these Pages uh let me have one more page here I will name that page as let's say New and JSP I I will say this page as about us. JSP and in this page what I will display is I will say uh this is is no ready from telesco H telesco learnings okay that's what we want to
show in the about us page again the view will not be that good okay just we are just focusing on the login part I'm not concerned about the view how it looks so we we'll try to keep it simple so we have this about us page which prints the about us data so what I want this page should be open page it should not have any restriction to open so if I go to login page so login page and about page they are uh unsecure pages and this welcome and videos they are secure pages so
if I play this if I run this login.jsp now if I run this okay uh it will open in my browser I hope it will be in Firefox okay that's Firefox here so that's my login page okay so I caness I can access login page I can access uh welcome page welcome. JSP so I don't want my user to access this welcome. JSP page okay so this should be accessible only when the user is logined so what we'll do is we'll try to provide a login button here or we'll provide a form so we'll say
this is form we'll form action and we'll Mention let's mention so we'll say double codes and we'll mention here form action we'll call A A serlet here which is let's say this the serlet name is login okay and we'll keep the method as uh method as get now we require a username so we'll say enter user name and we'll say this is input type equal to text and we'll say this name is equal to you name okay and we'll say BR the next thing we require is enter Password okay and we'll say this is input
type equal to this is again this will this will set as a password so that you will see that uh star symbol there and we'll name this as pass okay and then we'll say BR now next we require a button so we said input type equal to text or not text this time it will be submit and we'll say value what it should show is a login button right so now once you type all these codes once You click on this login button it will call a login dot or login. Java file which is login.
class file which is serlet right now let me create a serlet here so we'll say not here we Java file so right click new we let's create a serlet and it belongs to a package which is com. login I have already created that created that package here com. login and the class name let's say this is login. Java once you click on finish it will give you the full source code For subet right so we're not consum about this thing let's make it simple we don't want do Post we just want do get here okay
not even comments okay so we just want do get and inside do get what we want so let okay so now we need to F first fetch those details so we say string un name equal to and we'll say this is request this is request. get parameter and inside this get parameter you have to mention that you want data from uname now this uname Here which is the request.get parameter you have to fetch data from this field that's why un name and next we have to fetch password so we'll say string pass pass equal to
request. get parameter and we'll say this is again pass okay now once we got these two details now let's call let's verify now how to verify we'll say if again you can fetch this or you can verify this data with the help of database so you can Create a dow layer you can request your database to give you the username password and then you can verify but time let's make it static we'll say if my un name is equals if it is equals to telesco and and the password need to be equals with let's say
learnings if these two are same so if my username is teliscope and password is learning in that case we can call welcome. JSP now how to call that so before calling we Need to also very we need to also add the data into session right because how will you verify by when you're calling welcome. JSP how will you verify that the user has logined oh let's not do that first initially let's keep it simple let's say directly we'll call uh welcome. JSP we'll simply say this is response so we say response do send redirect we'll
say send redirect and we have to mention here the page name which Is welcome. JSP that's it right it's that simple and when I run this code it should call welcome. JS SP let's try so if I on this code now okay it's running okay so here it will ask you for two things let's enter it is telescope and we'll say this is H I guess I have entered a wrong details so learnings and if I say oh welcome you can see we got welcome. JSP but the thing is this page Doesn't have anything we
say welcome that's it okay now let's go back to this enter we got welcome right right now if let me enter once again I will say learnings enter we got welcome. JSP right but what if I enter a wrong password and if I say login you can see we got a login page or the login subet and it's not it's not showing anything so what we want if the username and password is wrong or if any of the one is wrong we'll call the same page again We'll call login page again so we say response.
send redirect uh send redirect and we'll mention this is login.jsp so it will again call the same page so let me show you this and if I say enter so I got login page let me enter any any data here if I say login uh it's not yet deployed I guess okay let me just refresh again okay so let me try something else if I click on login you can see we got the same same page again right so if Your username password is right it will call welcome. JSP if it is wrong it will
call login.jsp now what will happen if I if I if I'm not logging it okay let me just change my browser let me use Opera and inside op if you paste if I paste this link which welcome. JSP you can see I've not login right and let me go back to this and say welcome. JSP and we got welcome it's because we we have not mentioned that uh We need to check for the login now how to verify that user is login anyway you you're able to access the welcome page right so what we can
do whenever a user login we'll will set the data into a session now how to use session we can see HTTP session we'll say session equal to now if you don't know how to use session just go back to the video where I have talked about how to use sessions and then we'll sayp session we'll say this is Request do get session okay so once we got the session now we'll set the data into session let's import the package okay now it is session dot we have to set the data into session so we have
to say set attribute we'll say this is we'll set the label which is let's say username as a label and the data we need to send is the UN name which he has entered while login so we say this is you Name sounds good so we have set we have uh we have set the data into this session now in the welcome page we need to check so before executing anything we we need to check that was the data he has entered so how to how to check that uh so we'll check if if this
session. get attribute since we are when you assign data you use set attribute to get the data we use get attribute if the attribute with username is equal to equal to null because if user directly Trying to access this page without login this will be null right if that is the case we'll again call response. send redirect let's call login.jsp okay if not then it will print welcome let's try let's go back to Opera so is it Opa this is this is Opa now I'm trying to access welcome. JSP without doing login and if I
say enter you can see we got login page awesome right this is this is possible with the help of this thing Here which is session now let's say if I'm loging if I'm trying to log in here we'll say this is Tesco and the password is learnings and if I say okay we got log we got welcome page right but the thing is I want to show the username also so we can use jstl here which is expression language and we can directly write dollar and we can write here uh what username so this is
jstl so Whatever you have so let's say in session we have this one wa session so inside session we have username right so in this welcome page it will fetch the username from the session so that's how you use jstl which is Java standard tag library or expression language to be specific now in this welcome page what we need we require a label we require uh what what we require we require a a link for videos. JSP so what we can do is we can provide a anchored tag we'll say AHF Equal to we'll say
videos. JSP we can mention a anchor tag close and we'll mention this is videos we say videos here and once I click on that link videos here it will go it will take me to video. JSP right let me just enter this and you can see we got welcome telescope and it says videos here if I click on videos it will take me to videos page and how to include videos so can I include my video there let me try I've never tried it Let's me let me go to YouTube and let me search for
my let me search nin let me search Java tutorials yeah that's me uh let me fetch this video okay copy the link okay and let me paste it here I know will it work will it work I don't know oh it will not not work of course right this is just a link right so let me just show you the link not the actual Video but a link right for that we have to use a video link so we got a link for my video you can you can actually use a video link on your
video tag on your website and you can embed this link there what we want is we let's imagine instead of this we are getting a video so uh if I try to access this page directly without login so let me okay we have we already log in right let me use it now this time we'll use Chrome and inside Chrome let me paste this link L and you can see we are able to access this page right without login so what we need to do we need to do the same thing just go to welcome
page copy this code and paste it here sounds good and now if I go back to my Chrome and if I say enter it takes me to login page awesome right so that's how you use login but there's one more thing let's say if I'm providing a log out button here so if I say log out so let me Provide a log out button so we'll say form action I will provide log out only on the welcome page so I will say form action uh it it should call the log out. sulet or log out.
Java file and we'll create a button here so we'll say this is input type equal to submit and in this the text we we can show is value will say this is log out simple and once you click on this log out button it will call a log out. JSP page so we'll say new uh log out not JSP log out. sulet so we'll say new sulet this will be com. login and it belongs to the name it will be log out right and let me remove all this extra stuff here let's only focus on
the get method and inside this will mention when whenever you click on logout button what should what should happen is it should remove your data from the session so we'll say this is session Request. getet session right and we can just import the package by pressing control shift o and we'll say this is session dot so while log in you set the session while log out you remove the session or remove the attribute which attribute we have to use we have to say this is user name and we need to do one more thing which
is session. invited so that it will remove all the data okay now let me I hope it will work now so let me run this and Again this will be opened in a Firefox that's my default browser for the eclipse okay so now let me enter let me login so a teliscope and this is learnings okay so we we we got the log we got the welcome page so I'm login right if I change my tab here if I go to videos. JSP I'm able to access this page is just because I'm login let me
log out okay that's it I've have logged out let me go to videos. JC page or different Tab and If I click on okay you get the login page awesome right so that's how you manage this session or you manage the login session in sulet and JSP we can even navigate from after once click on log out button you can navigate to the uh login page we can simply write this is what uh response. sendry direct right and then we can mention this is go back to login page so every time you log out it
will take you to login.js page awesome right so that's How you work with uh login module in sub JSP so let me know if you have any doubts any questions let me just go for a quick recap before that uh that's a login page when you click on this login button it will take you to login do login solet where you will verify your credentials if your Lo username is right it will take you to welcome. JSP otherwise it will take you to login.jsp but before going to welcome. JSP it will set the attribute as
a username when you Go to welcome. JSP you can print the same username but before that it will check are you logined if you how can you check that you are login it will check for the attribute if it is not if it is null then it will take you login.jsp otherwise it will show you the button or link for for videos when once you click on this button which is videos it will take you to this page and if you you can also click on log out button here once you click on log out
it will go to log Out subet and it will remove the attribute so that's it login using serate and JSP practical part two in fact I made the earlier video last year and luckily I got good number of views on that video and people were able to run this code and everything is working perfectly but then with one particular issue and that issue is when you click on log out and when you go back using back button you can see the P last page which is the a Secure page and this this should not happen
right example when you go to Facebook and when you click on log out now when you click on back on after going to Facebook it will not show you the homepage right it will go to it will go to login page automatically how that works because if you see our code in fact I have written the same code in Eclipse now as you can see we have the code ready here and what I will do is okay in fact if you have not seen my First video make sure that you watch this video because then
it will make more sense otherwise this will be what you say a question mark video for you so make sure you watch this video first and in fact you will find the link in description area and you can continue with this video then okay so if I run this code if I run this code once again if I say right click and run as run on server uh okay it's running on server now it will open my Firefox as a browser Because that's what I've set in my Eclipse so the moment you run this code
what's happening is is it running or it's taking some time so by the time it it opens let me open Firefox as well and you can see in the Firefox uh we have to navigate to login.jsp because that's where you have your things we'll say uh login.jsp in fact before going for login.jsp what I going to show is I will say welcome. JSP if I click on enter you Can see we got a login page because welcome. JSP and the video videos. JSP those are secure Pages if you want to go there first you have
to login so we will say theis and L and you can see this this is one more issue we are able to see the password that's not that that's not a good way and if I click on login you can see we got the welcome page now if I click on videos here you can see we got video link in fact I I want to change this as well we'll do that in Some time if I go back and if I click on log out now we are logged out of the system right and if
I click on the back button can you see that we are able to see the uh secure page now this is not a good thing right this is this is what you say a security flaw now how to avoid this so we'll do all these things in this video first of all let me change something let me change this password from l i mean instead of saying this password let's make it dot now how do I Do that I will go to my eclipse and I will say hey I don't want to see the password
so I will go to login.jsp and when you whatever you have text for a password let's men mention password because this will make sure that you cannot see the password okay we can verify that if I go if I refresh this page now and you can see whatever password you type now it will be in that dot format right now you cannot see the password if I click on if I press L you can see we got dot so we got our first thing the second thing I want to do is uh if you can
see in the form tag when you say form if you don't mention the method name it is by default get and in fact in Ser it as well I'm using do get now again this is not a good practice whenever you send data from client to server we have to make sure that we are using a post request because whenever you send data to the server you should use post whenever you Are fetching data from the server you should use get now in this scenario post makes more sense so first of all I have to
change this to from do get to do Post because I want to send a post request now and in login.jsp as well I will mention the method as post so that's one one more one more change which I had done here so we got a post request and we are using do Post there so we have done two changes the Third change what I want to do is in this video videos. JSP in instead of uh instead of seeing that link example if I if I logged in here and if I oh what's wrong okay
we have toart the server because I made some changes in the serlet so let me just do that let's say restart the server and if I go back back to my browser if I click on login now oh I forgot to enter the password if I click on login um it's taking some time Because the server is still getting restarted what's wrong I'm using a get request is it a get request here okay this is a post request that's perfectly fine in login. we got do Post what's wrong here let me just run this application
I don't know what is going wrong there okay is not able to fully restart the thing as well okay it says H method get is not Supported by this URL but why we are using a post request everything is perfectly fine there okay so this should be do post and we got do Post uh nothing nothing else nothing okay so now sometime it happens you know the you can what you can do is you can clean your project just to make sure that everything is perfect you know you you also get some cachy files in
the uh in your Eclipse as well okay let's so we have done with the cleaning and now Let's run this project once again if I run as run on server we start the server and let's verify okay server is getting started and I will go to Firefox just to verify everything is working let's restart refresh this page and and we have to call login.jsp it will ask you for two things let's insert insert teliscope and password L and if I click on login oh It's working perfectly now I guess there's some issue with the temporary
files okay if I click on videos here you can see we got the link I don't want a link here what I want is the video the way you can do that is uh okay what's what's going wrong there is we have written the videos link as it is this is not a good way uh instead of hting the link of the video you should be using some embedded text now from where you will get the embedded link so if you go Back to the video and inside every video will have in fact on YouTube
every video will have this option of share now if you click on share there's option of embed now in this embed if you just copy that embed link and paste it in your Eclipse your job is done now the moment you click on uh I mean the moment you pasted the JSP if you save let's go back to Firefox let's refresh this page now instead of getting uh link you can actually watch a video in fact you can Play this video uh and it will get play and as you can see it is getting played
now okay let's go back so we have done all those changes now the most important change what we want is okay let me do that once again the most important change is if I click on log out if I click on back this should not come now first of all before solving that issue first understand why the issue is coming the issue is there because you know we have this awesome feature which is Called as HT HTTP protocol now HTTP protocol provides you lots of features example session is provided by HTTP uh we got uh
cookies and the most important part is cash now cash is what makes your web browsing experience way faster right if you go to Facebook if you go to YouTube you know what I'm talking about you know you can access all those pages as quick as possible because we do main we we do maintain the cash now same thing is happening here When I click on log out and when I click on when I go back this is the page which you are getting here is a catched page what I have to inform to my browser
hey browser please don't catch these Pages because these are secure pages I don't want this to be cached but how do you interact with your browser now whatever you want to mention to the browser you can do that with the help of headers now every browser read headers as well so how do you how do you send a header that I don't want to save this page not not just videos page but also welcome page let's do that in welcome page first now the way you can do the the way you can send the headers
to the client is using the response object so we can say response dot there is a method called set header so you can send lots of data to the client one of the method is set header now inside this you have to mention two things the first is the label so what you are sending a label is You are sending a label for cache control now your browser understand what Cas cach control is you can pass your instructions here now if you don't know what the instruction is it's very simple in double codes you have
to mention no cach again you have to BU hard this because this is browser specific or this is what you say uh this is browser is something which a browser needs this thing okay so we'll say no cach no Store and you have to mention must revalidate for every action I mean every time you call this page it should revalidate if it is logged in or not I mean whatever you want to whatever you want to check for the validation right so you just have to set the header as cach control no Cache no store
rev viate I will copy this code and let me just paste this in videos. JSP as well because I want it there as well so what I will do is once we have done all the Changes let's me restart the server okay and if I go to Firefox and and if I refresh this as well now if I insert L and if I click on login okay it's happening something is happening behind the scene okay you got you got your page if I click on videos here it's working perfectly if I go back and if
I click on log out now so logged out successfully if I click on back can you see that we are not getting welcome. JC page that means the the it was not cast And your browser is not able to give you that welcome. JC page so this is what we want right when you click on log out it should it should show login.jsp and it should not allow you to go back now this was one way the another way is you can disable your back button right but then this is not a good way because
if you disable your back button what will happen is uh it will give a user a bad experience because no one wants to disable their back button how how dare You to do that right so no one will use your website so this is one way I mean this is the best way uh which is response. set header now the thing is uh this works this perfectly works on HTTP uh 1.1 right this is the this is the this protocol version supports this type of text what if you have the older version of HTTP working
in that case you have to set one more thing because this will not work there so you have to say set header And you have to specify pragma and the value for that would be no cache if you are using so this thing if you are working on the older version which is HTTP 1.0 but then if you are using any proxy server then you have to say response. set header and you have to say expires because prox server will have your cache if you don't want to maintain The cache there just say expires and
you have to mention the time zero so for 0 minutes or 0 seconds you have to mention this this should be I mean that expir is 0 second the moment you access that page it will will be gone right for pro Proxes so 1.1 for HTT I mean for 1.1 the first line 1.0 second line and for Proxes the thir third line the same thing need to be done on all the pages which whichever Pages you have so in the videos. GSP also I will do the same Thing remove this okay done so that's how
you can stop going you can you can prevent from back button so yeah that's it from this video I have talked about the back button working um okay now what I want to do here so there are some some more changes uh the changes like you know if you talk about this login. J login Java here we are checking statically right we are verifying the static coded text now the thing is this will this doesn't look good because the Data should be coming from database so again we'll be doing that in one of the video
and then we'll also do JavaScript validation now I want to show you one more thing in this video which is very very important now in uh in one of the comment I saw is they were talking about this thing session. attribute username so I'm stared writing username here which means uh I mean according to the comment it says whenever a user logged in it will allow all other users now That doesn't make any sense because if you talk about session object this is browser specific which means the moment you change your browser a new browser
will have a new session object when you are verifying here you are verifying for one particular browser right let me prove my point so if I go back to Firefox you can see if I log to the system now I'm logged in if I go to if I type here manually if I type videos. GSP I will be able to do that because I'm Already logged in but if I go to a different browser example if I go to Safari here and if I enter the same text if I say enter you can see it
is not opening videos. JSP is opening a login page because this is a new browser a new session and of course if you have a new session it will uh it will it will send you to login page again right so yeah that's it from this video in the last video we have talked About how do you provide this login access right so we can enter username and password and you will get authenticated but then you know uh we are verifying so if I go to my login. Java you can see we are verifying using
the static values we have a theis score and L as a usern password now this is not how we write code right we always make sure that the data is there is coming from database but question arise how do we do that here we have not done Any database code yet right so the best thing would be you have to create a database first and let's populate our database so that we can verify so step one let's create a database so again you can use any database which you like you can go for uh MySQL
you can go for po you can go for for Oracle which is your favorite uh I will go for MyQ here because this is my favorite if I open MyQ workbench this is the GUI version right and I already have a local Instance created here and if you don't know how to how to use this do watch my video on how to use mask or workbench but if you already know that's let's continue so first of all we have to create create a database right uh in fact we I do have lots of databases here
but let me create one I let me use my n database as you can see we already have a database here I will use that one in that database I do have some tables but ignore those tables for time being uh I Will say create uh okay I will say use naven first I will say use naen so I'm using this database and now I want to create a table so I will say create table and I mention the table name as let's say login I mean there should be a logical name for this login
will do for this example I will say login and in this login I want some Fields right so I will say the first is username that that is your name of type Vare which will which will be off line 20 and then we'll be having a password we'll say password and again this one instead of saying password we'll simply say pass and we'll say this is where C and this is also 20 again you can have more than 20 uh 20 uh text so you can change it in that way you can have a username
of 50 letters if you can have a password of 100 letters if you you want to torture your audience or your C or your clients okay We got this table here let me execute this query and you can see we got our table ready now let's insert some data here just some demo data so that we can verify we'll say insert into login there should be a registration form for this again you can write your code you can create a form for a user to register themsel but time let's imagine if you already have data
here so I will say insert into login values I'm inserting some values here I Will say the name is naven or will have a small letter N so that's my username naen and my password would be let's say um 9 okay this is my password which is 9876 again that's uh you can have any password there so let me just run this query let's see if is getting inserted and everything is fine it is got inserted let's have one more dummy value uh let's say this is um we can have any one we can say
uh Tusk and the password for Tusk would Be let's say Tusk we have a username and password both as Tusk so if I run this query you can see it is working let's F the select query just to verify if you got the data there so if I say select start from login and if I say run you can see is working we got nav 9876 and we got Tusk and Tusk that's that sounds good right now what's the next thing the next thing is once you got your database already once you got a table
let's verify using uh using Java code Now how do I do that now first of all when you say you want to verify you this is not the right way right this is not the right way of verifying it normally whenever you want to connect with database uh there are different layers we work with we work with this is not a big application we can write the Java code here itself in fact we can write the Java code in this in this method itself in instead of this if condition here we can write the Java
code but then What will make more sense is if you create a separate class because when you work with database it's having a separate class will always I mean it it will always give some advantage and that separate class will always be in a different package okay not compulsory but then it's better to have that in that way I will say this is login da now da is a special keyword which you should know uh da simply means no it's not a keyword but it's a Standard we always use da stands for data access object
so whenever you want to interact with database uh it's better to work with Dow classes and I will say this is belongs to Dow package again you can you can use login repository you can say login database you can say anything you want but Dow makes more sense and if I click on finish you got your class here Dow class now in this D class I'll be having a method called as public and I will I will say this method will Return Boolean so if you want to verify if if the login was successful you
will say true if the login failed you can say false all I will say check uh details or check credentials we can you can use anything we can say check details or we can say only check anything we do right so we can say check details check credentials so let's have a check method which will take two parameters one is username and the second one is password right both need To be taken both will be taken here and once you got this values let's return false by default it will be false right okay now how
do you verify so first of all to to connect with database we have to write certain steps now if you have if you know how to work with jdbc uh you can continue otherwise what I would recommend is if you have not learned jdbc before uh do watch my video on jdbc first because that then it will make more sense uh you will find those videos In the description area uh so make sure that that you know how to use how to work with jdbc now first of all when you work when you want to
work with jdbc we work with different type of dbms right in this example I'm working with m maybe you want to work with Oracle for different dbms we have different jav files which you have to work with and those jav files are normally call as connectors now question Arise from where I will get the jav file now you have two choice you can download this jav file from the internet okay from anywhere uh I guess I already have that jav file here so if I say ml connector you can see I do have that file
uh this Mas connector I just have to say show all in finder okay I do have that somewhere uh okay if I want to check the location for this I will say reel in finder okay if you're using uh Windows you can you can actually search That what I will do time bin is I will put that file on the desktop right so we just want this masle connector to be used okay this is one way the another way is you can actually get the Java file using Maven so if you have if you are
familiar with mavin you can convert this existing project into mavin okay and then you can add your Java file uh not needed in this project but then you know we can actually try it out so We can right click here and you can say configure click on convert to mavin project you will get a mavin project and you can add that jav file using just a text again it's your choice time being I will not convert to mavin let's use the same project so when you want to use the same project what you have to
do is you have to paste that jav file in this lip folder so past it here and you can see we got M connector now if you don't have the Java file with you uh what you can Do is you can just go to uh you can you can use Google to search for that or you can go to MAV repository and in this m repository search for mask your connector okay so if you go here you can you can select any version I will prefer this table version I will go for 4.1.4 one and
you can click on download job so this is one way you can download okay but you using MAV it will be very easy but okay it's your choice again so this Is where you will get this jav file just click on download you will get the jav file just add it in the leave folder now we have to also add that because for develop developing also we need that so we'll go to properties we'll go to Java build path and here as well I will add that particular Java file so I will click on add
Java files I will navigate to my desktop I will say mask connector done okay now let's write our steps the first step is you have to Create you have to load the class I will say class. forame ignore that error of eclipse so yeah class. forame and inside this you have to mention com. MySQL what's happening com. MySQL jdbc do driver okay we got our class oh it might thr an exception so let's everything let's put everything inside a tri block okay and let's generalize it to exception so that I don't have to write Trip
block once again okay so we got class. forame the next step you have to create a connection object uh we'll say connection con equal to uh driver manager. getet connection again there are multiple ways of creting this connection object we can use data source we can use yeah we can use data source we can use uh different tools available right uh we can use u a connection pooling concept but time and let's make It simple I will simply use a u RL here for database I will say string URL and the URL for mql is
jdbc colon MySQL colon SL localhost colon 3306 as dbms changes this link will also change and my database name is naven right so we got this thing we got uh URL here the next thing is username uh we got username we got this username and the username is root and we'll say string password is equal to zero for my Case the password is zero okay so we got this URL then we got a username and we got a password right so we need these three things so we got URL we got username and we got
password uh I guess I don't know why it is giving that symbol just ignore okay okay just ignore that okay so we got URL username and password the next step is you have to get a statement object this is databas steps right again if you're not familiar with this do Watch my videos on JBC that will make more sense okay we'll say create statement or uh prepared statement will make more sense here because I'm passing the values right so I will say prepared statement but then how will you fire this query now that's that is
important right what query you will fire now how to verify we have that username and password which is correct now there are different ways of implementing it what you can do is you can you can fetch the Entire table and you can verify each and every record you can run a loop and you can navigate to The Loop you can find the username password is matching or not but then don't you think you're fetching the entire table that doesn't make sense right so what I will do is I will create a query which will be
bit bit efficient than normal query we'll say select uh we'll say select star from uh from login but then where I will say where un name is equal to question mark and pass is Equal to question mark now we'll replace those question marks okay first of all let me just write it SQL here we'll replace those question mark we'll say SE string let let's replace the first question mark with un name and let's replace oh I just made a mistake here the problem is this password and this password is same I will make this as
pass okay that will be better and this would be the password for the database which is this one so This is my password this is database password that's weird I know I will say st. set string and we'll say two and this will be pass okay what will happen now is when you file the query example if I say username is naven and password is any password so it will try to match if it will try to match this thing with a database and of course if I have my entry there with that username and
password it will return something otherwise it will return nothing that's What we want to do we just have to fire the query we say result set RS let's import the package and I will say St do execute query now if if I simply check if rs. nextt if there is a record then return true it's that simple this is what we want to do okay let me just do this once again let's say if I enter the username and password okay the username which I enter now is naven and the password is 9876 so the
F the query would be select Star okay the query will become select star from login where you name is equal to uh naven and the password or the pass in this case is equal to 9876 if this is the data which I'm entering in the in the while login and if I run this query you can see we got a record but let's say if I make a mistake let's say if I entering a wrong password now in this scenario if I run this query you can see we got nothing that's what we want to
do if uh if we got something After filing the query that means it is a valid user otherwise he's not a valid User it's that simple right now once you once we have done this code let's go back here and let's create an object of login Dow again you can create that object anywhere you can create a reference somewhere here or just for this example not a good practice again I'm creating the object inside this I will say login Dow we'll say da equal to new login Dow Let's import the package as well here itself
let's say import the package okay package is imported and here I will say instead of checking that thing we'll say Dow dot uh check with that username and particular password whatever username password you got and that's it your job is done if it is verified it will work otherwise it will not work let's verify let's run this code right click run as run on server I think it should work work let's Verify it's running it's running it's running waiting for the console waiting for the browser to pop up okay I got my Firefox browser here
hey I don't want to make your default pass default browser okay so if I say welcome uh. JSP now and you can see we got a login page let's verify with Tesco and L and this will not work of course because I don't have that data there the data which I have is naen and the password is 97 okay that is 9876 if I Click on login you can say it is working now right if I go back if I log out and if I give a wrong data example if I said Tusk and task
one if I give a wrong password you can see it is not working is it it's not working can you see that but if I give a right username and write password Tusk it's we got the login right so that's that's how we can verify with database so try it out and let me know if you have a better solution in the in the comment section So if you think we have we do have a better solution than this uh comment section is open for you so that if if I feel that is better I
will make a I will surely make a video on that as well and yeah so we have we are expanding this project like anything we uh okay so we got the login now with the help of uh database maybe in the next video we'll talk about how to do client side validations right and in this video we'll see an example Covering saet JSP mavin and jdbc in one video we are C covering all these things so this video will be helpful for those people who want to revise all these topics so maybe you have learned
these topics from somewhere and now you want to make a quick recap so this video will be helpful for you because we will be covering most of them uh I mean the internal topic of that as well and then if you want to just see how saet JSP M and jdbc looks like you know you you see You talk to lot lots of people and say okay you should you should learn J savet and JSP but you're not sure what exactly we see in that you know some people know Cod Java and they just want
to see how s JSP looks like so this video will be helpful in that way now again this video is not for those people who want to learn s and JSP in day because we'll not be covering all those thing in detail here but then if you just want to get a gist and then if you want to if you just Want to do a quick recap this video will be helpful for you so let's get started now first of all to make it work work we require two softwares here the first one is Eclipse
again you can go with any IDE which you which you like maybe you can go for net beans or eclipse or in J which you like in fact in my machine I already have Eclipse so I will continue with that and then the second software we need is a rdbms so in this example I'm using myql you can opt for Oracle Poist whichever you like so I'm in this in this video I will be using eclipse and uh my skill yeah so so in my Eclipse you have to open CP you have to select a
workspace and then let's create a project so I will say new and now you have two choice you can go for a d Dynamic web project or you can go for mavin project now what's the advantage of going for mavin so in Dynamic what you do is you create a project and then you assign a tomcat server again you Have to do that so you have to assign a tomcat server and then what you do is you uh when you when you want to use some external jar files example for MySQL for jdbc we need
a jar file right and you have to go to Google I mean you have to go to internet you have to download the Java file and you have to add in your project now when you work with lots of dependencies it creates some issues uh example if you work with Advanced framework like spring hybd net Or maybe STS framework there are so many dependencies you have to work with so inad of downloading all those dependency by yourself I'm talking about J files okay what you can do is you can you know uh you can uh
ask mavin or a build tool to give it to you so mavin will say Okay programmer you want uh you want my connector I will give it to you if you want spring framework dependencies I will give it to you so your mavin will give you all those dependencies again You have you have to config somewhere you have to inform M hey what what are what are things I need so M will give you give you those things provid is there with mavin and luckily we have all the Java files available available with mavin okay
so here I will be selecting a mavin project and how to do that just click on others and you will simply say okay it's taking some time I will say mavin so search for MAV project and you can see we got M here click on Next and we'll say next here we'll mention the project type so I'm going for a web app project again you can select quick start if you want to go for a standard on software here we are building a web application so let's select web app click on next and here we'll
enter group ID so group ID is basically your company IDs so let's say if you have th projects on on some web services or some Play Store you have to make sure that every app is different And you can differentiate between those with the help of group IDs and the package name so I say group ID is scisco that's how I represent my group ID I can say fact I did the project name I will say sulet JSP demo again this is seret JSP jdbc and M demo but let's name it as JSP demo and
we'll click on finish so we got our mavin project now this project is coming from the internet you can see is taking some time to configure now I will if I go back to my project or not This one this is the spring project so this is the project which we have created now which is uh seret JSP demo again it's taking some time to make the to build the project and you can see we got our project here we got resource folder we got JRE we got MAV dependencies where M dependencies so this is
your uh where is that where M dependency so if I if I open Java resources uh you can see in this we have M dependencies and we only have one Dependency what I want is I want a myql dependency as well because if you want to work with MyQ again in this application we'll be working with a database right so we require a mql connector to connect with this database in fact I also have a database ready with ready with us uh so I have installed MySQL preand so you can install MySQL or post is
your choice so I have Mas with Mas workbench so it's taking some time to open so by the time It opens what I will do is I will download the dependency now question arise how will you get the M dependency it's very simple you just need to inform M hey you want M dependency and how do we interact with mavin uh you you do that with the help of pom file so in this pom which is Project object model you have to inform what type of what type of dependency you want so we just need
to add a dependency which is Master dependency here so we already Have junit now we don't have to TI it by yourself okay if you're thinking you need to buard all the dependencies not needed you can simply go to a website and so the website is mvn repository okay so here here we search for mvn repository and it will take you to M repository where you just need to search for which dependency you need so here we need a mql connector so if you can search for M connector it will give you a connector here
so if I click on search And it will give you connector and which one you select you can select any version I will select 6.0.6 just copy this and go back to your myql you go back to your eclipse and paste it here the moment you paste it you can see it will down download the Mas for you and our job is done so using mavin you can download any dependency if you want if you want Mas you get it there if you want spring fra spring framework Dependencies or jav I'm talking about you can
get that using Maven so Maven has lots of Advantage like it's a build tool you can build the application it will package the thing for you but time mean we only need a jav file yeah so we got a Java file there now the next thing is uh the next thing is I want to get a page right so using which I can send data so if I go back to my my workbench you can see I will just connect with my work with my workbench and the password Is star star star star which is
Root in this case for my machine at least and okay so I already have a database call as n if I explore navine you can see uh we have a database navine in which I have a table called as alien so I already have all this data ready with us so that you know when you work on a project it will not make it will not waste much of time so I have a m database which is uh naven and the data the table name is aliens we just need to Fetch data from this example
if a user says Hey I want data about uh user ID nine so it will print nine Maria and Hardware if someone says Hey I want user ID two so it will print two archa big data if I want five PR and PR and Oracle so this is what I want to do so for this we need a page right so that a user can enter the value so if you can see we already have a index.jsp page now why do we need JSP why not HTML because when you say you want to make a
Web page you always go for HTML right but in this case when you say you want to create a page and that should be a dynamic page because see what happens with HTML is you can make only static content with HTML oh again we if you are using JavaScript and then if you're sending a async request we can achieve Dynamic content there but imagine if you want to make a dynamic page without using JavaScript and all the stuff if you want to write Java code inside uh Inser HTML is it possible can you run Java
code inside HTML not exactly right so that's why we require JSP so whenever you want to create a dynamic content you have two choice you can go for salet or JSP now when you want to process something s is better and if you want to get a view JSP is better so suets are used to process the request so whatever a client sent a request Ser will handle that again you might be thinking why suet then we can use normal Java right Suets is Java okay suet suret is is a Java file the only thing
is in saet it has a special feature where it can accept a user request from from the internet right uh it's normal Java code if if IUN if I if I make the code it will it will make sense but then we have saet which will accept the request and we will process some information and then we will show the data on JSP now again we have one more thing which is called as MVC which stands for model View controller now MVC is used let's say if a user sent a request request will go to
controller now controller will generate data again if you want to know about if if you're new to MVC please watch the MVC video first I do have a spring I mean I do have a video on MVC do watch it first and then uh so if you know about just to make a revise revision uh we have a controller which will accept the request we have a model where you will hold the data and we have A view where you will show the data yeah so we can Implement controller with the help of suret we
can Implement model with the help of a normal Java class and we can Implement view with the help of Js so this is our JSP here and you can see we getting some error here the error is because we don't have Tomcat linked here so we have to make sure that I do have tomcat and fortunately I do have a tomcat here can you see that let me just verify if it is working so if I say Tomcat start uh so Tomcat is there and it is in running State I guess yes it is running
State you just need to connect your project with Tomcat because your project is not connected with tomat yet so I will right click here and I will say properties and this option of Target one time you simply have to check this thing which will make sure that your tomat is linked and click on okay now this will link your tomcat and we got a tomat There so you can see no there's no problem with JSP file now in this file I will create a form tag and I will say form action equal to and I'll
mention I want to fetch the user data so I want to specifically I want to fetch the alien data because the table name is alien right so I will say get alien so that's my user request so action is get alien and here I will specif what I want to fetch so I want to fetch uh the data based on the a ID so if I mention a ID I Want the complete data of the user okay and then I also need to make sure that I have uh I have a button here to to
click it so I will say input uh type equal to submit yes so I will say submit here okay now when I click on the submit it should call it should fetch the data right now question arise when you click on this submit there need to be a controller who will accept your request right and that's where we have subet so Let's get a simple subet here and there's one more issue I don't have a Java folder you can see that in SRC we need uh we need Java folder as well so we have to
make sure that you have Java here okay not here let's let's go to Main and say I want to get a new folder I will say new uh folder and that folder name is Java folder which should be inside main okay now in this Java folder I want to create some resources so I will right Click on my project I will say oh what's that and I want to I want to Sol it right so I will simply say I want a class or maybe I simply say C salet so right click I will say
new sulet and this Ser will have a name so it will have package name first I will say com. Tesco and com. Tesco do web and this class name here will be alien or I will say get alien controller yeah so I'm specifying a controller name as get alien controller I will click on next now the URL mapping so for which request I want to call this controller so the request type is get alien so whenever I use a request for get alen I want to call this controller so this is the linking this is
how you link so you have a controller which is a saet and you have a request so request is get alien right and the controller is the name of the controller is get alien controller you can see the spelling difference there right so whenever are User request for get alien you can see in the HTML we are specifying get alien you have to request get alien controller click on okay and click on finish this will create a solet for you and where's the mapping you will be seeing the mapping will be done inside web INF
so web INF we have web.xml file this is where you have done the mapping so the mapping is done with the help of subit tag and solid mapping tag so in the solid mapping you specify for Which request you you want to call which controller can you see that the controller name is here the class name com. alisco web app.get alen controller simple right so that's how it works that's how it does the mapping now if I go back to controller here we have a class we don't need all this extra thing we just need
a do get method so basically when whenever you have a get request so basically we have two type of request in fact we have five but then in Serv we used to we have get request and we have post request now get is something where so if you want to fresh data from server you use get request if you want to submit data on the server you use post request so get to fetch and post to submit data here we are to F data right and that's why we using get request okay now once you
specify get request what to do so based on the aid first of all a user is sending Aid right so if you can see in the index page a User is sending Aid how can you fetch the a ID in this subet thanksfully we have a request object so us request object you can actually fresh data so I would say request. get parameter so request is an object of HTTP subject request so we just need a method called as get parameter in which you specify hey I want data which is coming from a ID so
request. get parameter a ID it will give you the data but then we have to store It somewhere right so okay I will just delete some extra roles here I will say I want to store that in a a ID variable so I will say int a a ID equal to request. get parameter now unfortunately get parameter will give you a integer oh sorry it will give you a string I want integer so I will simply say integer. pass int and here I will just try to remove this yeah so you can see I have
I have uh I have passed it right so you you're Converting your request data into a integer format now once you got your integer I want the object of alien right again you can fetch one one data you can fetch Aid you can fetch name and you can fetch Tech so you can see the column names we have Aid name and Tech so these are the three columns in your database now you can fetch these three values but we are working with Java right and Java is purely object oriented that means even if you want
to fetch This data this should be passed in the object format how do we get the object so to create the object first you need a class so in this I will create a class and this class name will be called as alien and this alien is a model so whenever you have a class which which whose object will represent some data that's a model object yeah so I would say I have alien here and this alien will have three things it will have private in a ID it will have private String a name and
it will have private string uh Tex so we have these three things and for these three variables I want Getters and Setters so I will simply right click here I would say Source generate geta Setters and I will select all the variables here and I will click on okay now once I got this data cers let also use two string mirror so that if you want to print this object it should print some data right so I would say uh Source two string Because what will happen is if you try to print a reference reference
of of a class it will print the hash code I don't want that I want the data so override two string so whatever possible in fact I would suggest for every class override two string that's how we do it okay so now once we got two string here let's go back to controller and now we can create object of a ID so I would say uh Alien so first create object of alien so I would say alien A1 equal to new Alien let's create the object of alien no we should not be getting object of
alien right because you want to F this data from database okay that's that's a thing right how do you f data from database now remember this when you want to work with database you need to write database steps jdbc steps right now jdbc has seven steps in it now if you think about those seven steps the first step is you have to import the package you have to then Second step is you have to load your driver you have to create a connection object you have to create a statement object you have to process results
and then you have to close the connection there are so many steps right now where to write all the steps should we write all the steps in inside the controller which is serlet that's not a good idea in sulet it should so s should only fetch the request or it will accept the request it should do some processing but Not DBC processing and then it should return the data to the view so who will do the JBC processing or who will have the all the content all the all the code and that's where we have
a special layer which is called as Dow layers again it's not compulsary to go for Dow layer but that's a good practice right if you want to be a good developer if you want to create a software which will be having less bugs go follow some standards and that's Where you have a dow layer so let's get a class and I will call this class as alien Dow which belongs to a package com. telesco uh. web. Dow so we are putting that in a dow class or Dow package alien Dow so example if you have
five tables so you'll be having five classes which represent those table and you'll be having five Dow Dow classes right so that's how so every class will Have its own every table will have its own Dow class and in each Dow class you'll be having those four operations uh four operations yeah uh create create data or create alien read alien update alien delete alien right so cut options normally normally that's what we do so in this in this uh Alien to I'll be having a method which will return me the alien object and I will
say this is get alien so this get alien will accept a a ID and it will give me the alien object That's simple so let me just create alien object I will say alien alien and then I would say this alien or we have to import the package for this because it belongs to different package than that that and it will say a do set a ID so time I I I will specify static IDs okay static values so that let's wait let's see if it is working or not I would say name is naen
not small capital a then a do set Tech as my my technology Java always Java and then I will return The Ali object of course this data should be coming from database but then let's go step by step now once we got this class let's go back to controller now the alien data will be coming from the Dow layer right and that's how that's where you require da object so we'll say alien Dow and I will say da object equal to new equal to new alien da yeah and let's import the package for that so
you can import the package in Eclipse by saying control shift O So control shift o is used to uh you know to import the package for required packages and using this da now you can specify hey da get me the alien if I pass the value which is a ID so if a user says a ID one it will pass one but that doesn't matter because even if you have even if you passing one you are passing static values yeah so we are doing some know uh some thing here some weird stuff anyway so once
we got the S Object I want to display it but where to display that and to display this data we need to create a JSP because JSP is a place where you can actually show the data right again you can print data from here as well but creating a HTML page in server is a bad thing okay it's not a bad thing but then it will be damn difficult to design a page think about it let's say if you want to make a awesome website where you have lots of Graphics so each so even if
you create a Small page with lots of Graphics it needs around 70 80 lines of HTML tags what do you think will you write all the HTML tags in this in this Ser by saying out. print that's not a good idea so that's why we we require a separate page where you will which where you will have design and where you can put data so let's do that so in this uh in this web in this project or in this project I will say right cck I want to get a JC page which will show
my alien I will say Hey JSP show the alien so I will say show alien. JSP I will click on finish so we got a JSP page here which will have some layout example I will change the color I will say BG you can design your stuff I'm very bad with HTML uh so you can see I'm simply saying BG color equal to Canan that's it that's what I know about HTML and here I will print data about alien now how do we print it first of all how Do we call a JSP from a
sub that's the first question right so I will go back to seret now how do you call a JSP now there are two ways of calling it one is dispatcher which is request dispatcher the second way is using send redirect which one to use here now we can use anything but then it is always recommended that if you if you want your client to know that you are getting redirected then go for redirect otherwise if you're using dispatcher Client will not even know what is happening behind the scene and that's what we want right we
don't want to confuse our clients by changing the URL every time so because a client is sending request to get alien not show alien right because if I use redirect the URL will be show alien okay so what I will do here is I would say I will use the dispatcher so I will say Dispatcher and I will say Rd equal to requ dispatcher is interface and the way you can get the object of it by saying request dot get request dispatcher and in this you have to specify so in this bracket you have to
specify the name of your JSP so name of the JSP is show alien. JSP yeah and you simply have to forward the request you will say rd. forward and you will pass you will forward these two objects which is request and response Because you want to send so when a user send a request requ EST right user send a request to the index page right from index page user sending request to the controller that's one request the same request object will go to JSP so users should not know that we sending two request right so
that request will go to JSP and that's why we have to forward otherwise if you don't want to forward is to your choice you can go for another way as Well okay which is dispatch which is redirect way time and let's go for dispatcher and now it will forward the request to JSP it works okay well let's verify in JSP I will print in show aient just to see if the flow is working yeah so we are we will be running index.jsp index.jsp will call a controller which is your seret and ser will call JSP
let's verify right click run as run on server it will I guess it will run on my uh Firefox maybe let me just remove this MVC from here because that's not required click on finish uh my browsers are working so very slow today yeah so we can see it is asking for the a ID I would say 101 I would click on submit and you can see we it is printing a JS page remember the page color which is Canan color and then uh see the request request is still get alien even if you're calling
a page which is get uh which is show alien the you URL is get alien because we're using Dispatcher so a client don't even know which page is getting called that's the beauty yeah but if you're using redir then you it will it will show the page name okay so I will stick with dispatcher Salvage but how do we pass this object with alien here so I want to pass this alien A1 object to JSP and the way you can do that is by using the request object because in forward you are sending request object
right so what if you can simply add your data in the Request object it's something like you know let's say if from us if your friend is coming to your place let's say so if I'm staying in India now so let's say one of my friend is coming from us to India and if I want to order something from us us he can carry it right so he's not coming to India just to give me the uh just to give that uh thing to me but he's coming to India for his own work but then
I'm asking him to take that thing for me that's the same thing we Can do here the forward is sending the request object right so we can put that data into request object and send it so I would say request dot set attribute and I would have a name so I will say I'm sending the alien data I'm sending alien and the object is A1 so request. we are sending alien which is a A1 here and in JSP you just need to fetch it now you can write Java code here right so to write Java
code inside JSP we simply use this uh This angular backet and percentage this is called as criet yeah so in JSP we have this we have declaration tag we have scriptlet tag we have different tags available but if you want to Java code inside this we can simply use scet tag and I would say I'm I'm fetching the object of alien which is A1 here but how do you fetch it so you simply say request. get attribute and you specify hey I want to fetch alien now if you remember in sulet In sulet we have
set we have set the attribute in JSP we are getting the attribute with the name Alien same thing right and here we'll print the A1 object oh not this way we'll say out do print Ln and will say A1 okay I I guess we have to import the package now this alen pack alen is a class which belongs to package model so you simply say control space and that's the package you can see import package comes something like this this is how You import packages in JSP not like normal normal Java code where you write
import space and package name you have to say import equal to because page is an uh tag for you now and this becomes a attribute okay we are getting some error here because we have to typ cast it because get attribute will give you object of object I want object of alien so we just have type cust it it should work now right it should it should print data at least some data right so let's Restart the server and let me just go back to my page here refresh and I will enter data which is
101 I will say submit can you see that we got our data we got alien a ID is equal to 101 name naven and Tech Java but hold on this is this is static data right this is not coming from from database so how do we connect with database here so if you want to connect database you just need to use Dow layer right because this this is where you are Fake you are faking this data you just you don't need to create a dummy data you want to F it from database so let's remove
this or maybe you can just have it there that's fine what I want is I want to write database code here now to work with database we need to follow certain steps right the first step you have to import the package so I will go here will say import java.sql dostar that's how you import the package uh and then the next step is you have to create The connection object now I would say I will simply create a connection object outside the method or let's let's do that inside just just for time being I would
say try catch because it might generate error so I will say try catch and then here we'll write all those statements we'll say uh first the first step is we have to say class. for name and here we have to mention com do my. jdbc do driver yeah so this is how you load your Driver so for mcale we have this string for Oracle we have a different string for p have a different string so you have to just go to Google and search for the uh the driver name for different different drivers different different
dbms that's the first step we got your class. forame next is you have to cre a connection object so I will say connection con equal to now connection is interface and the way you can create the object of it was by saying driver Manager do get connection and here we have to pass three parameters so in this get connection we have to pass three parameters the first one is the URL the second one is the username and third is password so password is root and the username is also root for MySQL for my machine and
the URL is jdbc colon MySQL colon slash Local Host colon 3306 slash database name is naen I know it's a big string right but then this is Called as U RL which is which is used to connect database so jdbc col m/ Local Host colon 3306 slnv now once we have done with the connection object we need to create statement object because let's say if you want to call someone you uh you create the connection first you dial the number you connect and then you think what to say and then you say it right so
that thinking is statement right so let's get a statement object I will say Statement St equal to con do create statement now you are you are saying you you are you'll be saying something and how to say so you will say st. execute query and you will find the query here what's the query for us it is Select star from Alien that's the uh query you have but then this query is not complete because we have to specify the ID as well right because I don't want all data I want only one so I would
say Where a ID is equal to plus I'll mention a ID simple and then this execute query will return you the object of Al not this will not return object of alien right it will return the object of result set because statement object Returns the object of result set so I would say result set R is equal to so result set interface so the moment you file a query it will not give you normal data right it will give you a table structure and that will be Stored inside result set and now from result set
you can say so we so we have to Traverse between different rows right so if you you get five rows how do you specify which row to pick up and that's how you that's why we use rs. nextt so. nextt is basically to shift your pointer between different rows and we are not even sure if you have a next row and that's why we'll say if so that we can avoid the exceptions if you have a next row because next will Also give you a return return it will return bullion value if you have a
row if you have a row then what we will do is we'll fetch one one value and how do you fetch so we have so we will fetch and we will assign to the alien object we say a do set a ID now this data is coming from result side so we say rs. get int the First Column is a ID so we have to mention column name so when I say get in you have to mention the column name we'll say a do set a Name I will say rs. get string because a name
is string format I would say this is a name you can also use column numbers if you want 1 2 3 or you can use column names so to be on safe side always use column name yeah it is safe right you know what column you're fetching you don't have to remember the sequence of uh columns and once we got it that's it we don't even need to specify the static Value because data will be coming from database now these are the steps oh one more important step you have to close it but that's fine
you can do it in your code and after doing all this thing let me just verify it is working I will right click here I will say run I'll say go back and refresh and this will say nine I will say submit you can see we got nine Maria and Hardware if I say five submit you can see we got five PR and Oracle if I say one you can see we Got naen Java so it's working right so we have done with uh mavin jdbc salet and JSP in one code so this is how
it works awesome right again this video was not to not for uh beginners this was for people who have already done JSP salet and just they want to revise it or if you just want to see this stuff you know some people have the Curiosity of how I mean what exactly it is you know so let's say if you're learning code Java and someone says hey you need to learn s JSP in future and they're not sure what they are what they're going to learn so this video will be helpful for them so as I
mentioned in the as I mentioned earlier so it's that awesome now once the once we once you once you watch the video check the description area where I where you have a we have a where you have a link uh so I have uploaded this project on GitHub so you will find the link in in the description area and you can add your own own code now so Assignment for you after this after this after after doing this code on your machine is uh let's say I also want to create a new alien because I'm
just fetching an alien right what if I want to create new alien so in this I have 101 carel Java right let's say if I want to have one more one2 U let's say Rika and some technology maybe let's say say python again so in that case you need to create a alien and where do you specify the data so you have to create a page Where you will it will ask you for three values when you click on submit it will go to a controller and controller will use a dow layer to save the
alien right create a new method called as save alien here and write the same steps and save it in the in the uh thing in fact the moment I will when I get some time I will I will upload that as well so you can check the description you will check the uh link it may be or may not be available when you when you check this Video when you check the link okay so that's it I hope you enjoyed the entire session let me just go for a quick recap what we have done till
now so first you have a index page where you're asking for the uh Aid when you click on submit request goes to the subet and this is how you create a solet you create a class and extend with s solet you have a dog get method here yeah and then in dog get you're fetching the a which is coming from client you are using a dow Layer to fet data and from Dow you're saying get alien by passing that a ID and that same data you're assigning to A1 there's one more thing we can do
here you know if you if you don't want to use equ dispatcher you can also use redirect so the way you do that is by saying response do send redirect and you can specify the name which is show alien. JSP so we can specify this as well but the thing is if you if you're using this one what will happen is you cannot use Request object because a client will send a new request when I say client I'm talking about browser here so in total you're sending two request one to controller and second to the
JSP as well so in redirect your response goes to the client again and from client it goes to the show show alien so it is a new request object so this will not work so this request will not work in that case you have to use sdp session so you have to work with session management so we Have cookies we have uh web URL rewriting or you can also use HTP session session is the best thing I feel so I will say session uh session equal to and the way you can get the hold of
session is by saying request. get session and now you have to set the attribute in session not in request object so I will say session do set attribute the same thing alien and A1 you're passing A1 so nothing changed the only thing is Instead of using request object using session object now because it doesn't matter how many requests you send to the server now the session will be maintained and in show alien instead of fetching from request object you have to use session object cool let's run this code let's verify if it is working I
will r as or not let's let's restart the server that will be enough let's go back to the page and refresh let's enter one again I will say Submit now look at the look at the URL you can see see now you're calling show show alien. JSP in the earlier case it was get alien because since we are using redirect it is changing the URL as well so as a client now I know that I I got redirected to a different URL that's the beauty about send redirect yeah so that's how you use session object
in this video we'll see how to upload how to upload files on the website so let's say if if you're make If you're a website in Java so you'll be using S JSP for the server Technologies right so in this video we'll see how to upload multiple files on the server okay so it will be useful for your website where you where you want to upload multiple files maybe you want to upload a photo maybe multiple photos so let's see how to do that so of course when you when you say you want to build
a website you need a project right so what we'll do is we'll start with the project and We'll send new and we'll go to others now of course we can make a simple application I mean we can make a simple web application or uh in this in this video I'll be using a mavin uh MAV project again you can go for simple application you can go for you know you can simply search for dynamic web page Dynamic web project so you can go with this and the thing is in this project we'll be needing a
a jar file so now why we need a jar file to upload a File on the server so Apache provides you a jar file a library to do that okay so you have to you have to make a project and then you have to download the jar file from the website or from the Google other instead of that what I will do is I will create a mavin project because when you make a mavin project it is easier to maintain your libraries right so I'll be going for a mavin project is not that difficult uh
you can choose either Dynamic project or mavin Project so we'll select next and we'll send next now here uh whenever you work with mavin you have to select the type of project you're working with the arch type uh I will select mavin Arch type web app okay we'll say next now if you don't know how to work with mavin if you want to learn about this again uh on my channel I have a mavin video so you can just search for mavin tutorial on telesco learnings you will get that uh I will create I will
make a group ID which Is a package name we say com do uh Tesco and we'll make an artifact ID as file demo or file upload demo so my project name is file upload demo and we'll say finish now once you click on finish you will get a project on the left hand side you can see we got a project here which is file upload demo and in this thing the first thing we need is the library right of course when you work with uh file upload you need a library which is Apache common file
upload so there are Multiple ways you can do that you can download the J from from Google you can just go to Google and search for in fact let's do that let's let me show you how how that works if go to Google and search for common file uh what is the spelling off common so it is common file upload okay it's upload so if you can uh search for this okay this is the this is the library you need okay you can download jar file from here okay in fact they also have a code
For you that's good so you can download J file from here but I'll I will not be using uh this common file upload jar file from Apache I'll be using uh from mavin so I will say so how to do that just double click on the pal file which is Project object project object model file and you can see there's a option of dependencies here and you can say um maybe add and search for I hope it will work here otherwise we have different way so search for common file Upload it should take it up
if you're n is working there's something wrong here indexing is disable that's weird uh Commons no that not this one file upload that's my project not this one okay this is not working here so what we what we can do is we can go to P XML file and here we can add the dependency now you can get this dependency from mavin itself so you can just go to Maven website so you can see MAV repository common file upload so when you search For common file upload on Google it will give you mavin option option
also uh you can download any version I will prefer 1.3 which is a stable version and you can just copy this file in fact you can also download jile from mavin I prefer mavin in fact lots of Industry people they prefer mavin project and let's paste it here when you say save you can see it is somewhere in the Java resources there should be libraries and there should be M dependencies and you Got your file there can you see that we got Commons file upload and commons. IO or Commons hyphone iio so we got these
two libraries now once you got this now what next now once you got the r file let's create a web page from where you can upload the files okay so let me just go to um okay let me get a web page so we'll say right click I should already get a web page here do we have a web page buil okay we we already got a index Page uh there's one more issue with the project uh it it is see whenever you create a jsv page or suet page we need something called as uh
uh Tom server right again Tomcat or Joss whatever is preferable let me assign a tomcat server here how to do that how to assign a tomcat server I click on the project and say properties there should be something called servers here you see that or server or runtime where is the option Target run time so you have to search For Target runtime and select Apache Tomcat 8.0 if you don't have that then you have to download Tomcat you have to click on new and then you have to select the particular version you want to work
with Apache then specify the version and click on download it will download from the internet but since I already have Apache tomat here we can also use J boss but I have Apache lightweight so click on okay and you can see all the erors are gone because it was searching for The tomcat and we got Tom Cat now what else I need here so once we got this page um okay hold on so once we got this page let's create uh the page I mean the create let's create the required page here so in the
body tag what I will do is I will create a form tag okay let me because I want to upload a file right so when you want to send something on the server we need to create a form tag and in form we'll say action this action Will call something called as upload URL that's upload and the type of method we'll be using is post because you want to send data to server and in this let's create input type so when you normally when you create input type uh when you want to send text we
use input type text right but since we are to work with file so we have to say we have to specify input type file now when you say input type file it will give you a option of Select I mean you can browse your files here using type file we can also uh mention name we'll say file itself and that's it that's your input file type and we also need to send data to server so we'll say submit button so that's submit I guess that's it uh that's what that's what we need let's right click
and let's see if we are getting this we are getting something which we need we say run on server I will select Tom Catch next I will click let me remove the old project from here let me just go with the new project and when I say finish you can see it will run and it will run on the server which is Tomcat and we got the output here so you can see that we got a page which says hello world and it is giving me option of choose files now why we're getting option of
this choose file is because we are writing a file here right so when you say type file it will give you Option of choose file and if you can you can choose the files from here you can select any file I already have Java demo. Java file if I click on open you can see it says demo. Java I can if I say submit oh there's error because we have not created this server site yet but I need one more thing here when you say choose file I want choose files it should it should it
should select multiple files here and for that what we'll do is we'll simply say multiple so Multiple is attribute using which you can achieve multiple files so you can see we are having choose file if I refresh this page we got option of choose files and if I select this I can actually select multiple files now uh can I do that let me select multiple files do have do I have multiple files here uh so we got this one and this one if I click on open so you can see it says two files okay
that's it that's on the client side but hold on there's one Little issue here when you send data when you send files on the server you have to also mention the encoding type okay so when you send files we have to mention the encoding type and inside encoding type you have to mention I'm sending a multi-art document so is multiart so to say multiart document and some multiart and form data so this is this type of data you want to pass it's multiart form data okay so now once you click on submit it Will call
this URL which is upload and on that upload serlet you'll sending all these files so clients are clients are done let's work on the server side now and on this server side we'll be creating a solet so we'll right click we'll send new and do we have option of subet here yes we have a suet and let me mention a package name as com. Tesco itself and the class name I will say is file upload okay so we got file upload which Extends HTTP serlet anything else here nothing so that's my URL mapping next do
we need any method here we'll only need one method which is do post I don't need to get we click on finish now you can see once we say that we we get all the extra files we don't need extra things here I like to clip my uh CLI my uh you know subet clean I don't need extra things here do that let me just remove extra thing I'm not a big fan of writing clys after the method name again We have two types of people in the world right one who put curly brackets after
the method and one who puts below below below the method definition okay so once we have done that let's see our web.xml file is it mapping properly or not let me just go to source so mapping is done there's no problem with the mapping uh if it is not done automatically in your machine you can type it manually you can just see this code here and you can type it Manually by pausing the video but since it is done on the mic and it is done on my on my application I'm directly Contin with the
servess side now what will happen when you send all these files so let's say if you're selecting three files now this three files when you click on submit it will call this file upload subet right and on this on this side we have to first check uh how do we check that is it so see when what whenever you send data it send it gets In inside this request object right any any data for that matter so when you said uh when you send data like let's say you want to add two numbers so those
two numbers can I mean you will get those data in this request object so in fact your files will be also coming in this request object now how do you verify that you're getting actual files so we can say if okay we'll do that uh you know those stuff checking stuff later we can also Do that later so let's say if I'm sending multiple files to to fetch those multiple files I will fetch those data and will put that data in a list list of I mean collection of files right so when you say you
sending multiple files will be saying list and the DAT the type of data you'll be getting is something called as file item now file item is a inbuilt class inside your uh common folder Apache Commons it's not working Here uh okay we need to we going to import that manually it should work let me expand the library which we have downloaded it is common file upload so it should be somewhere inside disk if I'm not wrong so if I expand this it's not that here if I expand this one okay you see that file item
so we need to import this package it is not getting imported automatically so we have to do it manually sometime it Happens you know sometime your Eclipse doesn't support what you want to do so you have to do it manually so you have to say or. Apache dot Commons dot file upload and inside this file upload we have uh this file which is file file item right and now you can see there's no error it was not giving any error before and now we'll say our data will be coming in we can go we can
use Any uh any name so we'll say file I mean list of file items so every file will will be called as file item and we have we creating list of that we say new now how do you fetch values so for sure our data is inside request object right so this is the object request object in which you have all your data but we need to fetch it we cannot directly use in that way right we cannot say request it will give you all the data so the the Way you can fetch it is
with the help of object of request or subet file upload so before that we have to create object of subet file upload now the awesome thing is seret file upload also comes from Apache I mean this Commons file Commons uh file upload Library so you can see the importance of this file I mean this jar file okay so if you to do this upload part you need to use common file upload and when to create object of this first we'll say SF for file upload Equal to new subet file upload so we'll say subet U
my my machine is not working so fast today server file upload and we'll say semicolon now this seret file upload in in this we have to pass the object we have to pass the object of disk file item Factory so this file item Factory that's the Constructor we have to pass item Factory yes so we have to pass the object here now once you got the object Of SF okay so once we got the object of suet file upload which is required to pass your request so we'll say SF dot so in this we already
have a method called as pass request can you see that we got pass request and request so what this pass request will do is it will return you so if you see the Declaration of pass request which is inside Ser file upload again how to check the documentation I normally have this habit of checking the documentation of Everything so if you if you want to check the documentation just click on pass request it will give you documentation which accepts the request object it will give you the list of file items and that's what we want
right that's what we wanted and we got that so we got list of file items now once you got that what next now what we'll do is once we got the list of item we can run a for Loop right we can run a for Loop To fetch each item so we'll say file item I hope you're familiar with enhanced for Loop if you're not just go to my channel again and search for enhanced forlo Loop so I'm muing I'm using this multip multiple files and we'll fetch one one file so it will first fetch
the I mean it will first it will fetch the first item and it will be inside item class or item object and let's let's write that item so we can simply say it as item dot now how do you How do you write the item on the server side we'll say item. write and inside this you have to mention the file name now which file name I'm talking about uh I'm talking about the file you have to mention the file object here we'll say file and inside this you have to specify the path where you
want to store the data uh okay good question where I where I want to store data so I want to store the data inside my where's my project this is my project uh that's My project area this is this is where I want to upload the file and how will I get this uh PA so I will go to CMD that's the best way I mean the terminal if you're working with Windows you can go with command prompt since I'm working with the let me just say clear okay so now I'm here in this command
prompt I will say command documents and I'm inside my project which is file or not I have to get into my uh workspace with is telescope and Inside that I have to search for a project which is file upload and if I say LS now you can see we got this three files so this is this is the place where I to upload the file so I will get the working directory this is the path I want to upload so copy go back to the project and simply say this is the path I want to
upload my file path slash that's the path plus I want to specify the name of the file and how can you fetch the name of the file which your Which a user is uploading we'll say item dot get name this should work even if it's not working we'll do the debugging part and I guess that's it if you can do that your file will be uploaded on server so take one one file and put all your files on the server um it should work and once we have done that let's print a message um how
do you print the message now that's weird we'll say response Do let me print the message on server itself and say file uploaded again we'll we'll do that how to upload that how to print that data on the on the client side and we'll I guess it everything is everything should work now if I run this code on the Tomcat server we don't need to run this it's already running State it's already in running state right anyway so I'm running this part and now it is asking asking for the files hope so it will Work
let me select multiple files here let me just upload first file here which is demo. Java upload submit oh it is giving you an error it is not able to find the upload part that's weird we are saying upload oh we should be saying we need to change that in the XML file right because I'm saying upload and in XML the mapping is done with something else let me check what's the mapping here oh mapping is done with file upload I have to say upload Okay so now let's me just let me just restart my
server I will say where's my server it's here this is stor cat restart okay there's always some type of Errors which is coming on the way let's refresh it let me select Java again uh demo. Java and if I say open submit oh now we got one more error it says class not found exception it is not able to find file upload that's Weird it is there it's here and it's here let me just verify once what is the issue again go back to the XML file I guess there's something wrong with XML file here
everything seems good there's nothing wrong let me remove this extra part this. telescope is it the package name which we are working With okay so the problem is we are putting that into a resource folder right it should not be inside resource folder it should be Java folder let me create one more folder here this sometime this is what happens when you work with mavin sometime so we need to move this into Java so it should be inside Java and you can see is giving me some errors now let me import the package for list
okay because we need to also import the Package for list right and it is giving me an error because it it is it may throw an exception is that the issue yes it it it is it is trying to throw an exception let's write everything inside our try catch so we say try and catch say exception e and let me just print the exception here okay now let me just format this Format this thing just to make it look better and formatting is done I think everything is done now let's refresh choose jav file it's
here and says submit and we got one more error we are always getting some sort of erors let me just I think we have to restart the server this not able to find that again let me just restart it once again okay refresh choose the file is the file Now Java submit and you can see we have uh done that there's no errors now because anyway we are not getting any text because we're not printing any stuff let me just go to the console it says uploaded let's verify let me just go to my project
you can see we got demo. Java so we are able to upload a demo. Java file uh let me just upload let me just run this once again by uploading more files and let me remove this demo. Java for now I want to upload More files now delete let me choose more files um we'll choose this one aw. PDF we'll choose code. JSP we'll choose demo. Java demo. JSP and display contact. zip we uploading f file files submit it will take some time of course and you can see we got this five files and that's
how you can upload the files on the server side simple so it's that simple to work with this uh this file upload that's it that's it from this video let me know if You have any queries thanks for subscribing