1

I was working on a java code for servlet programming to implement an application which provides a REST API with endpoints for searching, creating and deleting “server” objects. The server object is passed as a json-encoded message body. Example:

{ “name”: ”Centos”, “id”: “000”, “language”:”java”, “framework”:”spark” }

My code isn't throwing any error so I've no idea where the error is. Generally the error for 404 lies in xml file as far as I've read on other's stackoverflow issues, but I've checked my xml too. Here is my xml file details: (I've removed the 2 head files (xml-version and web-app) for now)

  <display-name>KaiburrTask1</display-name>
  <servlet>
    <servlet-name>Server Handling</servlet-name>
    <servlet-class>com.kaiburr.task1.ServerHandling</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Server Handling</servlet-name>
    <url-pattern>/serv/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

Full code is on: https://github.com/Shashank-Shukla/KaiburrTask/tree/master/KaiburrTask1

@ApplicationPath("/")
@Path("")
public class ServerHandling {
    private static String name,language,framework;
    private static int id;

    @GET @Path("/Servin") @Produces("application/json")
    public String display(String name, int id, String languauge, String framework) {
        String dispStyle="\n{\name: \"%s\",\nid: \"%s\",\nlanguage: \"%s\",\nframework: \"%s\"\n}";
        return String.format(dispStyle, name,id,language,framework);
    }

    @PUT @Path("/Enterr") @Produces("application/json")
    public void updateServer(@QueryParam("name") String name, @QueryParam("id") int id, @QueryParam("language") String language, @QueryParam("framework") String framework) {
        ServerHandling.name=name;
        ServerHandling.id=id;
        ServerHandling.language=language;
        ServerHandling.framework=framework;
        display(name,id,language,framework);
    }

    @POST @Path("/dispServerName") @Produces("text/plain")
    public String dispName() {
        return name;
    }
    @POST @Path("/dispServerID") @Produces("text/plain")
    public int dispID() {
        return id;
    }
    @POST @Path("/dispServerLang") @Produces("text/plain")
    public String dispLang() {
        return language;
    }
    @POST @Path("/dispServerFrame") @Produces("text/plain")
    public String dispFrame() {
        return framework;
    }

    public String getName() {
        return name;
    }
    public int getID() {
        return id;
    }
    public String getLang() {
        return language;
    }
    public String getFrame() {
        return framework;
    }
}

The error is:

Error Response

Even using:

curl -X PUT "localhost:8082/KaiburrTask1/Enterr"

it throws this error:

HTTP error 404 not found.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • My index.html is working properly and is getting loaded onto the server but other links aren't loading. – Shashank Shukla Feb 14 '20 at 11:01
  • You are mapping the servlet url as `/serv/*`, but you are not using that url in the browser. [Checkout this post on servlet mapping](https://stackoverflow.com/questions/15385596/how-are-servlet-url-mappings-in-web-xml-used) – R Wri Feb 14 '20 at 15:59
  • Does this answer your question? [How are Servlet url mappings in web.xml used?](https://stackoverflow.com/questions/15385596/how-are-servlet-url-mappings-in-web-xml-used) – R Wri Feb 14 '20 at 16:00
  • @RWri : I'm sorry that I didn't include the screenshot for those but I did try these in the url too. `http://localhost:8082/KaiburrTask1/serv/Servin` **OR** `http://localhost:8082/KaiburrTask1/serv/Enterr/` but unfortunately it turns out error 404 everytime. Is it something to do with directory issue with the source file? I mean error 404 is a client-side unreachable issue right, and locally it means that the server created cannot reach the source file right!! – Shashank Shukla Feb 15 '20 at 18:32

0 Answers0