• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

HttpURLConnection

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
i want to submit a simple form using threading.
import java.net.*;
import java.io.*;
public class Untitled1
{
public static void main(String args[])
{
try
{
URL cellUrl = new URL("http://127.0.0.1:8080/a/servlet/CreateFile");
HttpURLConnection con = (HttpURLConnection) cellUrl.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded");

String x = "Hi HTTP SERVER! Just a quick hello!";
con.setRequestProperty("CONTENT_LENGTH", "" + x.length());
System.out.println("Msg Length: " + x.length());
System.out.println("Getting an output stream...");
OutputStream os = con.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
osw.write(x);
osw.flush();
osw.close();
System.out.println("After flushing output stream. ");
InputStream is = con.getInputStream();
// any response?

/* InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ( (line = br.readLine()) != null)
{
System.out.println("line: " + line);
}*/
} catch (Throwable t)
{
t.printStackTrace();
}
}
}

the output is
Msg Length: 35
Getting an output stream...
After flushing output stream.
java.io.FileNotFoundException: http://127.0.0.1:8080/a/servlet/CreateFile
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:574)
at untitled1.Untitled1.main(Untitled1.java:32)
and the code CreateFile servlet is
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CreateFile extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res) {
try {
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<form method=post>");
out.println("<input type=text name=x>");
out.println("<input type=submit>");
out.println("</form>");
}
catch(IOException ex){System.out.println("Error "+ex);}
}
public void doPost(HttpServletRequest req,HttpServletResponse res) {
try{ System.out.println("Creating File");
FileOutputStream file=new FileOutputStream("String.txt");
DataOutputStream dos=new DataOutputStream(file);
dos.writeBytes(req.getParameter("x"));
file.close();
}
catch(IOException ex){System.out.println("Error "+ex);}
}
}

i am using this code but it is not working. can any one explain what is problem with my code.it create file but throws and exception
java.io.FileNotFoundException: http://127.0.0.1:8080/a/servlet/CreateFile
i want to get data from Untitled1 as a parameter String.
 
Raja Islam
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I solved my prob it is due to cause i am not passing parameter value to CreateFile Servlet.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic