*
The moose likes Servlets and the fly likes help: need help for post Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "help: need help for post " Watch "help: need help for post " New topic
Author

help: need help for post

Zoe Peng
Ranch Hand

Joined: Nov 27, 2000
Posts: 112
Hello.
I got problems to return results after I open URLConnection. Here is the information:
1: pass 3 params(ProjectID, TypeID, ObjectLabel; in html file) to servlet(java) (ok)
2. servlet(java) open a URLConnection(ok), and pass three param to serverA(just accept POST)
3. serverA automatically to generate results and return results 4. servlet get the results and print out
The problem I have is the serverA didn't get param after I open the URLConnection. (I can print out the param(passed from html file) and open connection)
If I try to use response.sendRedirect, server can get the param but still not return the contents for me. cos serverA just accepts POST method.
URLConnection also just "get" "1" parameters, too.
How can I do to POST these three parameters??
Is something wrong with my code? or the order?? Thanks in advance.
--------
public class ClarityConnection extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String ProjectID=request.getParameter("ProjectID");
if ((ProjectID == null) | |
(ProjectID.length() == 0)) {
reportError(response, "ProjectID Not Specified");
return;
}
String TypeID=request.getParameter("TypeID");
if ((TypeID == null) | |
(TypeID.length() == 0)) {
reportError(response, "TypeID Not Specified");
return;
}
String ObjectLabel=request.getParameter
("ObjectLabel");
if ((ObjectLabel == null) | |
(ObjectLabel.length() == 0)) {
reportError(response, "ObjectLabel Not Specified");
return;
}
String Debug = "true";
String data = ProjectID + TypeID + ObjectLabel;
ProjectID = URLEncoder.encode(ProjectID);
TypeID = URLEncoder.encode(TypeID);
ObjectLabel = URLEncoder.encode(ObjectLabel);
// Specify content type.
response.setContentType("text/html");
// Access the output stream.
PrintWriter out = response.getWriter();
URL url = new URL
("http://test.test.com/content/get/returnObject.cfm?debug=1");
//open connection
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
urlConn.setRequestMethod(request.getMethod());
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
urlConn.setUseCaches(false);
urlConn.setRequestProperty("HTTP-
Version", "HTTP/1.1");
urlConn.setRequestProperty("Content-
Type", "application/x-www-form- urlencoded");
//for testing parameter
out.println(request.getParameterNames());
//The program then creates an output stream on the connection and opens a PrintWriter on it:
DataOutputStream printout = new DataOutputStream (urlConn.getOutputStream());
//the program writes the required information to the output stream and closes the stream
printout.writeBytes(data);
printout.flush();
printout.close();
//sends information back to you via the same URL
BufferedReader in = new BufferedReader (new
InputStreamReader (urlConn.getInputStream()));
StringBuffer str = new StringBuffer();
String line = null;
try{
while ((line=in.readLine()) !=null){
str.append(line);
}
}catch(NullPointerException e){
out.println("error!!!");
e.printStackTrace(out);
}
out.println(str);
in.close();
}
public void reportError(HttpServletResponse response,
String message)
throws IOException {
response.sendError(response.SC_NOT_FOUND,
message);
}
public void doPost(HttpServletRequest
request,HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: help: need help for post
 
Similar Threads
URLConnection to Servlet
Uploading Image from Java Desktop App to Server
Trying to post from servlet directly to server see code
Help me Plz.........Confused:
Fake a post request from java class