public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
Document testDocument;
DocumentBuilder testBuilder;
DocumentBuilderFactory testFactory;
// set content type and other response header fields first
response.setContentType("text/html");
// get the communication channel with the requesting client
PrintWriter out = response.getWriter();
out.println("This is a test");
try {
testFactory = DocumentBuilderFactory.newInstance();
testBuilder = testFactory.newDocumentBuilder();
testDocument = testBuilder.parse(request.getInputStream());
Element tempUser = testDocument.getDocumentElement();
String tempUserName = getElementValue(tempUser,"username");
String tempPassword = getElementValue(tempUser,"password");
out.println(" The user name is : "+tempUserName);
if(tempUserName.equals("essai") && tempPassword.equals("Yes")){
out.print("<loginreply status='ok'></loginreply>");
}
else{
out.print("<loginreply status='failed'></loginreply>");
}
//out.close();
}
catch (Exception e) {
out.print("<loginreply status='failed'></loginreply>");
out.println("ERROR: "+e.getMessage());
out.println(stack2string(e));
}
out.close();
}
private String getElementValue(Element parentElement, String elementName){
NodeList tempList;
Element tempElement;
Text tempText;
tempList = parentElement.getElementsByTagName(elementName);
tempElement = (Element)tempList.item(0);
tempText = (Text)tempElement.getFirstChild();
return tempText.getNodeValue();
}
static public String stack2string(Exception e) {
try {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
return "------\r\n" + sw.toString() + "------\r\n";
}
catch(Exception e2) {
return "bad stack2string";
}
}
Partager