2012-03-16 154 views
0

我們是學生。 在我們的項目中,我們希望通過http post方法將xml塊(基本上是saml assertion)從一臺服務器發送到另一臺服務器。 任何人都可以幫助我們將XML對象從一個servlet發送到另一個servlet,其中每個servlet都駐留在兩臺不同的java計算機上。通過HTTP POST發送XML對象

/*在這裏,我們正試圖從一個servlet的發送XML對象(根),其駐留在不同的PC另一個servlet ......但調度方法心不是在這種情況下工作。*/

public class sp1serv extends HttpServlet 
{ 
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws  ServletException,java.io.IOException 
{ 
Connection c=null; 
Statement s= null; 
ResultSet rs = null; 
String d=null; 
int flag=0; 
resp.setContentType("text/html"); 
PrintWriter out = resp.getWriter(); 
Response response=null; 
XMLObject root=null; 
HttpSession session1=req.getSession(); 
System.out.println(session1.getAttribute("sAccessLevel")); 
System.out.println(session1.getAttribute("sUserId")); 
String eid=session1.getAttribute("sUserId").toString(); 
String[] str1 = {"response","attr",session1.getAttribute("sAccessLevel").toString(),  session1.getAttribute("sUserId").toString() }; 
String filename= eid.concat(".xml"); 
try { 
response=SAMLProtocol.passResponse(str1); 
root=SAMLSignature.passSignature(response,filename); 
} catch (Exception e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
req.setAttribute("SP1",root); 
String abc="http://169.254.229.232:8080/sp_response_handler"; 
RequestDispatcher rd=getServletContext().getRequestDispatcher(abc); 
rd.forward(req, resp); 
break; 
} 
} 
} 
}} 

/*這個servlet用於檢索XML對象(根)和解析it..on另一個服務器。*/

public class sp1_response_handler extends HttpServlet { 
private static final long serialVersionUID = 1L; 
public sp1_response_handler() { 
super(); 
// TODO Auto-generated constructor stub 
} 

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
// TODO Auto-generated method stub 
Response resp=null; 
//XMLObject resp=null; 
resp=(Response) request.getAttribute("SP1"); 
int result=0; 
//SAMLSignature verification=null; 
try { 
result=SAMLSignature.verify(resp); 
} catch (Exception e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 
if(result==1){ 
List attributeStatements = resp.getAssertions().get(0).getAttributeStatements(); 
for (int i = 0; i < attributeStatements.size(); i++) 
{ 
List attributes = ((AttributeStatement) attributeStatements.get(i)).getAttributes(); 
for (int x = 0; x < attributes.size(); x++) 
{ 
String strAttributeName = ((XMLObject) attributes.get(x)).getDOM().getAttribute("Name"); 
List<XMLObject> attributeValues = ((Attribute) attributes.get(x)).getAttributeValues(); 
for (int y = 0; y < attributeValues.size(); y++) 
{ 
String strAttributeValue = attributeValues.get(y).getDOM().getTextContent(); 
System.out.println(strAttributeName + ": " + strAttributeValue); 
} 
} 
} 
response.sendRedirect("SP1.jsp"); 
} 
else 
{ 
System.out.println("NOT a Valid Signature"); 
} 

}} 
+2

通常人們對計算器可以幫助你找出什麼地方錯了你的代碼,但我們不做你的功課:)嘗試一下,顯示你的代碼,你會得到幫助:) – MarcoS 2012-03-16 14:51:34

+0

可能重複[從servlet調用servlet](http://stackoverflow.com/questions/7774749/calling-servlet-from -servlet) – 2012-03-16 14:52:45

回答

0

如果使用彈簧,可以使用RestTemplate。從文檔:

String uri = "http://example.com/hotels/1/bookings"; 
PostMethod post = new PostMethod(uri); 
// create booking request content 
String request = post.setRequestEntity(new StringRequestEntity(request)); 
httpClient.executeMethod(post); 

if (HttpStatus.SC_CREATED == post.getStatusCode()) { 
    Header location = post.getRequestHeader("Location"); 
    if (location != null) { 
    System.out.println("Created new booking at :" + location.getValue()); 
    } 
} 
0

類似的東西應該工作(與參數是一個Map<String,String>):

  StringBuffer data = new StringBuffer(); 
      if (parameters != null && parameters.size() > 0) { 
       for (Entry<String, String> e : parameters.entrySet()) { 
        if (data.length() > 0) { 
         data.append('&'); 
        } 
        data.append(URLEncoder.encode(e.getKey(), "UTF-8")).append("=").append(URLEncoder.encode(e.getValue(), "UTF-8")); 
       } 
      } 
      String parametersAsString = data.toString(); 
      // Send data 
      URL local_url = new URL(url); 
      URLConnection conn = local_url.openConnection(); 
      conn.addRequestProperty("Content-Type", "text/xml; charset=utf-8"); 
      conn.setDoOutput(true); 
      OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
      wr.write(parametersAsString); 
      wr.flush(); 
      break;