Servlet works on Eclipse but not on Tomcat server "The requested resource is not available."

186 views Asked by At

I created a servlet that worked on Eclipse but gives me

"The requested resource is not available."

When accessed on my tomcat server at http://cs3.calstatela.edu:8080/cs3220stu48/Labs/RequestSummary yet works when on Eclipse http://localhost:8080/cs3220stu48/Labs/RequestSummary Here is my Servlet code

package CS3220;


 import java.io.IOException;
 import java.io.PrintWriter;
 import javax.servlet.ServletException;
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.util.Date;
 import java.util.Enumeration;
 import java.util.Map;


@WebServlet("/Labs/RequestSummary")
public class RequestSummary extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    Date date = new Date();
    out.println("<!DOCTYPE html>");
    out.println("<html lang =\"en\">");
    out.println("   <head>");
    out.println("       <link rel= \"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\" integrity=\"sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u\" crossorigin=\"anonymous\">");
    out.println("       <meta charset = \"UTF-8\">");
    out.println("       <title>Request Summary</title>");
    out.println("   </head>");
    out.println("   <body>");
    out.println(    "<div class=\"container\">");
    out.println("           <div class =\"jumbotron\">");
    out.println("               <h1>Request Summaray</h1>");
    out.println("           <p>");
    out.println(                "The following " + "<code>" + request.getMethod() + "</code>" + " request was sent on " + "<code>" + date.toString() + "</code>");
    out.println("           </p>");
    out.println("           </div>");
    out.println(            "<h3>Request Parameters</h3>");
    out.println(                "<table class = \"table table-bordered table-striped table-hover\">");
    out.println(                    "<thead>");
    out.println(                        "<tr>");
    out.println(                            "<td>Parameter Name</td>");
    out.println(                            "<td>Parameter Value</td>");
    out.println(                        "</tr>");
    out.println(                    "</thead>");
    Map<String, String[]> Map=request.getParameterMap();
    for(String key:Map.keySet()){
        String[] Parameter=(String[])Map.get(key);
            out.println("<tr>");
            out.println("<td>" + key + "</td>");
            out.println( "<td>");
        for(String value:Parameter){
            out.println("<span class=\"label label-info\">"+ value + "</span>");
        }
            out.println("</td>");
            out.println("</tr>");
    }
    out.println(                "</table>");
    out.println(            "<h3>Header Information</h3>");
    out.println(                "<table class = \"table table-bordered table-striped table-hover\">");
    out.println(                    "<thead>");
    out.println(                        "<tr>");
    out.println(                            "<td>Header Name</td>");
    out.println(                            "<td>Header Value</td>");
    out.println(                        "</tr>");
    out.println(                    "</thead>");
        Enumeration<String> e = request.getHeaderNames();

        while(e.hasMoreElements()) {
            String name = e.nextElement();
            String value = request.getHeader(name);
            out.println("<tr>");
            out.println("<td>" + name + "</td>");
            out.println("<td>"+ value + "</td>");
            out.println("</tr>");
            }

    out.println(                "</table>");
    out.println("       </div>");
    out.println("   </body>");
    out.println("</html>");
}



protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    doGet(request, response);
}

}

And my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" 
version="3.0">
  <display-name>cs3220stu48</display-name>
  <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  </web-app>

I was told by some classmates that I shouldn't have to modify my xml file and would just need to tweak my url pattern, however I'm not too sure about what to do with it. I'm also sure I uploaded it to the right directories. The servlet into the web-inf/classes folder and the xml into web-inf. This is my first time working with servlets and would appreciate some help and also some advice for the future.

EDIT: I've also realised along side my code, the place i put my java file could be the problem. I put web.xml in WEB-INF and RequestSummary.java in WEB-INF/classes

EDIT 2: Also it seems that my build folder is empty on Eclipse, I believe there should be a classes folder in there, I checked my build path and it does lead to builds/classes as default

1

There are 1 answers

0
STRAN On

I was uploading the class file to the classes folder but what I should have done was upload the class file as well as the folder found in build/classes. I panicked and assumed it was my code but was a directory issue.