• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

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

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Sai!
May be it has to do with something in registering ur servlet in ur server
Rgds
Manohar
 
sai sridhar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....

 
If you're gonna buy things, buy this thing and I get a fat kickback:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!