Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HttpURLConnection

 
Raja Islam
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Hi
I solved my prob it is due to cause i am not passing parameter value to CreateFile Servlet.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic