0

I am trying to demo simple Ajax/Servlet request.For this i have created a new Dynamic web project in eclipse and added a simple Servlet to take the request and present the same back to UI. I have included my Servlet details in web.xml. I am using Tomcat as my server. No Build scripts yet(i felt not needed at this point)

Servlet Code:

response.setContentType("text/html");
    PrintWriter out =response.getWriter();
    String txt = request.getQueryString();
    out.println(txt);

Js Code:

$(function(){
        $.get('/jirarequest','OK',function(op){
            $('#maindiv').appendTo(op);
        });

    });

Html Code:

<?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>Jira-Synchronization</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>

<servlet>
    <servlet-name>JiraSyncServlet</servlet-name>
    <servlet-class>src.JiraSyncServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>JiraSyncServlet</servlet-name>
    <url-pattern>/jirarequest/*</url-pattern>
</servlet-mapping>

My Project structure:enter image description here

My HTML:

enter image description here

I am getting a error in Fire bug saying

"NetworkError: 404 Not Found - http://localhost:8080/jirarequest?OK"

The URL i am using in browser is http://localhost:8080/Jira-Synchronization/index.html. i have double checked all kind of typo. I debugged by putting breaking point but the debugger never hit my Servelt. I am not sure what is wrong? URL is wrong or wiring is wrong or the way it set up something is missing?

Lee Meador
  • 12,829
  • 2
  • 36
  • 42
pushya
  • 4,338
  • 10
  • 45
  • 54
  • Try to hit `http://localhost:8080/jirarequest/whatever?OK` (You can test from your browser without using the javascript.) – Lee Meador Aug 06 '13 at 17:09
  • I tried it is throwing "HTTP Status 500 - Error instantiating servlet class src.JiraSyncServlet" – pushya Aug 06 '13 at 17:12
  • That means the URL is correct with something after `jirarequest/`. Now on to problem 2. There should be a log somewhere on the server showing an exception and stack trace. The particular exception should help you. Error 500 usually means an unexpected exception occurred. – Lee Meador Aug 06 '13 at 17:13
  • For the console log i see this "Allocate exception for servlet JiraSyncServlet java.lang.ClassNotFoundException: src.JiraSyncServlet". I think the reason is Tomcat is not able to see the servlet. I see nothing is deployed to Tomcat Webapps directory. I have no clue why tomcat can;t see this class. – pushya Aug 06 '13 at 17:20
  • 1
    Is the package of your JiraSyncServlet really src? – Roger Lindsjö Aug 06 '13 at 17:21
  • I haven't included any package to the class. i just gave the directory structure in the web.xml. is this incorrect? what should i put in – pushya Aug 06 '13 at 17:23
  • When i used JiraSyncServlet in the web.xml and typed URLhttp://localhost:8080/Jira-Synchronization/jirarequest?OK it worked. But with this http://localhost:8080/Jira-Synchronization/index.html I still see the 404 error. Why is that – pushya Aug 06 '13 at 17:30
  • 404 means Tomcat is not exposing anything at that URL path. 500 means Tomcat has something there but got a Java exception using it. Leave off the `src.` since there is no package. If you look in your jar/war, you will see the servlet class file in the root. (Use a zip file viewer to look inside.) – Lee Meador Aug 06 '13 at 17:41

1 Answers1

1

Some info:

  • The 404 error means Tomcat doesn't have any file or servlet or anything exposed for serving at that URL path.

  • The 500 error means Tomcat has a servlet (or jsp) exposed at that URL path and an unexpected exception was generated while trying to run the java code.

Collection of things we know need to be changed:

  $.get('/jirarequest/whatever','OK',function(op){

and

<servlet>
    <servlet-name>JiraSyncServlet</servlet-name>
    <servlet-class>JiraSyncServlet</servlet-class>
</servlet>

Plus you can test in the browser by typing this URL which should return OK:

http://localhost:8080/Jira-Synchronization/jirarequest/whatever?OK
Lee Meador
  • 12,829
  • 2
  • 36
  • 42