sir, i can't access Xml which is present in my d drive plz can u should go through my code n tell me the things where i went wrong
java servlet program
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/xml"); FileRead fr = new FileRead(); FileWrite fw = new FileWrite(); ResultSet rs=null,rs_path=null; String xmlpath; response.setHeader("Cache-Control", "no-cache"); System.out.println("Hai "+request.getParameter("url")); String url = request.getParameter("url"); String userName = request.getParameter("userName"); String searchPassword = request.getParameter("searchPass"); System.out.println(userName+" "+searchPassword); try { PrintWriter out = response.getWriter(); rs = search_pass.execSQL("Select * from Member_Registration where searchpass='"+searchPassword+"' and username='"+userName+"' and FaultSearch=0"); }if(rs.next()) { tempValue=0; rs = bean_obj.execSQL("Select * from feed_url where URL='"+url+"' and username='"+userName+"'"); System.out.println(rs.next()); String filename= request.getParameter("FileName"); filename = filename.replace(":","_"); xmlpath = "D:/Leo/"+filename+".xml"; out.println("<p Style='position:Absolute;top:275px;left:660px;font:bold 12pt/14pt Times New Roman;color:#FFFAF0'>"+xmlpath+"</p>"); if(rs.next()) { rs_path = bean_obj1.execSQL("Select filename from feed_url where url='"+url+"' and username='"+userName+"'"); rs_path.next(); String ss =filename; rs_path.getString("FileName"); System.out.println(ss); xmlpath = "D:/Leo/"+ss+".xml"; String fileContent = fr.fileRead(xmlpath); System.out.println(fileContent); out.println(fileContent); } else { out.println("New"); bean_obj.updateSQL("insert into feed_URL(userName,URL,FileName) values('"+userName+"','"+url+"','"+filename+"')"); readXML = new XmlBean(userName,url); String str = fw.fileWrite(xmlpath,readXML);
system.out.println(str);
} search_update.updateSQL("UPDATE Member_Registration SET FaultSearch=0 WHERE Username='"+userName+"'"); } //System.out.println("Invalid s1234earch Password "+tempValue); else { if(++tempValue>=3) { search_update.updateSQL("UPDATE Member_Registration SET FaultSearch=1 WHERE Username='"+userName+"'"); tempValue=0; response.sendRedirect("Search_Password_Error_logout.jsp"); int i=00; out.println("Lock"); } else { out.println("Invalid search Password "+tempValue); response.sendRedirect("Search_Password_Error_logout.jsp"); int i=0; out.println("Invalid"); } } } catch(Exception e) { System.out.println(e); } } // <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 { processRequest(request, response); } /** Handles the HTTP <code>POST</code> method. * @param request servlet request * @param response servlet response */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** Returns a short description of the servlet. */ public String getServletInfo() { return "Short description"; } // </editor-fold>
}
jsp:
<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <%-- The taglib directive below imports the JSTL library. If you uncomment it, you must also add the JSTL library to the project. The Add Library... action on Libraries node in Projects view can be used to add the JSTL 1.1 library. --%> <%-- <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> --%>
<% String usname = (String)session.getAttribute("uname"); session.setMaxInactiveInterval(-1); //out.println(usname); %> <p style="position:absolute;font : bold 12pt/14pt Times New Roman;top: 8px;left:650px;"> <% String name = (String)session.getAttribute("MemberName"); out.println(name); session.setMaxInactiveInterval(-1); %> </p> <body> <form id="main"> <!-- DIV FOR LOG OUT --> <div style="position:absolute;top:10px;left:800px;cursor:hand;" onclick="logout()"> <img src="Image/img03.gif" style="height:20px;width:100px;"/> <span style="position:absolute;top:0px;left:23px;font-weight:bold">Log Out</span> </div> <!-- DIV FOR HOME PAGE AND MANAGE SUBSCRIPTION --> <div class="home" id="home" style="position:absolute;top:35px;left:5px;width:980px;height:100px; background-color:#87CEFA;overflow:auto"> <span style="position:absolute;top:30px;left:70px;font-weight:bold">Check Feed URL :</span> <input id="url" type="text" size="75" style="position:absolute;top:30px;left:230px;"/> <span style="position:absolute;top:70px;left:70px;font-weight:bold">Search Password :</span> <input id="searchPass" name="searchPass" type="text" size="75" style="position:absolute;top:70px;left:230px;"/> <input type="button" onclick="checkXML('<%=usname%>')" size="14" value="Add URL" style="position:absolute;top:45px;left:750px;"/> <p style="position:absolute;top:105px;left:100px;font-weight:bold"></p> <!--<a id="home1" href="Manage_Feeds.jsp" target="_self" style="position:absolute;top:60px;left:40px;font-weight:bold;text-decoration:none;cursor:hand;"> <u> Manage Subscription </u>--> </div> <div id="Rss_Title" style="position:absolute;top:150px;left:2px;padding:0px 0px 0px 0px;background-color:#FFFFFF;" > </div> <div style="position:absolute;top:10px;left:50px;cursor:hand;" onclick="Search_His('<%=usname%>')"> <img src="Image/img03.gif" style="height:20px;width:150px;"/> <span style="position:absolute;top:0px;left:23px;font-weight:bold">Search History</span> </div> <div style="position:absolute;top:10px;left:250px;cursor:hand;" onclick="setting('<%=usname%>')"> <img src="Image/img03.gif" style="height:20px;width:100px;"/> <span style="position:absolute;top:0px;left:28px;font-weight:bold">Setting</span> </div> </form> </body>
Use dom parser..........
import java.io.*; import org.w3c.dom.*; import javax.servlet.*; import javax.xml.parsers.*; import javax.servlet.http.*; public class ReadXML extends HttpServlet{ public boolean isTextNode(Node n){ return n.getNodeName().equals("#text"); } public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); try{ DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.parse("C:/roseindia.xml"); out.println("<table border=2><tr><th>Name</th><th>Address</th></tr>"); Element element = doc.getDocumentElement(); NodeList personNodes = element.getChildNodes(); for (int i=0; i<personNodes.getLength(); i++){ Node emp = personNodes.item(i); if (isTextNode(emp)) continue; NodeList NameDOBCity = emp.getChildNodes(); out.println("<tr>"); for (int j=0; j<NameDOBCity.getLength(); j++ ){ Node node = NameDOBCity.item(j); if ( isTextNode(node)) continue; out.println("<td>"+(node.getFirstChild().getNodeValue())+"</td>"); } out.println("</tr>"); } out.println("</table>"); } catch(Exception e){ System.out.println(e); } } }