You will require to have these Java components before you can run a servlet:
- Java Runtime Environment (JRE)
- Java software Development Kit (JDK)
- Java Enterprise Edition Software Development Kit (Java EE SDK)
- Go to: "http://java.sun.com/javaee
/downloads/index.jsp " and click on "Download with JDK button" - Accept agreement by clicking on the radio button
- Under the header "Windows platfform" click on the file name to download
- You can save the setup file to a folder of your choice
- Once download completes, click on the file and run it. Proceed with the installation with all default options and settings
- Restart the PC
- Go to "http://tomcat.apache.org/"
- In the left column, under the "Download" heading, click on the link to the latest version
- Once on th downloads page, search for the heading "Binary Distributions"
- Click on "Windows Service Installer" to save the file
- You can save the setup file in a folder of your choice
- Once download completes, click on the file and run it. Proceed with the installation with all default options and settings
- Restart the PC
- Write a HelloServlet.java file (see attached)
- Save it to the JDK bin directory (In this case: "C:\Program Files\Java\jdk1.5.0_06\bin")
- Start Menu> Run > Type "cmd" (without quotes) in the box. MS-DOS Command prompt will open
- Change directory to JDK bin using the command (cd C:\Program Files\Java\jdk1.5.0_06\bin )
- Compile the HelloServlet.java file using 'javac' (command: javac HelloWorld.java)
- Your servlet will be compiled (if no errors are present) and put in the same bin directory
- Go to Windows Explorer (My Computer)
- 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" - 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
- 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)
- Manually start the Tomcat server by going to "C:\Program Files\Apache Software Foundation\Tomcat 6.0\bin" and clicking on "tomcat"
- Let the DOS window remain open throughout your session. If server starts successfully, it wil show INFO:server startup in 2748ms (example)
- Execute the servlet by typing the path "http://localhost:8080/sams
/HelloServlet " in your browser - 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:
Post a Comment