This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Problem getting servlets working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem getting servlets working" Watch "Problem getting servlets working" New topic
Author

Problem getting servlets working

Raihan Jamal
Ranch Hand

Joined: Mar 23, 2010
Posts: 86
I am new to AJAX so I was testing my code on the tomcat server by using simple example..... But I am not sure where should I put all the files that have .html extension and where should I put .java file extension and where .class file..
In the WEB-INF/classes folder, my getname.java file and getname.class file is there.. am i doing ryt?
and my ajax.html file is in examples/servlets folder am i doing ryt??
so when I was testing my this code it is not working... everytime i get, error during ajax call...
This is my HTML Code.... saved as ajax.html,



and this is my getname.java file:-




please let me know what wrong I am doing and what other things i have to do.. like i have to add something in web.xml file or sever.xml file....??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60808
    
  65

Raihan Jamal wrote:am i doing ryt?

"ryt"? What does that mean?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60808
    
  65

It would also help to show us the error that you are getting. Otherwise, all we can do is to guess.

Please through the "smart questions" link in my signature.
Raihan Jamal
Ranch Hand

Joined: Mar 23, 2010
Posts: 86
am i doing right?
it means that whether I am doing right or not... Since i am not sure where should I put the files... so that's why I asked you...


And How can I show you the error.... I am not sure where should i look for that....
When I opened the page ajax.html....
it doesn't work... instead it shows an alert box message (Error During Ajax call.. Please try again) that I have set up in the Ajax.html page...
sriinu reddy
Greenhorn

Joined: Mar 10, 2009
Posts: 11
Where is that examples/servlets folder placed ?

do place the files in application root , that is convention followed by web app developers.

I guess your ajax request may not be finding the resource at server.
Please make sure you configure the servlet in web.xml.
And also ensure that your ajax request is pointing to resource at Server.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60808
    
  65

The servlet specification sets out how a web app needs to be laid out. There's also Tomcat documentation.
Raihan Jamal
Ranch Hand

Joined: Mar 23, 2010
Posts: 86
I placed the .html file here:-
jakarta-tomcat-4.1.27/webapps/examples/servlets/ajax.html

and When I opened ajax.html page so when I click the submit button to test my AJAX Program... it was not working.. instead it shows an alert box that I have set up in ajax.html page...What I am trying to do.. I am calling servlet getname through the button click event in ajax.html page but when I tried to open the servlet directly...
I got these two error:-


1) exception

javax.servlet.ServletException: Cannot allocate servlet instance for path /examples/servlet/getname




2) root cause

java.lang.UnsupportedClassVersionError: getname (Unsupported major.minor version 49.0)


I have added these two lines in web.xml file
<servlet-mapping>
<servlet-name>getname</servlet-name>
<url-pattern>/getname</url-pattern>
</servlet-mapping>

and

<servlet>
<servlet-name>getname</servlet-name>
<servlet-class>getname</servlet-class>
</servlet>

And also ensure that your ajax request is pointing to resource at Server.

how can I make sure about this thing??

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60808
    
  65

This has very little to do with Ajax, and everything to do with creating and setting up servlets correctly, so I've moved it to the servlets forum with an appropriate title change.

Two things:

java.lang.UnsupportedClassVersionError: getname (Unsupported major.minor version 49.0)

Means that the JDK being used to compile the class is different than the one being used to run it. Check which JDK versions you are using to complie, and which is being used by Tomcat.

and
<servlet-class>getname</servlet-class>

Servlets must be in a package other than the default.

Also, follow class naming conventions. Class names should be in mixed case -- not all lowercase.

I suggest you back off a bit and write some simple servlets and get them working before trying more complicated things such as Ajax.
sriinu reddy
Greenhorn

Joined: Mar 10, 2009
Posts: 11
it's working..
web.xml







document.myForm.message.innerHTML is error prone.

cheers..
srinu


Raihan Jamal
Ranch Hand

Joined: Mar 23, 2010
Posts: 86
Now I am getting this error... As you said I was working with the servlet ryt now... So when I tried to run the servlet directly
then i got this error...

HTTP Status 405 - HTTP method GET is not supported by this URL


my web.xml file has these fields now..

<servlet-mapping>
<servlet-name>getname</servlet-name>
<url-pattern>/examples/servlet/getname</url-pattern>
</servlet-mapping>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60808
    
  65

Raihan Jamal wrote:As you said I was working with the servlet ryt now

"ryt" again? Please use real words when posting to the forums. Abbreviations such as "ryt" in place of "right" only serve to make your posts more difficult to read and less likely to generate useful responses.

Please click this link ⇒ UseRealWords for more information.
Raihan Jamal
Ranch Hand

Joined: Mar 23, 2010
Posts: 86
Sorry again for that

Now I am getting this error... As you said, I was working with the servlet right now... So when I tried to run the servlet directly
then i got this error...

HTTP Status 405 - HTTP method GET is not supported by this URL


my web.xml file has these fields now..

<servlet-mapping>
<servlet-name>getname</servlet-name>
<url-pattern>/examples/servlet/getname</url-pattern>
</servlet-mapping>
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

If you directly send request to Servlet through direct access to the Servlet URL, it goes as GET request ... Since your servlet handles only POST request, you are getting this error... I think you can follow sriinu... Please check again...


SCJP 6 [SCJP - Old is Gold]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60808
    
  65

Raihan Jamal wrote:HTTP Status 405 - HTTP method GET is not supported by this URL

As your servlet only implements a POST handler, this should be no surprise.
sriinu reddy
Greenhorn

Joined: Mar 10, 2009
Posts: 11
Rhian first please learn the basics of servlets and then try with ajax.

 
Don't get me started about those stupid light bulbs.
 
subject: Problem getting servlets working
 
Similar Threads
How to Destroy a Session when the user close the browser
Can AJAX helps me to do that
Java Dynamic Servlet
A problem in hiding a button
Passing resultset from Servlet to JSP