Summary
Keywords
Full Transcript
FULL COURSE: JSP, Servlets and JDBC (80+ videos) http://www.luv2code.com/jsp (SPECIAL DISCOUNT) ---- This JSP tutorial series will help you quickly get up to speed with JSP. Download Tutorial Source Code: http://www.luv2code.com/downloads/youtube-jsp/jsp-tutorial-source-code-lite.zip ---- View more videos on the playlist: JSP Tutorial: https://goo.gl/fFVMrj Closed-Captioning and English subtitles available for this JSP Tutorial. ---- Follow luv2code for more JSP tutorial: Website: http://www.luv2code.com YouTube: http://goo.gl/EV6Kwv Twitter: http://goo.gl/ALMzLG Facebook: http://goo.gl/8pDRdA --- If you liked my JSP tutorial, then join my mailing list: Get exclusive access to new Java tutorials. - http://www.luv2code.com/joinlist --- Questions or problems about this JSP tutorial? Post them in the comments section below. --- Want to suggest a video for my JSP tutorial? Leave a comment below. I'm always looking for new video ideas. Let me know what video you'd like for me to create. --- Premium JSP Course Need More Details on JSP? - See my Premium JSP and Servlets course (80+ videos) - http://www.luv2code.com/jsp --- JSP Tutorial Transcript All right, let's go ahead and move into the clips. We're going to get our hands dirty by creating a "Helloworld" Servlet. The first thing I want to do is create a new project. I'll say, "file," "new," and "dynamic web project." For the name of the project I'll actually call it "Servlet Demo." Then I'll keep all the other defaults here: Apache Tomcat eight, version 3.1, everything else the same. Then I'll go ahead and click on "next." Here we'll just go ahead and keep the defaults here for the default outlet server. I hit "next," and then for the context root and content directory I'll keep the defaults, and also I'll check the box here for "generate web.xml." The context root is the actual name of our application once we deploy the Servlet demo. I'll go ahead and hit "finish." Now we should have a new project here out to the left called "Servlet demo." We have a web content directory and it's empty right now. The first thing I want to do here is create a new Servlet, so I just right click on a project, I say "new," and I move down to "Servlet." For this Servlet I give a job a package name, so I just call it "com.luv2code.servletdemo" and that's just the name of the package for grouping these classes together. The actual class name, I'll call it, "Helloworld Servlet." Once I'm happy with that I go ahead and click on the "next" button. Here they have the Servlet name, I'll keep it as is. I'll accept all the other defaults. I'll move ahead and click "next." Here they can tell us which methods they want to create for us on the fly, so right now we'll go ahead and accept the defaults. They're going to create a "do get" and a "do post" method. We'll go ahead and keep all the defaults here. We'll just hit "finish." Great, so this looks similar to the code that we had in the PowerPoint slides, but Eclipse generated a lot of this for us. They created a new package, and they also have the new Servlet.java files so this is really good, this is a good jump start for our Servlet development. Let's go ahead and dig into the Servlet and just kind of see how it works here. First on line 14 we simply have our Helloworld Servlet, so public class "HelloWorldServlet, extends HttpServlet," and we also have that declaration there. Up on line 13 we have our "@webServlet." This is an annotation that gives an actual path, or the URL for us to actually access this Servlet. Let's go ahead and look at some of the code. There's a lot of generated code. What I want to do here is take a look at the "do get" method. This is the one method that we override, where we're going to actually provide our response so when the browser makes a call to our Servlet, then it'll actually access or call the "do get" method. This "do get" method has a request and a response. The interior there I'm going to remove, that was all the auto-generated code and I want to get my own custom code here. Let's go ahead and write some code. First off I always like to put out the comments as far as what I need to do here. For step one I need to actually set the content type. Then for step two I need to get a handle to our print writer that I'll use for sending back data. Step three is actually generating the html code on the fly. Those are the three main steps as far as what we need to do inside of this "do get" method. Let me just get some white space here just so we can kind of have some room to work with. For setting the content type we have two parameters coming in. We have the request object coming in, and it has all of the form data coming in from the browser, and then the response object. That's our handle that goes back to the browser. [Snip] for complete transcript of JSP tutorial, select "More ... Transcript"
