RSS

Monthly Archives: January 2014

Servlet inside package

C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ROOT\WEB-INF\classes\Second.java

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

public class Second extends HttpServlet
 {

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

compile it :
C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ROOT\WEB-INF\classes> javac Second.java -d .
So it will generate the p1 folder and put class file in that folder

Do the mapping in web.xml

<servlet>
        <servlet-name>Second</servlet-name>
        <servlet-class>p1.Second</servlet-class>
    </servlet>
	 <servlet-mapping>
        <servlet-name>Second</servlet-name>
        <url-pattern>/Second</url-pattern>
    </servlet-mapping>

Run it like :
localhost:8080/Second

 
Leave a comment

Posted by on January 28, 2014 in Example

 

steps to set up the environment to run the servlet

1) First Download the JDK latest version from
http://www.oracle.com

2) Second Download the tomcat version 7.0 or latest version from
http://tomcat.apache.org

here i am using jdk7.0 and tomcat 7.0.

Once its downloaded set the environment Variables :
Right click on My Computer -> properties->Advance System setting->Advanced Tab->Environment variables.

3) set “CLASSPATH=.;C:\Program Files\Java\jdk1.6.0_26;C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar”
set “CATALINA_HOME=C:\Program Files\Apache Software Foundation\Tomcat 7.0”
set “JRE_HOME=C:\Program Files\Java\jdk1.7.0_11”
set “JAVA_HOME=C:\Program Files\Java\jdk1.7.0_11”

After setting the ENVIRONMENT VARIABLES:
Start the tomcat server :
“C:\Program Files\Apache Software Foundation\Tomcat 7.0\bin\startup.bat”

4) open the browser and type localhost:8080, if tomcat server is properly started then it display the HOME PAGE like below :
tomcat

5) Write a servlet Source code.
First.java

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

public class First extends HttpServlet
 {

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

6) compile it using javac First.java
7) copy the class file into
C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ROOT\WEB-INF\classes
if classes directory is not available in WEB-INF then create directory named “classes” and put your .class file in that directory.
8) do the mapping of the servlet
C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ROOT\WEB-INF\web.xml

       <servlet>
        <servlet-name>First</servlet-name>
        <servlet-class>First</servlet-class>
    </servlet>
	 <servlet-mapping>
        <servlet-name>First</servlet-name>
        <url-pattern>/First</url-pattern>
    </servlet-mapping>
    

9) now run your Servlet:
localhost:8080/First

 
Leave a comment

Posted by on January 28, 2014 in Example