Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help: need help for post

 
Zoe Peng
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic