Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: how to check the value

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default how to check the value

    hi,
    in my project...i had used jsp pages..if i enter value in jsp page..it should match with the parameter passed in servlet...and if it is checked it should do process...my project includes xml file..im not getting validated..can any one help me out..if that 'packName' is equal then only it should do deletion otherwise it should show me the message...here is my code

    delete.jsp

     
    <td></td>
                            <td><font color="#800000">&nbsp;</font><font color="#800000"><b>Pack Name</b></font><font color="#800000" size="">&nbsp;</font></td>
                            <td><input name="packName" type="text" id="packName"> </td>
                            <td></td>

    delete.java

     
    package proj;
     
    import java.io.*;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Properties;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    import org.w3c.dom.*;
    import javax.xml.parsers.*;
    import javax.xml.transform.*;
    import javax.xml.transform.dom.*;
    import javax.xml.transform.stream.*;
     
    public class delete extends HttpServlet {
     
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
     
            response.setContentType("text/html");
            PrintWriter pw = response.getWriter();
            String s = null;
            String packName = request.getParameter("packName");
     
            try {
                DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
     
                // Creating the path for XML File using Properties
                ServletContext servletContextObj = request.getSession().getServletContext();
                String excelTemplatesPath = servletContextObj.getRealPath("/");
                String filePaths = excelTemplatesPath + File.separator
                        + "config.properties";
                Properties propObj = new Properties();
                propObj.load(new FileInputStream(filePaths));
                String gprsLoginPath = null;
                gprsLoginPath = propObj.getProperty("GPRS_LOGIN_PATH");
                if (gprsLoginPath == null || gprsLoginPath.trim().equals("")) {
                    gprsLoginPath = excelTemplatesPath;
                }
                gprsLoginPath += File.separator + "login.xml";
                java.io.File file = new java.io.File(gprsLoginPath);
                Document doc = null;
                if (file.exists()) {
                    try {
                        doc = docBuilder.parse(file);
                    } catch (Exception ex) {
                        doc = docBuilder.newDocument();
                        Element root = doc.createElement("PACKS");
                        doc.appendChild(root);
                    }
                } else {
                    doc = docBuilder.newDocument();
                    // Creating Root Tag
                    Element root = doc.createElement("PACKS");
                    doc.appendChild(root);
                }
     
                // creating a new instance of a DOM to build a DOM tree.
                // Document doc = docBuilder.newDocument();
                delNode(file, doc, "PACK_NAME");
                pw.println("<b>Xml File Deleted Successfully</b>");
            } catch (Exception e) {
                System.out.println(e);
                e.printStackTrace();
            }
        }
     
        public static void delNode(java.io.File file, Node parent, String filter)
                throws Exception {
     
            NodeList children = parent.getChildNodes();
     
            for (int i = 0; i < children.getLength(); i++) {
                Node child = children.item(i);
     
                if (child.getNodeType() == Node.ELEMENT_NODE) {
     
                    if (child.getNodeName().equals(filter)) {
                        parent.removeChild(child);
                    } else {
                        delNode(file, child, filter);
                    }
                }
            }
            // TransformerFactory instance is used to create Transformer objects.
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            // create string from xml tree
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            //Document doc = null;
            DOMSource source = new DOMSource(parent);
            transformer.transform(source, result);
            String xmlString = sw.toString();
     
            //File file = new File("E:/xgprs/xmlgprs/login.xml");
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(file)));
            bw.write(xmlString);
            bw.flush();
            bw.close();
     
        }
    }

    thanks in advance..waiting for the reply..


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: how to check the value

    Ok, well I dont know anything about this jsp stuff, but I can help you figure out what it is currently doing.

    Basic Run-Time Debugging includes placing appropriate System.out.println statements to discover exactly what it is doing. So, the first thing you want to do is figure out what the two values you are comparing are equal to when you compare them. So, right before you do the comparing, but the statements:
    System.out.println("PackName = "+packName);
    System.out.println("Other Variable = "+otherVariable);
    Change Other Variable to the variable you are comparing to PackName. From there, we can discover what exactly is happening and work from there.
    Last edited by aussiemcgr; July 21st, 2010 at 11:50 AM.

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to check the value

    hi,
    thanks for you reply...i got it.. i need another help..my doubt is if im going to delete data in xml file...im getting white spaces..but i dont need whitespaces...can you help me how to remove the white spaces...here is my code...

     
     
     
    private void delNode(java.io.File file, Document parent, String filtervalue)
                  throws Exception
          {
            NodeList nodeList = parent.getElementsByTagName("PACK_NAME");
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node child = nodeList.item(i);
                System.out.println("child " + child.getTextContent());
                if (child.getTextContent() != null && child.getTextContent().trim().equalsIgnoreCase(filtervalue.trim())) {
     
                    Node packNode = child.getParentNode().getParentNode();
                    Node packsNode = parent.getFirstChild();
                    packsNode.removeChild(packNode);
     
                    break;
                }
            }
             parent.normalize();
    }

Similar Threads

  1. Collision Check Error
    By Josh Yaxley in forum What's Wrong With My Code?
    Replies: 19
    Last Post: July 12th, 2010, 02:12 PM
  2. Can Anyone Check This Link
    By arpitgadle in forum Java Servlet
    Replies: 5
    Last Post: October 7th, 2009, 08:56 AM
  3. check if a number is an integer
    By rsala004 in forum Java Theory & Questions
    Replies: 3
    Last Post: August 15th, 2009, 03:51 PM
  4. Replies: 5
    Last Post: May 21st, 2009, 02:45 AM
  5. Problem in AWT and IFrame implementaion
    By AZBOY2000 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 24th, 2009, 03:41 AM