Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Failing to post data from servlet to web browser see code

 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.net.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TestServlet extends HttpServlet {
String name ="xml=myself";

String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<pwCmdXML>" + "<Command>SubmitJob</Command>" +
"<CommandPassword>aPwd</CommandPassword>\n\r" +
"<ProjectName>TheProject</ProjectName>" +
"<ProjectType>pType</ProjectType>" +
"<ObjectClass>stuff</ObjectClass>" +
"<ObjectList>1,2,3,4,5,6,7,8,9,10,11,12,13,14,15," +
"16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31," +
"32,33,34,35,36</ObjectList>" +
"</pwCmdXML>";

byte[] b = new byte[100];
private String s=URLEncoder.encode("A Test string to send to a servlet");

public void init() throws ServletException {

System.out.println("TestServlet : init");
}
public void destroy() {
System.out.println("TestServlet : destroy");
}



public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {

res.setContentType("text/html");

PrintWriter out = res.getWriter();

// print content


out.println("<html><head>");
out.println("<title>TestServlet ( by ");
out.println( name );
out.println(" )</title>");
out.println("<style>body, p { font-family:tahoma;");
out.println(" font-size:12pt; }</style>");
out.println("</head>");
out.println("<body>");

out.println("<p>TestServlet (");
out.println(b);
out.println(" ) :</p>");

out.println("</body></html>");
out.close();
}

public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {

res.setContentType("text/html");

///////////////////////////////////////////////////////////
try
{

URL url;
URLConnectionurlConn;
DataOutputStreamprintout;
DataInputStreaminput;

// URL of CGI-Bin script.
url = new URL ("http://rsmilgius:5000/InScope.xml");


// URL connection channel.
urlConn = url.openConnection();

// Let the run-time system (RTS) know that we want input.
urlConn.setDoInput (true);

// Let the RTS know that we want to do output.
urlConn.setDoOutput (true);

// No caching, we want the real thing.
urlConn.setUseCaches (false);

// Specify the content type.
urlConn.setRequestProperty("Content-Type", "text/html");



// Send POST output.
printout = new DataOutputStream (urlConn.getOutputStream ());
printout.writeBytes(xml);



// printout.writeBytes(content);
printout.flush ();
// printout.close ();

// Get response data.
input = new DataInputStream (urlConn.getInputStream ());




//String str;
while( input.read(b) > 0) {}
printout.write(b); //send the original content back
System.out.println (b);


/* while (null != ((b = input.readLine())))
{
System.out.println (b);

} */
doGet(req, res);

}
catch (IOException e)
{
// e.printStackTrace();// should do real exception handling
System.out.println(e.getMessage());
doGet(req, res);
}


}
}
Thanks in advance
Ray Smilgius
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is due to the fact that xml doesn't follow the required format or encode for the post body of HTTP protocol. This is where SOAP comes into play. I recommend you take a look at the SOAP servlet project on apache.
 
Ray Smilgius
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks I will investigate further into this.
Thanks Ray Smilgius
 
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic