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