Thursday 29 August 2013

How to compile and execute servlets on tomcat server using Note Pad


The servlet example can be created by three ways:

By implementing Servlet interface,
By inheriting GenericServlet class, (or)
By inheriting HttpServlet class

The mostly used approach is by extending HttpServlet because it provides http request specific method such as doGet(), doPost(), doHead() etc.

Here, we are going to use apache tomcat server in this example. The steps are as follows:
Create a directory structure
Create a Servlet
Compile the Servlet
Create a deployment descriptor
Start the server and deploy the project
Access the servlet

Create a Directory Structure:




2. Create a Servlet:

There are three ways to create the servlet.
(a) By implementing the Servlet interface
(b) By inheriting the GenericServlet class
(c) By inheriting the HttpServlet class

The HttpServlet class is widely used to create the servlet because it provides methods to handle http requests such as doGet(), doPost, doHead() etc.

In this example we are going to create a servlet that extends the HttpServlet class. In this example, we are inheriting the HttpServlet class and providing the implementation of the doGet() method. Notice that get request is the default request.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FirstServlet extends HttpServlet
{
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
  {
   response.setContentType("text/html");
   PrintWriter out=response.getWriter();
   String cont="<html><head><title>First Servlet</title>" +
              "</head><body bgcolor=yellow> <h1> Hello"+
             "</h1></body></html>";
   out.println(cont);
  }
}


Compile the servlet

For compiling the Servlet, jar file is required to be loaded. Different Servers provide different jar files:

Jar file                          Name of the Server

1) servlet-api.jar          Apache Tomcat
2) weblogic.jar            Weblogic
3) javaee.jar                Glassfish
4) javaee.jar                JBoss

Two ways to load the jar file

1)set classpath
2)paste the jar file in JRE/lib/ext folder


Put the java file in any folder. After compiling the java file, paste the class file of servlet in WEB-INF/classes directory.

Now we go for first way
1)set classpath

Go to My computer->out folder drive
go to command prompt:
c:\cd \raj\servlets\demo2
now set the class path
D:\raj\servlets\demo2>set classpath=C:\Program Files\Apache Software Foundation\
Tomcat 5.5\common\lib\servlet-api.jar;
compile the servlet program

/*Note: If we ignore to set the class path we will get the errors
*/

//So setting the class path is mandatory:



D:\raj\servlets\demo2>javac HelloServlet.java

D:\raj\servlets\demo2>

Create the deployment descriptor (web.xml file)




How to deploy the servlet in tomcat?

1. Go to tomcat ->webapp
2. Create WEB-INF folder
3.create subfolder "classes", write "web.xml" file
paste the servlet class(i.e FirstServlet.class) in classes folder

<web-app>
<servlet>
 <servlet-name>example</servlet-name>
 <servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>example</servlet-name>
 <url-pattern>/myfirstservlet</url-pattern>
</servlet-mapping>
</web-app>


4.save the file as web.xml, now its time to test our first servlet

Start the Server and Deploy the Project

5. start the tomcat server: go to tomcat in program files
bin->start .bat file or  double click on tomcat5.exe file


*/



How to Access the Servlet

Go to any browser

type in the address bar: localhost:8080/FirstServlet/myfirstservlet (press enter)
or

Type localhost:8080 in the address bar
(8080 is port number if you change the port number , type changed port number)

Then it display the Tomcat home like below.

Once you click on Manager App, one dialog box will display

User Name: admin
Password  :  xxxx

While we are installing Tomcat server, we can set the user name and password

Now it will display the all the applications which we had deployed.
click on Our Folder.


We got 404 error due to we did not write welcome list in web.xml, so we need to type
localhost:8080/FirstServlet/myfirstservlet (press enter)




*/

No comments:

Post a Comment