Tuesday, September 8, 2009

Chapter 09: Beginning Servlets

What: Servlet

A Servlet is a Java Program that extends HttpServlet class. A servlet is basically responsible for processing request & sending response to the client. Servlet helps developing a more robust, secure web application.

Like: Servlet

These are the other scripting languages that are like Java Servlet. If you are learning a concept, it’s good to know the counterparts.
  1. CGI
  2. PHP
  3. ASP.NET

The King: web.xml

An XML file called "web.xml" contains information about all the servlets written in a java application. This is the most important file in a JEE web application and it is mandatory. Inside the project folder/directory, a folder called “WEB-INF” should be created. Inside this folder, the web.xml file needs to be placed.

Example:

<?xml version="1.0"?>
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Demo Application</display-name>
<description>
For Learning purposes.
</description>
<servlet>
<servlet-name>MyFirstServlet</servlet-name>
<servlet-class>DemoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyFirstServlet</servlet-name>
<url-pattern>/HitTheServlet</url-pattern>
</servlet-mapping>
</web-app>

Servlet Vs JSP

Both Servlet and JSP are basically same. A JSP is actually converted to a servlet before execution. Whenever a JSP file is executed, it is first compiled into a Java Servlet & then executed by the server.

My First Servlet

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet
{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}

Download Video Source: High-Quality : 33 MB

Click below arrow to download….
Watch it !

8 comments:

  1. A Servlet is a Java Program that implements the Servlet interface (u said in the first line) .... but u never implemented anything... u 've just extended HttpServlet class...am i getting anything wrong?

    ReplyDelete
  2. You are right Rajiv. Servlet extends the HttpServlet abstract class and doesn't implement an interface. Thanks for correcting me.

    ReplyDelete
  3. Chapter 9 is a 30 minute video and YouTube only has the first 10 minutes. You can freely download the complete source file. Check that video for download link.
    URL:
    http://rapidshare.com/files/277802513/Tamil_JEE_Tutorial_Chapter_09_Beginning_Servlets.wmv

    ReplyDelete
  4. i get ERROR as file missing when i try to download the Servlet video..

    ReplyDelete
  5. @Above: I checked them and the links are still alive and working. What is the error you are getting?

    ReplyDelete
  6. "The requested resource(demo) is not available". This is the error I have got, when I am clicking my aplication "demo" . Can you please tell me what might be the error I have made?

    ReplyDelete
  7. can u please get me the code for the java program which connects to a database??? i will be more thankful if i get it with explanation....

    ReplyDelete
    Replies
    1. That is easily possible. Can you search on google for "JDBC Examples". You will get a lot of working code.
      I actually wanted to add a chapter on JDBC under the J2EE section, but I am yet to begin working on it.

      Delete

Our Google Group

Our Facebook Group