Ashish Agarwal Ashish Agarwal - 3 months ago 26
Java Question

Simple Test Servlet not working, when hosted on GoDaddy server,Why?

I was trying to test servlet by hosting it on shared hosting by GoDaddy server. I am compiling the code using Java SDK 5_0_22(The SDK version of GoDaddy server). I tested same code,folder structure, on LOCAL Tomcat5.0.27(The server verision of Godaddy).

But I am able to run JSP file; this proves that my account is JAVA enabled.Please help.
P.S. Every time am getting 404 error.index.jsp is working fine.

FolderStructure:-

WebApplication--|
                          
|-META-INF
                          
|-WEB-INF---|
                                               |-classes--|--test.class
                                               |-web.xml

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


test.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/


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;

/**
*
* @author SAM
*/
public class test extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
// TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet test</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet test at " + request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");

} finally {
out.close();
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
public String getServletInfo() {
return "Short description";
}// </editor-fold>

}

Answer

Running packageless servlets doesn't work in all circumstances. Classes in the default package are by specification invisible/unimportable to classes in a real package. As far as I know, only certain Tomcat + JVM + Windows version combinations allows for that. But you should never rely on that environmental dependency. You should always put your servlets (preferably also all other Java classes!) in a package.

package com.example;

// ...

public class TestServlet extends HttpServlet {

    // ...

}

The compiled class should end up in /WEB-INF/classes/com/example/TestServlet.class and the servlet mapping should be updated as follows:

<servlet>
    <servlet-name>testServlet</servlet-name>
    <servlet-class>com.example.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>testServlet</servlet-name>
    <url-pattern>/test</url-pattern>
</servlet-mapping>

Also note that I fixed your classname to start with uppercase conform Java naming conventions.


Unrelated to the concrete problem, I know that you're just beginning with servlets and that your servlet is just a dummy test example. I would only stress that emitting HTML inside a servlet that way is considered a bad practice. JSPs should be used for that. See also our servlet wiki page.