import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import java.io.*;
public class Register extends HttpServlet{
static void toXML(Document doc,PrintWriter o)
{
try{
TransformerFactory xf = TransformerFactory.newInstance();
Transformer xformer = xf.newTransformer();
// xformer.setOutputProperty(OutputKeys.METHOD, "xml");
//xformer.setOutputProperty(OutputKeys.INDENT, "yes");
//xformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
//Result out = new StreamResult(new OutputStreamWriter(System.out,"UTF-8"));
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(System.out);
xformer.transform(source, result);
}
catch (TransformerConfigurationException tce) {
// Error generated by the parser
System.out.println ("* Transformer Factory error");
System.out.println(" " + tce.getMessage() );
// Use the contained exception, if any
Throwable x = tce;
if (tce.getException() != null)
x = tce.getException();
x.printStackTrace();
}
catch (TransformerException te) {
// Error generated by the parser
System.out.println ("* Transformation error");
System.out.println(" " + te.getMessage() );
// Use the contained exception, if any
Throwable x = te;
if (te.getException() != null)
x = te.getException();
x.printStackTrace();
}
catch(Exception exception)
{
System.out.println(exception);
}
}
public void doGet(HttpServletRequest request , HttpServletResponse response)
throws IOException , ServletException{
PrintWriter out = response.getWriter();
try{
out.println("<html><body background=\"images/backG.GIF\" >");
out.println("<basefont face=\"comic sans ms\" color=\" #7429C7\" />");
String param_name = request.getParameter("name");
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse("F:/users.xml");
Element rootElement = document.getDocumentElement();
NodeList users= rootElement.getElementsByTagName("user");
Element User= (Element) document.getElementsByTagName("Users").item(0);
char x = 'n';
for(int i=0; i<users.getLength();i++)
{
Element question_element = (Element) users.item(i);
String attribute_name = question_element.getAttribute("name");
if(param_name.equalsIgnoreCase(attribute_name))
{
x = 'y';
break;
}
}
if(x == 'y')
out.println("This Account Is Already Exists .... Try another name please");
else
{
Element new_child = document.createElement("user");
User.appendChild(new_child);
new_child.setAttribute("name", param_name);
new_child.setAttribute("type", "student");
//NodeList mm = User.getElementsByTagName("user");
toXML(document, out);
}
}
catch(Exception e) {
String errorMessage ="Error making root node: " + e;
out.println(errorMessage);
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request , HttpServletResponse response)throws IOException , ServletException
{
doGet(request, response);
}
}