Results 1 to 4 of 4
  1. #1
    BigBear is offline Member
    Join Date
    Jan 2010
    Posts
    37
    Rep Power
    0

    Default link to another servlet page

    Hi I'm very new in servlet programming. I have package with two .java files. When I put url
    Java Code:
    http://localhost:8080/test
    into browser, my first java file process and my browser shows web page. How can I create a link?
    XML Code:
    <a href="???">go to second page</a>
    There are my files: the first one "test/IndexPage.java":
    Java Code:
    package test;
    
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    public class IndexPage extends HttpServlet {
      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
    	
    	String html = HtmlFactory.head("IndexPage");
    	html += "\t<body>\n";
    	html += "\t\t<h1>Hi, I'm John</h1>\n";
    	html += "\t\t<br />\n";
    	html += "\t\t<a href=\"test.TestPage\">go to test page >></a>\n";
    	html += "\t</body>\n";
    	html += "</html>";
    
    	out.println(html);
      }
    }
    and the second one "test/TestPage.java"
    Java Code:
    package test;
    
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    public class TestPage extends HttpServlet {
      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
    	
    	String html = HtmlFactory.head("Test page");
    	html += "\t<body>\n";
    	html += "\t\t<h1>This is test page</h1>\n";
    	html += "\t\t<br />\n";
    	html += "\t\t<a href=\"/test\"><< go back to index page</a>\n";
    	html += "\t</body>\n";
    	html += "</html>";
    
    	out.println(html);
      }
    }
    and you will likely need my web.xml
    Java Code:
    <web-app version="2.4"
    		 xmlns="http://java.sun.com/xml/ns/j2ee"
    		 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    		 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    		 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
    	<servlet>
    		<servlet-name>IndexPage</servlet-name>
    		<servlet-class>test.IndexPage</servlet-class>
    	</servlet>
    
    	<servlet-mapping>
    		<servlet-name>IndexPage</servlet-name>
    		<url-pattern>/</url-pattern>
    	</servlet-mapping>
    
    	<welcome-file-list>
    		<welcome-file>
    			/test.IndexPage
    		</welcome-file>
    	</welcome-file-list>
    </web-app>
    What should I write instead of this line?
    Java Code:
    <a href=\"test.TestPage\">go to test page >></a>
    Thnx
    Last edited by BigBear; 03-04-2010 at 11:02 PM.

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    364
    Rep Power
    5

    Default

    How did you create those 2 servlets? Using IDE?

    Have you noticed URL mappings?

    Do you know what to write in URL to hit those 2 servlets
    and find a place in web.xml where
    every info about both servlets and URL mapping is used?

    Go through some basic servlet tutorial and create it carefully
    watching out what is going on in web.xml.

    Easy stuff - just go once more through it

    good luck!

  3. #3
    BigBear is offline Member
    Join Date
    Jan 2010
    Posts
    37
    Rep Power
    0

    Default

    I didn't find any basic servlet tutorial with more than one servlet file, do you know that tutorial? Can you give me a link pls?... I'll try find other else too. Thank you.

  4. #4
    BigBear is offline Member
    Join Date
    Jan 2010
    Posts
    37
    Rep Power
    0

    Default

    I found manual for web.xml creating. I managed my web.xml and it runs correct now. Thank you.

Similar Threads

  1. how can i link html+servlet+xml to form servlet
    By balachandarr in forum Java Servlet
    Replies: 0
    Last Post: 04-15-2009, 04:06 PM
  2. Link to specific part of a Java Servlet
    By jwilley44 in forum Java Servlet
    Replies: 2
    Last Post: 02-03-2009, 01:51 AM
  3. Replies: 1
    Last Post: 12-02-2008, 12:23 AM
  4. using a link to call servlet
    By sijokunnappilly in forum Java Servlet
    Replies: 3
    Last Post: 11-12-2008, 02:22 PM
  5. JSP - Link tcreate Folder (servlet)
    By roseline43 in forum Java Servlet
    Replies: 0
    Last Post: 09-15-2008, 12:27 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
  •