Saturday, November 17, 2007

JSP Servlets Tutorial

How to create and run a servlet?

You will require to have these Java components before you can run a servlet:
  1. Java Runtime Environment (JRE)
  2. Java software Development Kit (JDK)
  3. Java Enterprise Edition Software Development Kit (Java EE SDK)
These 3 can be downloaded as a complete package, or individually also. To download the complete package (recommended)
  1. Go to: "http://java.sun.com/javaee/downloads/index.jsp " and click on "Download with JDK button"
  2. Accept agreement by clicking on the radio button
  3. Under the header "Windows platfform" click on the file name to download
  4. You can save the setup file to a folder of your choice
  5. Once download completes, click on the file and run it. Proceed with the installation with all default options and settings
  6. Restart the PC
Besides that, you will also need the Apache Tomcat JSP Server, which can be setup as follows:
  1. Go to "http://tomcat.apache.org/"
  2. In the left column, under the "Download" heading, click on the link to the latest version
  3. Once on th downloads page, search for the heading "Binary Distributions"
  4. Click on "Windows Service Installer" to save the file
  5. You can save the setup file in a folder of your choice
  6. Once download completes, click on the file and run it. Proceed with the installation with all default options and settings
  7. Restart the PC
Assuming you have downloaded and setup all the tools, here's what you need to do to get your first servlet running
  1. Write a HelloServlet.java file (see attached)
  2. Save it to the JDK bin directory (In this case: "C:\Program Files\Java\jdk1.5.0_06\bin")
  3. Start Menu> Run > Type "cmd" (without quotes) in the box. MS-DOS Command prompt will open
  4. Change directory to JDK bin using the command (cd C:\Program Files\Java\jdk1.5.0_06\bin )
  5. Compile the HelloServlet.java file using 'javac' (command: javac HelloWorld.java)
  6. Your servlet will be compiled (if no errors are present) and put in the same bin directory
  7. Go to Windows Explorer (My Computer)
  8. Copy HelloServlet.class file FROM "C:\Program Files\Java\jdk1.5.0_06\bin" TO "C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\ROOT\WEB-INF\classes"
  9. Add servlet info (copy from attached file "add_to_web_inf") to the web.xml file (final file with added code attached) located in web-inf
  10. Stop the "Default Server" of Java EE SDK by right-clicking on the "Java EE 5 SDK" icon in system tray (besides the clock at right-bottom)
  11. Manually start the Tomcat server by going to "C:\Program Files\Apache Software Foundation\Tomcat 6.0\bin" and clicking on "tomcat"
  12. Let the DOS window remain open throughout your session. If server starts successfully, it wil show INFO:server startup in 2748ms (example)
  13. Execute the servlet by typing the path "http://localhost:8080/sams/HelloServlet" in your browser
  14. Note: Do not add any extension to the servlet name in the path
---------
HelloServlet.java

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
* The simplest possible servlet.
*/

public class HelloServlet extends HttpServlet {

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/plain");
PrintWriter out = response.getWriter();

out.println("Servlet invoked! ");
out.println(new Date());
}
}

-------------------------

No comments: