GeeCON Prague 2014*
The moose likes EJB and other Java EE Technologies and the fly likes Form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Form" Watch "Form" New topic
Author

Form

Raja Islam
Ranch Hand

Joined: Dec 07, 2001
Posts: 74
Hi
i want to submit a simple form using threading.
URL cellUrl = new URL("http://192.168.123.41:8080/a/servlet/Form");
HttpURLConnection conn = (HttpURLConnection) cellUrl.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded");
DataOutputStream dos = new DataOutputStream (conn.getOutputStream ());
dos.writeBytes (strData);
dos.flush ();
dos.close ();
i am using this code but it is not working can any one explain working of "application/x-www-form-urlencoded" and what is problem with my code
Snigdha Solanki
Ranch Hand

Joined: Sep 07, 2000
Posts: 128
conn.setRequestProperty ("Content Type", "application/x-www-form-urlencoded");
sets the ENCTYPE attribute of the form. ENCTYPE attribute sets the Multipurpose Internet Mail Extensions (MIME) encoding for the form.
<FORM ENCTYPE = sType... >
where sType = application/x-www-form-urlencoded
which is the default value.
Setting the content type is important because
some versions of Netscape's browser has a bug which does not give proper content type when doing POSTs.


Snigdha<br />Sun Certified Programmer for the Java™ 2 Platform
Raja Islam
Ranch Hand

Joined: Dec 07, 2001
Posts: 74
Hi
Thank u for reply.i understood what the working of application/x-www-form-urlencoded.but can u tell me what the problem with my code it do not submit any data to form.the code of form is given below and it is present in get method of servlet and data is submit on the post method of same servlet.i want to submit my data into post method using HttpURLConnection.

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);}
}
}
Snigdha Solanki
Ranch Hand

Joined: Sep 07, 2000
Posts: 128
Your code works fine for me. I added a few statement for debugging.

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("Parameter is"+req.getParameter("x"));
System.out.println("Creating File");
FileOutputStream file=new FileOutputStream("String.txt");
DataOutputStream dos=new DataOutputStream(file);
dos.writeBytes(req.getParameter("x"));
file.close();
PrintWriter out=res.getWriter();
out.println("String entered is"+req.getParameter("x"));
}
catch(IOException ex){System.out.println("Error "+ex);}
catch(Exception e){System.out.println("Error"+e);}
}

}
What is the error you get?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Form