Web Hosting Talk







View Full Version : Java Servlet GURU's Help please.


kermitus
03-14-2006, 05:07 PM
I am trying to send an XML stream via POST, as of right now when I run this code I just get an error from the remote server saying that I sent the XML Stream via get. Anyone have an idea what I need to edit so the XML stream is sent via POST to the URL in the code I have listed here? The URL is after the response.sendRedirect tag. I am thinking I need to change the response.sendRedirect tag or add something to it. Earlier I was given a tip to try calling the doPost method of the servlet where I am trying to send the XML stream I.E.

Protected void doPost(HttpServletRequest request, HttpServletResponse response){

servletInstance.doPost(request,response);
}

However I am not exactly sure how to implement this code. Not sure I understand. If this is ideally what I need to do, can someone explain this please, I would certainly appreciate it. I am a student, so I don't understand everything yet, but I learn very quickly.

Thanks,
Corey C

The following code is copied and pasted directly from my NetBeans console.



/*
* JavaTest.java
*
* Created on March 11, 2006, 5:07 PM
*/




import java.io.*;
import java.net.*;




import javax.servlet.*;
import javax.servlet.http.*;




/**
*
* @author Corey
* @version
*/
public class JavaTest extends HttpServlet {

/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/xml;charset=UTF-8");
PrintWriter out = response.getWriter();
String test = "";

out.println("<?xml version=\"1.0\"?>");
out.println("<head>");
out.println("<title>Servlet JavaTest</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet JavaTest at " + request.getContextPath () + "</h1>");
test="<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\n "+
"- <Request version=\"2.0\" id=\"1234567899:1\" xmlns=\"http://names4ever.com/schema/PirinDN/2.0\ (http://names4ever.com/schema/PirinDN/2.0/)" xmlns:a=\"http://rodopi.com/schema/crm/rAL/1.0\ (http://rodopi.com/schema/crm/rAL/1.0/)" xmlns:b=\"http://rodopi.com/schema/pd/rBL/1.0\ (http://rodopi.com/schema/pd/rBL/1.0/)" xmlns:cc=\"http://rodopi.com/schema/crm/rCC/1.0\ (http://rodopi.com/schema/crm/rCC/1.0/)" xmlns:ci=\"http://rodopi.com/schema/crm/rCIL/1.0\ (http://rodopi.com/schema/crm/rCIL/1.0/)" xmlns:i=\"http://rodopi.com/schema/fr/rIL/1.0\ (http://rodopi.com/schema/fr/rIL/1.0/)" xmlns:n=\"http://rodopi.com/schema/crm/rNL/1.0\ (http://rodopi.com/schema/crm/rNL/1.0/)" xmlns:xsi=\"<A href="http://www.w3.org/2001/XMLSchema-instance/">/n" target=_blank>http://www.w3.org/2001/XMLSchema-instance\">\n"+
"- <Header>\n"+
"- <Security>\n"+
"- <UsernameToken>\n"+
" <Username Type=\"customerId\">1234567899</Username>\n "+
"<Password Type=\"text\">secret</Password>\n "+
" </UsernameToken>\n"+
" </Security>\n"+
" </Header>\n"+
"- <Body>\n"+
"- <CheckDomain>\n"+
" <domain name="+request.getParameter("textfield") + "."+ request.getParameter("select")+" />\n" +
" <suggestions>true</suggestions> \n"+
" </CheckDomain>\n"+
"</Body>\n"+
"</Request>\")\n";
out.println(request.getParameter("textfield") +"."+ request.getParameter("select"));
response.sendRedirect("<A title='http://pirin.names4ever.com/TestXAPI/Pirin2Test.exe?"+test' href="http://pirin.names4ever.com/TestXAPI/Pirin2Test.exe?"+test">http://pirin.names4ever.com/TestXAPI/Pirin2Test.exe?"+test);


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

out.close();

}




------------------------------HTTP SERVLET METHODS-------------------

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doGet(request, response);

}

/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
// </editor-fold>
}

Burhan
03-15-2006, 02:54 AM
Ermm, I am not a servlet expert, but your xml in the test variable is not valid XML. It seems you copied it from IE's XML display, because it contains things like links where there shouldn't be any and - where there shouldn't be any.

For example:

xmlns:xsi=\"<A href="http://www.w3.org/2001/XMLSchema-instance/">/n" target=_blank>http://www.w3.org/2001/XMLSchema-instance\">\n"+

is not valid.

saidev
03-16-2006, 02:37 AM
Hope this will help,

By using the Jakarta commons library HTTPClient http://jakarta.apache.org/commons/httpclient/index.html.

Within your doPost or doGet method, you could do something like this.

HttpClient hClient = new HttpClient();
PostMethod postMethod = new PostMethod(url);
postMethod.setRequestbody(getXmlRequest()); //set function below
postMethod.setRequestHeader("Content-type", "text/xml;charset=ISO-8859-1"); //or any other header you want
int responseCode = hClient.executeMethod(postMethod); //this trigger the action to post the xml to url, not response code contain the html response code, like 200 is okay and 404 is bad.



//usually when you post xml, you should get some sort of response back
String xmlResponse = postMethod.getResponseBodyAsString();


//other private functions
private String recordResponse(String response) {
//record response
}

private String getXmlRequest() {
return //you might want to look up JDOM to create xml
}