JSP Tutorial #26 – HelloWorld Servlet Overview



JSP Tutorial #26 – HelloWorld Servlet Overview

JSP Tutorial #26 - HelloWorld Servlet Overview

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

Hey. In this video we’re going to learn about Servlets in Java. What exactly is a Servlet? Well a Servlet is basically a Java class that’s processed on the server. This Java class can generate HTML that’s returned to the browser. You can use Servlets to read HTML form data. You can make use of cookies and sessions. At a very high level, Servlets are similar in functionality to JSPs. I’ll talk more about that later. Just for now let’s kind of focus on how to build a Servlet and look at some of the coding for it.

With Servlets you’re going to process something on the server. You have your browser. You have your Servlet. What the browser is going to do is basically send a request over to the server. Your Java Servlet will execute. When the Servlet is executing, it can generate some HTML content. That HTML content will actually be sent back to the browser as a response. Again, we have HTML content that’s built on the fly, by a Java Servlet. The Java Servlet is actually a Java class that runs when we make a call to it.

All right. Now you’re probably wondering, okay show me the code. We can go ahead and look at the code. Here at the top we start off with our Servlet. The first thing you have here is an @webServlet. This is a Java annotation and we basically give a path for the Servlet. @webServlet/HelloWorldServlet. That’s the path for access in this Servlet.

Then we create our Servlet. Like I said earlier, the Servlet is simply a Java class. We create public class Hello World Servlet, extends HttpServlet.

Whenever we write a Servlet we can override either the doGet method or the doPost method. For this Hello World example, we’ll override the doGet method. The doGet method, it takes two parameters. They pass in a request object and a response object. When we make use of our Servlet code inside of this doGet method, we have three main steps.

The first step is setting the content type. Here we basically tell the browser, “Hey. I’m going to send you back some data and this is the type of data I’m sending back.” In this example I’ll say response.setContentType text/html. Then the next step is getting the print writer. This is a special object that we can get a handle on to actually send data back to the browser. Here I say PrintWriter out equals response.getWriter. Then, moving ahead to step three, we basically just generate our HTML content. This is very similar to like with JSPs. You simply use out.print line and you just give your HTML content. Here I say out.print line. I give the HTML body. Then I say out.print line hello world. Then I do another print line with HR. Then finally here I say out.print line, time on the server is and I can drop in new java.utl.date. Then I can do a slash body, slash HTML.

Basically what this will do is on the fly, it’s going to generate an HTML page very similar to the screenshot we have here, and it’ll give us the time on the server. It’s the basic idea of a Servlet. Again, similar to JSPs in that we generate HTML on the fly, but a tad bit different in that we actually create a Java class first. Then we do an out.print line on all of the HTML that we’re going to send back to the browser.

All right so this is a lot of good stuff. The Hello World Servlet in a nutshell. In the next video, we’re actually going to try it out and move into clips and start working with it. All right. I’ll see you then.

End of transcript for JSP tutorial.

Comments are closed.