1
votes

I developed a WebApplication using Netbeans and Glassfish server. I wanted to deploy that on the Tomcat server.But I was not able to run the servlet on Tomcat. I did the following

  1. Created a folder "fti" inside ROOT directory of Tomcat
  2. Placed index.jsp inside fti directory which I was able to access through my browser
  3. I created WEB-INF folder inside "fti" folder and put web.xml file inside it.
  4. I created classes folder inside "WEB-INF" folder and put the compiled java file test.class inside it.
  5. I tried calling the servlet through the browser by trying to access /fti/test and got the error as "The requested resource (/fti/test) is not available."

I was able to run the same servlet using the same web.xml file with Netbeans and glassfish server

My test.java file:

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class test extends HttpServlet {


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {

        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet test</title>");            
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet test </h1>");
        out.println("</body>");
        out.println("</html>");
    }
}


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}


@Override
public String getServletInfo() {
    return "Short description";
}

}

My web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

<servlet>
    <servlet-name>test</servlet-name>
    <servlet-class>test</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>test</servlet-name>
    <url-pattern>/test</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>

3
in step 4, are you placing manually .class files into classes folder ?Abhishek Nayak
are you sure your code compiles successfully without this error: try without catch, finally or resource declarationsAbhishek Nayak
Why on earth aren't you using an IDE? It will automate many of these steps for you and ensure everything is in the right place.user207421

3 Answers

2
votes

You're confusing the "ROOT" webapp with a top-level document directory. fti would need to be a sibling of ROOT to behave as you're expecting, not a child.

1
votes

A servlet, or any REST request path, is not simply a remote filesystem. In your web.xml you've told your container that the servlet's name is test and that its path is test/, so try calling test instead of fti/test.

0
votes

The folder fti should be under the folder webapps that you can find in the same folder where Tomcat is installed. In addition the class test.class should be in webapps/fti/WEB_INF/classes. You should be able to access it via /fti/test