aspose file tools*
The moose likes Servlets and the fly likes why am i unable to get the values  from a servlet to a normal class without a browser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "why am i unable to get the values  from a servlet to a normal class without a browser" Watch "why am i unable to get the values  from a servlet to a normal class without a browser" New topic
Author

why am i unable to get the values from a servlet to a normal class without a browser

sai sridhar
Greenhorn

Joined: Aug 23, 2001
Posts: 7
Hi all
I'm calling a servlet from a normal class without using a browser for HTTP requests..I get the following error when i try to capture the values returned by the servlet..
Can anyone please resolve this..

java.io.FileNotFoundException: http://localhost:8080/servlet/HelloServlet?&name=MyName&age=25
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at Hello.main(Hello.java:25)

The code which i wrote is :
import java.util.HashMap;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet {
public void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// getting the parameters
String name = request.getParameter("name");
String age = request.getParameter("age");
//putting them in a hashmap
HashMap hm = new HashMap();
hm.put("name", name);
hm.put("age", age);
//returning them
try {
ObjectOutputStream p = new
ObjectOutputStream(response.getOutputStream());
p.writeObject(hm);
p.flush();
p.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
This is the class that calls the servlet:
import java.net.*;
import java.util.*;
import java.io.*;
public class Hello {
public static void main(String args[]) {
ObjectInputStream is;
URL url;
String uri =
"http://localhost:8080/servlet/HelloServlet";
HashMap hash = new HashMap();
try {
//calling the servlet by passing params
url = new URL(uri + "?&name=MyName&age=25");
// open input stream and read the hashmap
// returned by the servlet
is = new ObjectInputStream(url.openStream());
hash = (HashMap) is.readObject();
// print it out
System.out.println(hash);
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
}
Thanks in Advance
sai
Manohar Karamballi
Ranch Hand

Joined: Jul 17, 2001
Posts: 227
Hai Sai!
May be it has to do with something in registering ur servlet in ur server
Rgds
Manohar
sai sridhar
Greenhorn

Joined: Aug 23, 2001
Posts: 7
Hi Manohar
How do i do so ??
I mean how do i register my servlet in the webserver ??
Thanks
sai

Originally posted by Manohar Karamballi:
Hai Sai!
May be it has to do with something in registering ur servlet in ur server
Rgds
Manohar

PC RE
Greenhorn

Joined: Aug 28, 2001
Posts: 29
Sai,
It depends on the servlet engine(tomcat etc) that ur using.
If ur using tomcat refer to the following url. http://jakarta.apache.org/tomcat/tomcat-4.0-doc/appdev/index.html
For example take the HelloServlet.java servlet in our case.
In WEB-INF\web.xml file in add the following in <servlet> web-app element
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>HelloServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
Make sure u have ur servlets in the appropriate directory.
Hope this helps. Let me know....

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why am i unable to get the values from a servlet to a normal class without a browser