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 Beginning Java and the fly likes Problem compiling servlet [RESOLVED] 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 » Beginning Java
Bookmark "Problem compiling servlet [RESOLVED]" Watch "Problem compiling servlet [RESOLVED]" New topic
Author

Problem compiling servlet [RESOLVED]

Mickey Smith
Ranch Hand

Joined: Jun 26, 2006
Posts: 30
Hi All,

I have compiled my servlet to a .class file and placed it in /webapps/myApp/WEB-INF/classes/com/myappln/ dir. But when i invoke it, it does not run and gives a 404.

Thanks in advance




[BPSouther: Changed subject line prior to moving to JIG]
[ July 14, 2006: Message edited by: Ben Souther ]
Ronald Tagra
Greenhorn

Joined: Jul 14, 2006
Posts: 4
how did you invoke it?
Mickey Smith
Ranch Hand

Joined: Jun 26, 2006
Posts: 30
i just invoked using the url http://localhost:8080/myApp/servlet/MyServlet
and also the url http://localhost:8080/myApp/servlet/com.myappln.MyServlet
Ronald Tagra
Greenhorn

Joined: Jul 14, 2006
Posts: 4
You have to enable the invoker servlet.

if you're using tomcat, it's found in TOMCAT_HOME/conf/web.xml

Look for commented:

<!-- <servlet>
<servlet-name>invoker</servlet-name>
<servlet-class>
org.apache.catalina.servlets.InvokerServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet> -->


And

<!--
<servlet-mapping>
<servlet-name>invoker</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
-->

portion and uncomment then restart tomcat.
[ July 14, 2006: Message edited by: Ronald Tagra ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I'd recommend to avoid using the Invoker and give a proper mapping to your servlet in web.xml, using the servlet-mapping tag.


[My Blog]
All roads lead to JavaRanch
Ronald Tagra
Greenhorn

Joined: Jul 14, 2006
Posts: 4
agree, that's the best way to go.
Mickey Smith
Ranch Hand

Joined: Jun 26, 2006
Posts: 30
I have done that but still it is unavailable.

During first invocation it gives a IllegalAccessException

and after that a 404.
Mickey Smith
Ranch Hand

Joined: Jun 26, 2006
Posts: 30
Here is the stack trace....

javax.servlet.ServletException: Cannot allocate servlet instance for path /myApp/servlet/com.myappln.MyServlet
org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:388)
org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

java.lang.IllegalAccessException: Class org.apache.catalina.core.StandardWrapper can not access a member of class com.myappln.MyServlet with modifiers ""
sun.reflect.Reflection.ensureMemberAccess(Reflection.java:57)
java.lang.Class.newInstance0(Class.java:302)
java.lang.Class.newInstance(Class.java:261)
org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:369)
org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
Ronald Tagra
Greenhorn

Joined: Jul 14, 2006
Posts: 4
paste your servlet too.
Mickey Smith
Ranch Hand

Joined: Jun 26, 2006
Posts: 30
This is my servlet code:

package com.myappln;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class FirstServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("GET Request. No Form Data Posted");
}

public void doPost(HttpServletRequest request, HttpServletResponse res)
throws IOException, ServletException
{
Enumeration e = request.getParameterNames();
PrintWriter out = res.getWriter ();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = request.getParameter(name);
out.println(name + " = " + value);
}
}
}

Also i had compiled the servlet without public access specifier coz when i put public the error in compilation is FirstServlet .java:8: class FirstServlet is public, should be declared in a file named FirstServlet.java
public class FirstServlet extends HttpServlet {

Hence i think because of the access specifier, Tomcat is unable to load it.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Is this the code (exact code) that you compiled?

Removing the public modifier was not the way to fix that problem.
The name specified in the class signature has to match the name of the file.
If you fix that there will be no problem with the public modifier.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
pradeep setti
Greenhorn

Joined: Jul 12, 2006
Posts: 9
hi this is pradeep.

i have tried ur servlet.

and got the result.

in web.xml do the following: i am giving the complete entries to get u understood:

<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>com.myappln.FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/FirstServlet</url-pattern>
</servlet-mapping>
pradeep setti
Greenhorn

Joined: Jul 12, 2006
Posts: 9
add : com.myappln.FirstServlet in <servlet-class></servlet-class> tag of ur web.xml
Mickey Smith
Ranch Hand

Joined: Jun 26, 2006
Posts: 30
well done that already but the main problem is whenever i put public modifier, the servlet does not compile even when the class and file names are the same.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The public modifier needs to be there.

You know that the casing needs to match in order for the class name and the servlet name to be the same, right?
Mickey Smith
Ranch Hand

Joined: Jun 26, 2006
Posts: 30
Yes Ben i know that but FirstServlet.java is unable to compile with a public access modifier
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

pradeep setti,

Welcome to JavaRanch and thank you for helping out.
I'd like you to take a minute to read: UseRealWords

Abbreviations such as "u" or "ur" in place of "you" and "you are"
or "you're" confound language translation software making it hard
for our non-English speaking members to read your posts.

Again thank you for your help; both with this situation and in general with Javaranch.

-Ben
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Post the exact code of the servlet you are trying to compile.
Then post the command that you're using when trying to compile the class.

If the compiler is giving you that error message then the name doesn't match the file name.

Note: Windows Explorer will somethimes show files with the first letter captialized, even it it's not. Try typing 'dir' in the command window to see a list of the files. See if the casing matches what you've got in the class signature.

In any case, changing the modifier to get it to compile will leave you with a useless servlet.
Mickey Smith
Ranch Hand

Joined: Jun 26, 2006
Posts: 30
This is the code which does not compile and i am using TextPad to compile it.

package com.myappln;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class FirstServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("GET Request. No Form Data Posted");
}

public void doPost(HttpServletRequest request, HttpServletResponse res)
throws IOException, ServletException
{
Enumeration e = request.getParameterNames();
PrintWriter out = res.getWriter ();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = request.getParameter(name);
out.println(name + " = " + value);
}
}
}

Also the other servlets within webapps compile without a problem.

Thanks
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Something is wrong with the filename then.

Did you do as I suggested and look at the filenames in the current directory by typing 'dir' at the command line?

If you're curious, open a command prompt, CD to the directory with that file in it and type:

Then open fileList.txt with an editor and post the contents.

If you're not curious and just want this to work, rename the file.
Once to junk.java, then back to FirstServlet.java (being very careful with the casing).
Mickey Smith
Ranch Hand

Joined: Jun 26, 2006
Posts: 30
Here is what the dir command gives:
jakarta-tomcat-5.0.28\webapps\myApp\WEB-INF\classes\com\myappln\

07/14/2006 03:13p <DIR> .
07/14/2006 03:13p <DIR> ..
07/14/2006 03:07p 899 FirstServlet .java
1 File(s) 899 bytes
2 Dir(s) 34,100,178,944 bytes free
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

There is a space between 'FirstServlet' and '.java'


Mickey Smith
Ranch Hand

Joined: Jun 26, 2006
Posts: 30
Hey Ben resolved the issue. Just couldn't think it could be so petty.

Thanks a lot.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Glad it's working for you.

A couple tips:
If you use the UBB Code tags when posting any literal text (code or screen dumps) things like this stand out a little better. The UBB code tags preserve whitespace and use a mono-spaced font which helps preserve indenting.

Please, never, NEVER, post code that is different from what you've actually compiled or are running. To do so is to drag a Red Herring across the path of the folks trying to help you. This will almost always result in a huge waste of time.

We've compiled a list of these types of tips.
You can read them here.




Since this has turned out to be a Java compilation issue and not a servlet invocation issue, I'm going to change the subject line and move this thread to the Java In General (Beginner) forum so people searching on javac issues will be able to find this there.

Thanks for posting back.
-Ben
 
Don't get me started about those stupid light bulbs.
 
subject: Problem compiling servlet [RESOLVED]
 
Similar Threads
calling servlet through javascript
servelet project
which book?
Pagging Problem
Help me in send redirct