A Simple Jsp Servlet Login Example in Tomcat and Eclipse

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we will create a login example using JSP servlet in our Eclipse so let's start file new dynamic web project project named simple login and target runtime Apache Tomcat which we had already configured so this eclipse is going to use Tomcat 8 do next then next and generate web dot XML deployment descriptor so that automatically the web dot XML will get generated finis ok so a project got created in the left hand side simple login if we click on this arrow symbol we get to see all this folders Java resources is the place where we will put the servlets or the java classes if we want to put jsps we will put inside the web content here in the web content in the web-inf folder we already have the web dot xml that is generated by our Eclipse so let's create a JSP page so whenever we create a JSP page we should create the JSP in the web content this is just like put it into the simple login folder the main folder so we'll create a JSP page within the web content right click create a new JSP name of the JSP is login dot JSP finish so a login dot JSP page is created here these are the default coding that eclipses provided us whatever we want to do we'll do it inside the body opening and closing the syntax is just like that of HTML only so here we'll create a form so let's do it form opening and closing form opening closing here method equals to post so method can be get as well as post xn equals to where you want to go when somebody hits the submit of this form so maybe from this JSP we want to go to another servlet so let's do log in check so this is a servlet this is the URL mapping of a servlet log in check so we have to create a servlet whose URL pattern will be log in check so if you want to go to another JSP on hitting the submit rather than giving the URL pattern you give log in check dot JSP but we want to go from a JSP to a servlet on hitting the summit so now here we have to create the table to structure things properly so let's create a table table opening/closing and we know we can have rows and columns TR is four rows so TR opening/closing so every opening should have a closing inside the row we can have columns TD opening TD closing so if you want two columns within one each of the rows then we can repeat the TD like this so TR is for the row and TDS are for the columns cells so here like username this is the label what will appear and the second TD opening closing let's have a textbox in per type equals text and the name is important name of the textbox is important let's give a name like you name you may give anything you an AME here so this is for the first row similarly we can have another row for the password let's do that so here password these are for the labels what will appear input type equals to password so password rather than text so that the alphabets will not appear when you type star star stars will should appear for security reasons okay name equals to password then we can have a submit button for that let's go to the second cell and create an input type equal to submit here submit button and the other the name equals two we can have value equals to what will appear on that button like maybe login login will appear on that button okay so we have created a JSP page which has two text boxes and a form like if we hit the submit it should go to this action that is logging check and for security reasons we have made method equals to post okay so let's build our servlet then okay let's structure this properly control a for selecting everything control shift F format okay so this is a shortcut in eclipse control safety F for formatting after selecting everything control shift F so now let's create a servlet named login check right click new servlet name of the servlet log in check login check and we can use the superclass HTTP servlet to create the servlet then do next and here is the URL pattern so it's also slash log in check by default like as we had mentioned in the JSP page acts on equals to log in check so it will go to this servlet on hitting the summit next and finish so servlet will get created for us here is our servlet login checks last log in check here is the annotation at the rate of whips of let slash log in check means there is another alternative like if you want to have a URL pattern you can go to the web dot XML and have the entry of other servlet as well as the servlet mapping but in recent versions so you can do like this you can have a notation for the URL pattern so our servlet is ready extending the HTTP servlet this is our solet so control will come to the do post method because we had done method equals to post so it will come here to this servlet to post method so here control will come to this do post method so whatever we want to do we can do it here inside the do post method like we can check with the database what we'll do inside the do post method will check whether the username or password is valid or not and accordingly we may go to this member page or we may go to an error page or whatever so let try to retrieve that textboxes values here the login dot JSP text boxes values here how do we retrieve that so when you have hit the summit on the JSP page the request object is coming and that request object is holding the parameters the textboxes values so we can get that from this request how do we get that request dot getparameter and here we will specify the textboxes name like you name you name we add mentioned on the textbox name so we'll write you name get parameter unit so this will return in the form of a string which will store in some variable so string you name equals to request dot getparameter you name similarly for the password we can repeat so request dot getparameter for password so whatever the user types on the password text box will also come here and you can store it and a variable password now next so we can check whether this username and password matches some random username and password if you want to check with the database using JDBC you can do that but now we'll check with some random username password like if you name dot equals we have the equals function to check the equality equals Java and and password dot equals one two three four so we want to go to the member page or else we'll go to the error page so so how do we go to the member page or the error page so there are two procedures One is using send redirect and one is using requestdispatcher so we'll see send redirect technique here so let's create two JSP pages in our web content how do we do that we can right click new JSP member dot JSP one p and again another page called error dot JSP okay in the member page just let us write down welcome you are logged in and the error page unsuccessful login let's save let's go to the log in check so here if it is valid the username in the password we want to redirect to the member page so as I said we have two techniques one is central direct one is request dispatcher so now we will be using send redirect here so how do we do that we have response dot send redirect to the page where we want to go that is member dot JSP similarly here dot send redirect to the error dot JSP save everything now it is ready now let's see so how do we deploy right click on the project run as run on server the Tomcat 8 as it is already configured is showing up here next and finish its deploying into the server it will try to find the default page that is index dot JSP and it will show up a four zero four error immediately right because it did find that index dot which we are not created no problem so we'll go to the login page directly login dot JSP so localhost:8080 localhost is the IP address of the same computer because the server is installed in the same machine we are hitting localhost else would have written something else the IP address of the server system eight zero it is already the port number to which tomcat listens to simple login is the project name login dot JSP is our JSP page here the enter we see our login page here and as we had mentioned Java and one two three four and hit the login and look it has successfully logged in and gone to the member page but if you want to try something else JSP and one two three four or something else hit the login it goes to the error page so in this example we have created a JSP page a servlet and member dot JSP and error dot JSP if it is successful it goes to the member page or else in the error page thanks for watching
Info
Channel: Chinmay Patel
Views: 223,701
Rating: undefined out of 5
Keywords: login jsp example, login jsp servlet example, servlet login example, simple login jsp, simple servlet login, jsp login tutorial, servlet login tutorial
Id: D0OfTZuIDcs
Channel Id: undefined
Length: 10min 10sec (610 seconds)
Published: Sun Jan 01 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.