Hi!, I want to connect my Java applications to Java servers through HTTP. To do this I have created servlets on the server side and wrap all client messages in HTTP requests.
On the client side, my code should use the URLConnection to send data to the server:
//connect URL url = new URL("http://localhost:8080/servlet/myservlet");
//Open output stream and send some data. OutputStream out = conn.getOutputStream(); //I am not going to send anything on "out," but you can fill this in.
out.flush(); out.close();
//Open input stream and read the data back. InputStream in = conn.getInputStream();
//Here you would read the data back.
in.close()
Everything is working fine. But, my servlet does dome dynamic content generation on client input, how do i send data as REQUEST PARAMETER to the servlet?
Regards, Kunal Jaggi Sun Certified Java Programmer
Hi Smith, Thanks 4 the post, but it still doesn't solve my query. I want to send request parameter 2 a servlet. I tried doing this: BufferedOutputStream out=new BufferedOutputStream(conn.getOutputStream()); out.write("MKT___001".getBytes()); out.flush(); out.close();
But it throws 405 exception. Regards, Kunal Jaggi SCJP
I just looked at the tutorial that Maybach sent to you.
The fourth section is "Applet-Servlet Communication with HTTP GET and POST" and the fifth is "Communicating w/ Object Serialization".
I didn't read it line for line but a quick skim tells me that it should have everything you need to get your program working. Try reading it again and see if it answers your question. [ January 22, 2005: Message edited by: Ben Souther ]
Hi!, I have thought of one alternative to the problem.
Code Snippet: URL url = new URL("http://localhost:8080/servlet/SalarySurveyServlet?emp_id=MKT_001");
Although this works but, i don't want to append the request parameter to the URL as a HTTP GET request.
Regards, Kunal Jaggi SCJP
kunal Jaggi
Greenhorn
Joined: Sep 16, 2003
Posts: 17
posted
0
Hi! Bibeault, Thanks 4 the post. I have gone through the article. What I suggested in my previous post is the GET method of sending output to the servlet.
Following is the code snippet from the article to incorporate the POST method: // connect to the servlet String location = "http://www.foo.com/servlet/TestServlet"; URL testServlet = new URL( servletLocation ); URLConnection servletConnection = testServlet.openConnection();
// inform the connection that we will send output and accept input servletConnection.setDoInput(true); servletConnection.setDoOutput(true);
// Don't use a cached version of URL connection. servletConnection.setUseCaches (false); servletConnection.setDefaultUseCaches (false);
// Specify the content type that we will send binary data servletConnection.setRequestProperty ("Content-Type", "<insert favorite mime type>");
// get input and output streams on servlet . . .
// send your data to the servlet . . .
But, I'm struck at how actually I would send request parameters as request/value pair?
I�m trying to POST data to a Servlet from a Java application and read back the response generated by the servlet. The servlet does a quick database query and returns the response.
//Open output stream and send some data. //OutputStream out = conn.getOutputStream(); //I am not going to send anything on "out," but you can fill this in.
ByteArrayOutputStream byteStream =new ByteArrayOutputStream(512); PrintWriter out = new PrintWriter(byteStream, true); String val1 = URLEncoder.encode("Sun Micro"); String data = "param1=" + val1;// +"¶m2=" + val2; // Note �&� out.print(data); // Note print, not println out.flush(); // Necessary since no println used
byteStream.writeTo(conn.getOutputStream());//send the real data
//out.flush(); //out.close();
//Open input stream and read the data back. InputStream in = conn.getInputStream(); BufferedReader br=new BufferedReader(new InputStreamReader(in)); String str=br.readLine().trim(); while(str!=null){ System.out.println(str); str=br.readLine().trim(); } //Here you would read the data back.
br.close();
} }
And this is the output that I get: <html> <body> <body bgcolor="white"> <h1>hello world servlet!</h1> The request parameter is --> Sun Micro Scott Michel </body> </html> Exception in thread "main" java.lang.NullPointerException at HttpTunnel.main(HttpTunnel.java:40)
Can somebody point out that what is causing the exceprion?
HI! Ben, Thanks for the post. I'm still struck. I have gone through the Code Snippet at the IBM Developer Works site. The only difference between my application and the code snippet on the IBM site is that I'm writing a byte of stream and the article discusses abt. writing a serializable object.
Hi Ben, Thanks 4 ur post. As far as my knowledge goes a Serializable object can be made persistent. It should implement the Serializable interface, which is a marker interface in the java.io package.
Could u plz. go through my code snippets and let me know that where exactly is the problem.
I can't be sure which is line 40 because you didn't post the line numbers. I'm guessing my numbering is right.
You're calling a string method (trim) before testing for null.
Try this:
Or Better:
BTW: If you're going to ask others to debug your code for you (which you really shouldn't (you're cheating yourself out of learning an important skill)), you should try to present your code in a matter that makes it as easy as possible for them to read it. There is a code button in this forum which, when used, preserves indenting. Disable smileys so we can see what you actually typed instead of stupid cartoon faces.
Try to find a text editor that will allow you to paste the line numbers as well as the code. This is particularly helpful if you asking someone to figure out a error listed in a stack trace.
I tend to drop into this site, when I'm waiting for a script to run, or when I have a meeting in 15 minutes and don't want to get too deep into my own code, at the end of a lunch break before getting started on something, etc.. I assume this is the same for most people with jobs. The longer it takes for me to find "line 40" in page with 2 or 3 full classes of code + a stacktrace, the less likely I am to bother. [ February 05, 2005: Message edited by: Ben Souther ]
Kunal Goel
Greenhorn
Joined: Sep 08, 2002
Posts: 16
posted
0
Hi Ben, Thanks for that. In feature I'll take care of all that. There a lot 2 learn here.