Results 1 to 5 of 5

Thread: Create servlet

  1. #1
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default Create servlet

    I just started with jsp and java servlet's.
    Now I downloaded the tomcat-apache-server for my .jsp pages and servlets.

    I can run my .jsp files with http://localhost:8080/myJspPage.jsp.

    But how could I run a servlet? When I try to go to http://localhost:8080/myServlet, it gives te error that the file doesn't exist.

    my servlet:
    Java Code:
    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    public class TestServlet extends HttpServlet
    {
    	private static final long serialVersionUID = 1L;
    	
    	public void init()
    	{
    		
    	}
    	
    	public void doGet(HttpServletRequest req, HttpServletResponse res) 
    		throws IOException, ServletException {
    
    		res.setContentType("text/html");
    
    		PrintWriter out = res.getWriter();
    
    		/* Display some response to the user */
    
    		out.println("<html><head>");
    		out.println("<title>TestServlet</title>");
    		out.println("</head>");
    		out.println("<body>");
    		
    		out.println("Hello World");
    		
    		out.println("</body></html>");
    
    		out.close();
    	}
    }
    I compiled it to an .class file and put it into the src folder of the tomcat-server.

    Could someone help me?

    Thanks,
    Dennis
    Last edited by Dennis; 11-05-2010 at 05:22 PM. Reason: spelling mistake

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

    Default

    First steps always looks heavy, but don't worry :)

    You have to become familiar with structure of web project
    and web.xml and how servlet is described and used there.
    Millions of examples are on net for this.

    What IDE you use? Eclipse? Netbeans? Have you crated something like
    'Dynamic web project' using wizard or you just write all by yourself?

    Next time pls paste your error so we know what went wrong.

  3. #3
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default

    I used eclipse to compile the servlet.

    But I don't understand the "structure".
    I've tomcat 7.0.4.
    In the webapps directory I have the directory "servlet-examples" created.
    in "servlet-examples" I created the directory "WEB-INF".
    In WEB-INF I put the "web.xml" file.
    With inside:
    Java Code:
    <web-app>
     <servlet>
      <servlet-name>TestServlet</servlet-name>
      <servlet-class>TestServlet</servlet-class>
     </servlet>
    </web-app>
    In the "WEB-INF" directory I created a directory called "classes".
    In the directory "classes" I put the "TestServlet.class" file.

    Structure now:
    Java Code:
    servlet-examples
        WEB-INF
            classes
                TestServlet.class
            web.xml
    But now I don't know how to run it, or is someting wrong.
    Last edited by Dennis; 11-06-2010 at 07:57 AM.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    You're missing the servlet-mapping element of the xml, which maps the servlet-name to a url.

  5. #5
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default

    I allready found it out, now it works.

    But thank you all for you help. :)

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. Servlet to Servlet connection through URLConnection
    By bsrjavaforum in forum Networking
    Replies: 0
    Last Post: 04-01-2009, 04:12 PM
  3. Replies: 1
    Last Post: 12-02-2008, 12:23 AM
  4. Replies: 0
    Last Post: 08-30-2008, 11:52 AM
  5. Replies: 1
    Last Post: 08-07-2007, 03:47 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
  •