Results 1 to 8 of 8
  1. #1
    Juuno is offline Member
    Join Date
    Feb 2009
    Posts
    18
    Rep Power
    0

    Default linking to a web page

    I have an xml file which includes file name and source(which is web page link) like following:

    <file>
    <name>Japan</name>
    <source>ttp://en.wikipedia.org/wiki/Japan</source>
    </file>
    <file>
    <name>USA</name>
    <source>ttp://en.wikipedia.org/wiki/Japan</source>
    </file>

    And my program accept the <name> as user input via web UI. So, when the user input 'Japan', then my program will search through the xml file and then return source, I mean the link of the web page.

    But I get the text of the <source> tag as a string and when I try to return it to my JSP and run my program, there's an error when I click on the link:
    ttp://localhost:8080/myfolder/%22(The request resource is not available).

    I know that it's because it really doesn't exist in that folder. But what I want is to point to the actual address, like search engine.

    I deleted 'h' in http bcoz my post count is less than 20. It's not program error.

    How can I do this?
    Thanks in advance.

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    You appear to be returning a " (%22) instead of the contents of <source>. Either your xml processising or your JSP is wrong.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    Juuno is offline Member
    Join Date
    Feb 2009
    Posts
    18
    Rep Power
    0

    Default

    Here is the code in my Java program:

    Java Code:
        public Stringb showLink(String filename, String inputname){
    	//filename is the name of xml file I have to search
    	//inputnmae is the user input which I have to match with
    	//the text of <name> tag
        	
        	String strlink="";
    		String outputlink="";
        	File filetosearch = new File("C:\\matchTest\\dtafiles\\" + filename);
        	
        	{
    	    	try{
        	    
    	    	   	DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    	    		domFactory.setNamespaceAware(true); 
    	    		DocumentBuilder builder = domFactory.newDocumentBuilder();
    	    		Document doc = builder.parse(filetosearch);
    
    	    		XPathFactory factory = XPathFactory.newInstance();
    	    		XPath xpath = factory.newXPath();
    	    		XPathExpression expr = xpath.compile("//body//file[name='"+inputname+"']//source//text()");
    	    		    		
    	    		Object result = expr.evaluate(doc, XPathConstants.NODESET);
    	    		NodeList nodes = (NodeList) result;
    	    				    			
    	    			for (int j=0; j< nodes.getLength(); j++){
    		    			strlink =  nodes.item(j).getTextContent();
    	    			}	
    
    	    	}catch(Exception e){return e.toString();}
           }
        	strlink = strlink.toString();
    		outputlink =  "<a href='\"' strlink '\"'>" + strlink + "</a>";
        	return outputlink.toString();
        }
    And here is the code from my JSP:

    Java Code:
    <jsp:setProperty name="obj" property="name" />     		<jsp:getProperty name="obj" property="name"/>:
    <br> 
    
    ${tf:testMethod(obj, obj.name)}
    And it shows the results as a valid link, for example:

    Japan:
    ttp://en.wikipedia.org/wiki/Japan
    But the problem is when I click on that link, there's an error I described.

    Do I need to download and save those web pages in my Web Server folder?
    Thanks.

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Java Code:
    <a href='\"' strlink '\"'>
    Exactly as I said, this gives you an href of " followed by some invalid html.

    Also, why are you pointlessly calling toString() on Strings?
    Are you aware that your for loop discards everything except the final iteration?
    Last edited by OrangeDog; 05-01-2009 at 05:49 AM.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  5. #5
    Juuno is offline Member
    Join Date
    Feb 2009
    Posts
    18
    Rep Power
    0

    Default

    invalid link??

    Can you explain me more detail and how can I solve it?
    Is it because of the port opened in Tomcat?

  6. #6
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    No, it is because you don't understand either Java strings or html.

    This is a link to a page about Japan
    Java Code:
    <a href="http://en.wikipedia.org/wiki/Japan">http://en.wikipedia.org/wiki/Japan</a>
    This is not
    Java Code:
    <a href='"' strlink '"'>http://en.wikipedia.org/wiki/Japan</a>
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  7. #7
    Juuno is offline Member
    Join Date
    Feb 2009
    Posts
    18
    Rep Power
    0

    Default

    Then, how can I parse that string from my Java program to JSP page?

  8. #8
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Can you really not see why
    Java Code:
    outputlink =  "<a href='\"' strlink '\"'>" + strlink + "</a>";
    gives the second example above instead of the first?
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. static linking
    By Nicholas Jordan in forum Advanced Java
    Replies: 35
    Last Post: 03-14-2009, 09:17 PM
  2. linking 2 Jframes
    By suhaib1thariq in forum New To Java
    Replies: 9
    Last Post: 02-13-2009, 01:30 AM
  3. applet linking menus
    By lordbob75 in forum Java Applets
    Replies: 0
    Last Post: 01-27-2009, 10:49 PM
  4. Linking of exe files
    By archu2friends in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-06-2008, 06:08 AM
  5. Linking code to GUI Interface
    By ai_2007 in forum Advanced Java
    Replies: 1
    Last Post: 07-02-2007, 01:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •