File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes getting Error in retrieving CGI variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "getting Error in retrieving CGI variables" Watch "getting Error in retrieving CGI variables" New topic
Author

getting Error in retrieving CGI variables

sandya patel
Greenhorn

Joined: Apr 23, 2005
Posts: 21
Here is code to servlet i used. i am getting errors like incompatible type of declaration. Can't convert int to java.lang.integer...


-----------------------------------------------------------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.http.HttpServletRequest;
import java.lang.*
import java.util.*;
public class Cgi extends HttpServlet
{
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String Authorization = request.getAuthType();
Integer Content_len = request.getContentLength();
String Doc_root = request.getRealPath("/");
Integer Server_port = request.getServerPort();
String Server_software = getServletContext().getServerInfo();
//PrintWriter out;
// set content type and other response header fields first
response.setContentType("text/html");
// then write the data of the response
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println(" <title>Servlet: Showing CGI Variables</title>");
out.println("</head>");
out.println("<body bgcolor=\"#FFFFFF\">");
out.println("<table width=\"75%\" border=\"1\">");
out.println("<tr>");
out.println(" <td width=\"51%\">Authorization</td>");
out.println(" <td width=\"49%\">" + Authorization + "</td>");
out.println(" </tr>");
//out.println("<tr>");
//out.println(" <td width=\"51%\">Content_len</td>");
//out.println(" <td width=\"49%\">" + Content_len + "</td>");
//out.println(" </tr>");
out.println("<tr>");
out.println(" <td width=\"51%\">Doc_root</td>");
out.println(" <td width=\"49%\">" + Doc_root + "</td>");
out.println(" </tr>");
//out.println("<tr>");
//out.println(" <td width=\"51%\">Server_port</td>");
//out.println(" <td width=\"49%\">" + Server_port + "</td>");
//out.println(" </tr>");
out.println(" <tr>");
out.println(" <td width=\"51%\">Server_software</td>");
out.println(" <td width=\"49%\">" + Server_software + "</td>");
out.println(" </tr>");
out.println("</table>");
out.println("</body>");
out.println("</html>");
out.close();
}
}
-----------------
someone get back to me please

Thanks
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

Originally posted by sandya patel:

Integer Content_len = request.getContentLength();

Integer Server_port = request.getServerPort();



int Content_len = request.getContentLength();

int Server_port = request.getServerPort();


Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
sandya patel
Greenhorn

Joined: Apr 23, 2005
Posts: 21
nope that doesn't work. I am getting error "Incompatible type for declaration. Can't convert int to java.lang.Integer.
Integer Content_len = request.getContentLength()
;"

I believe something is wrong in import.java.lang or something.
sandya patel
Greenhorn

Joined: Apr 23, 2005
Posts: 21
when I compiled it, i got 0 errors. but when I upload it to tomcat and checked it, i am getting following error


"javax.servlet.ServletException: Wrapper cannot find servlet class Cgi or a class it depends on"

"java.lang.ClassNotFoundException: Cgi"
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

[Posted before above Post]
Originally posted by sandya patel:
Integer Content_len = request.getContentLength();


I am wodering if still you have above line in your code, remember return type of request.getContentLength() is int and you are trying to hold it in an Integer variable.

please check same, also I would recommend you refer servlet documention.

Nearest way to servlet documentaion is, go to Ben's signature
[ April 24, 2005: Message edited by: Shailesh Chandra ]
sandya patel
Greenhorn

Joined: Apr 23, 2005
Posts: 21
oh ok i will this out. also one more clarification.
for every java, we need to have a web.xml fiel right?
so if you are implementing different .java files in same directory, then do we need to create web.xml file for each .java fiel or can just put some mapping in the same web.xml file and then use it??

can we write like this??
<servlet-name>Hello</servlet-name>
<servlet-class>Hello</servlet-class>
<servlet-name>Cgi</servlet-name>
<servlet-class>Cgi</servlet-class>


or do we need to create different web.xml file each time??
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081


Originally posted by sandya patel:
for every java, we need to have a web.xml fiel right?





I am not sure what you mean by this.
for every web application we need only one web.xml,no matter how many servlet you have in your application .



can we write like this??
<servlet-name>Hello</servlet-name>
<servlet-class>Hello</servlet-class>
<servlet-name>Cgi</servlet-name>
<servlet-class>Cgi</servlet-class>





a little bt modification is required





or do we need to create different web.xml file each time??


you need only one web.xml file

Shailesh
[ April 25, 2005: Message edited by: Shailesh Chandra ]
sandya patel
Greenhorn

Joined: Apr 23, 2005
Posts: 21
I changed interger to int. then also, I am getting the same errors when uploaded in Tomcat

"javax.servlet.ServletException: Wrapper cannot find servlet class Cgi or a class it depends on"

"java.lang.ClassNotFoundException: Cgi"

what should be changed now??
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

can you put stacktrace of exception !! because you code is ok if you replaced integer to int, however for my curiosity I tested in on tomacat with JDK 1.5.

I didn't get any error with with Integer because jdk1.5 has automatic conversion facility from primitive values to their wrapper class

Shailesh
[ April 25, 2005: Message edited by: Shailesh Chandra ]
sandya patel
Greenhorn

Joined: Apr 23, 2005
Posts: 21
hi
what do you mean by "stacktrace of exception !! ". sorry i am novice to java. so i dont know much details. by the way my server is Apache Tomcat/4.1.27. when i deployed in server, it is showing TRUE under "running" column but when i am testing in html page, outputs are not coming and the errors i posted before are coming. i dont know where is wrong??
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

you need to put complete code in a try block then catch the exception in catch block where you can print stacktrace like

try
{
//your code
}
catch(Exception e)
{
e.printStackTrace(); // you will see this on tomcat console

}
make sure that you are compiling correct code and placing in in tomacat



Shailesh
[ April 25, 2005: Message edited by: Shailesh Chandra ]
sandya patel
Greenhorn

Joined: Apr 23, 2005
Posts: 21
HI Shailesh
I am getting these errors after uploding to tomcat and when test the html file. do i need to create jsp file also??is there any way i can chat with you in Yahoo!messeger??

HTTP Status 500 javax.servlet.ServletException: Wrapper cannot find servlet class Cgi or a class it depends on.
java.lang.ClassNotFoundException: Cgi
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

Originally posted by sandya patel:
HI Shailesh
I am getting these errors after uploding to tomcat and when test the html file. do i need to create jsp file also??is there any way i can chat with you in Yahoo!messeger??

HTTP Status 500 javax.servlet.ServletException: Wrapper cannot find servlet class Cgi or a class it depends on.
java.lang.ClassNotFoundException: Cgi


still you can check the stack trace of application in a way I told,
I dont find any problem with servlet code at my machine,possible error can be you are not placing right file in tomcat or there is some problem with your web.xml. regarding YM ...well it will loose the purpose of Javarach, at YM only I would be there but here in JR you will find great brains to help like Bear,Ben Souther they are always available,however I have sent you my YM id in a PM you can add me I am online

I also want to add Ben,adeel and some other one in my YM list but I think that would violate purpose of JR.



Shailesh
[ April 25, 2005: Message edited by: Shailesh Chandra ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

HTTP Status 500 javax.servlet.ServletException: Wrapper cannot find servlet class Cgi or a class it depends on



Do you have a class named Cgi?
If so, is it in a package and where is the class file?



BTW: I agree with Shailesh. Moving the conversation to a private message platform and solving it there would make this whole thread useless to someone trying to solve the same problem weeks, months, or years from now.
[ April 25, 2005: Message edited by: Ben Souther ]

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

Joined: Apr 23, 2005
Posts: 21
Do you have a class named Cgi?
If so, is it in a package and where is the class file?



BTW: I agree with Shailesh. Moving the conversation to a private message platform and solving it there would make this whole thread useless to someone trying to solve the same problem weeks, months, or years from now.
---------------------------------------------------------------------
HI Ben
yes I do have class named Cgi.class. it is in WEB_INF-->classes--->Cgi.class
sorry, i dont want to voilate the rules or whatever. llet me know where my code went wrong.
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

I would like to see your web.xml

Shailesh
sandya patel
Greenhorn

Joined: Apr 23, 2005
Posts: 21
I have two java file in sample application.thats why I included two files "Hi World" and "Cgi".


<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<display-name></display-name>
<servlet>
<servlet-name>Hi World</servlet-name>
<servlet-class>Hi World</servlet-class>
</servlet>
<servlet>
<servlet-name>Cgi</servlet-name>
<servlet-class>Cgi</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hi World</servlet-name>
<url-pattern>/Hi World</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Cgi</servlet-name>
<url-pattern>/Cgi</url-pattern>
</servlet-mapping>
</web-app>
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

does your hi world servlet work ??? I dont think so , anyway this is out of your problem's conext.

Entries for Cgi servlet is ok in your web.xml
can you put full path of your Cgi.class like c:\tomcat\......
and what is your application name ,also provide URL your invoking on browser.

Shailesh
sandya patel
Greenhorn

Joined: Apr 23, 2005
Posts: 21
does your hi world servlet work ??? I dont think so , anyway this is out of your problem's conext.

Entries for Cgi servlet is ok in your web.xml
can you put full path of your Cgi.class like c:\tomcat\......
and what is your application name ,also provide URL your invoking on browser.

-------------------------------
Yes my HiWorld servlet is working.
path of Cgi class is sample_webapp -->WEB-INF -->classes-->Cgi.class
path of web.xml is sample_webapp -->WEB-INF -->web.xml
application name is Cgi
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

you have defined <servlet-class>Hi World</servlet-class> and there is a space in class name if your servlet hi world is running then that would be not from same web.xml.Are you able to compile a java file with a space in class name

Shailesh
sandya patel
Greenhorn

Joined: Apr 23, 2005
Posts: 21
oh sorry.

i modified it while uploading it, but gave you the old one. i dont have space in "Hi World". Its "HiWorld"
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

we can only assume your environment, and I feel there is some problem in configuration.
Since your HiWorld Servlet is working so on the first instance you can check you code of CGI servlet .

Put the code of CGI servlet in HelloWrold servlet and test ..if you face any error.
If no error is there just compare configuration of both servlet

Shailesh
Bimal Patel
Ranch Hand

Joined: Aug 29, 2003
Posts: 130
Hello,

Has it been solved? I think there is something wrong with web.xml only.
[ April 26, 2005: Message edited by: Bimal Patel ]

Work Hard, Expect The Worst...<br /> <br />Bimal R. Patel<br />(SCJP 1.2, SCWCD 1.4)
sandya patel
Greenhorn

Joined: Apr 23, 2005
Posts: 21
Hi
you are right. i created seperate web.xml for Cgi . then i got the result. thank u very much.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Wait, you don't have 2 web.xml files, do you?
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

Originally posted by Ben Souther:
Wait, you don't have 2 web.xml files, do you?


even this made me puzzled sandya is having two different application for two servlet or something is there that is not explained to us.

even no operating system would allow to have two file with same name in one folder.

Shailesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting Error in retrieving CGI variables