How to Execute Java Servlet

      No Comments on How to Execute Java Servlet

How to Execute Java Servlet

To Execute Java Servlet Program you need following software:

1)      JDK  (Download : http://www.oracle.com/technetwork/java/javase/downloads/index.html )

2)      Tomcat Apache  (Download : https://drive.google.com/file/d/0BxD5bHJ5-iirZHQ5Mzc5SG95dkk/view?usp=sharing )

3)      Editor to Write Program : Notepad++(Download :  http://notepad-plus-plus.org/ )

 

Environment Variable Setup Required

  • PATH to JDK/Bin Folder :
    • My Computer Right Click -> Property
    • Advance System Settings
    • Click Advance Tab
    • Click Environment Variables
    • Edit Path Variable from “System Variables” and Add Path to JDK Bin Folder.

 EnvironmentVariable1

  • CLASSPATH to TomcatApache\lib\servlet-api.jar and TomcatApache\lib\jsp-api.jar
    • My Computer Right Click -> Property
    • Advance System Settings
    • Click Advance Tab
    • Click Environment Variables
    • Add CLASSPATH Variable from “System Variables” and Add Path to Tomcat Apache Lib Folder and jar file
    • Sample Values:
    • C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\jsp-api.jar;.

How to Execute Java Servlet

 

Step 1 : Install JDK

Step 2 : Install/Extract Tomcat Apache

Step 3 : Setup Environment Variable.

Step 4 : Compile and Execute your First Servlet Program.

Hello Servlet Program

FilePath : C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\example01\WEB-INF\classes

FileName: ex01.java

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

public class ex01 extends HttpServlet {

     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          PrintWriter out = response.getWriter();
          out.write("First Servlet");
     }
}

FilePath : C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\example01\WEB-INF\

FileName: web.xml

 

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<display-name>Example 01 </display-name>
<description>  This is Hello World Servlet </description>
<servlet>
<servlet-name>ex01</servlet-name>
<servlet-class>ex01</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ex01</servlet-name>
<url-pattern>/ex01</url-pattern>
</servlet-mapping>
</web-app>

FilePath : C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\example01\

FileName: index.jsp

<%

     response.sendRedirect(getServletContext().getContextPath()+"/ex01");

%>

Leave a Reply