Results 1 to 9 of 9

Thread: Running servlet

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Resolved Running servlet

    I'm using Eclipse and wanted to run a simple servlet. I made a project on the workspace as you can see on the attached image. And got a code on WEB-INF/src on temp project
    VB Code:
    1. package net.nevyn.brownmonkey;
    2.  
    3. import java.io.IOException;  
    4. import java.io.PrintWriter;    
    5.  
    6. import javax.servlet.ServletException;
    7.  
    8. import javax.servlet.http.HttpServlet;
    9. import javax.servlet.http.HttpServletRequest;
    10. import javax.servlet.http.HttpServletResponse;
    11.  
    12. public class FirstServlet extends HttpServlet{
    13.     public void doGet(HttpServletRequest request,
    14.             HttpServletResponse response) throws ServletException,IOException{
    15.         PrintWriter out=response.getWriter();
    16.         out.println("Hello World");
    17.         out.close();
    18.     }
    19. }
    How do I run the FirstServlet? Can't seem to find an answer. All I found on the net was putting my class to ROOT/WEB-INF/classes on Tomcat's installed folder. Any articles or links for this stuff is very well accepted and thanked.

    Thank you so much.
    Attached Images Attached Images  
    Last edited by nebulom; May 25th, 2005 at 04:41 AM.

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Running servlet

    I think you need to change one of your import statements.
    Code:
    import javax.servlet.*;
    The last two you have are correct though.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Running servlet

    I haven't had the chance to run any servlets, only Bean's which i place in this directory C:\Jakarta\jakarta-tomcat-5.0.28\webapps\ora\WEB-INF\classes\com
    and .jsp pages that are in the following C:\Jakarta\jakarta-tomcat-5.0.28\webapps\ora. I think both are executed a little differently though.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Running servlet

    The thing is I want to test it not on my Tomcat's installed directory/webapps/ROOT/WEB-INF/classes but on my Eclipse's workspace/project/WEB-INF/classes. It's ok though if I just copy the class on Tomcat's installed directory/webapps/ROOT/WEB-INF/classes. But is there any work around with running it not on Tomcat's installed dir? Thanks, D

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Running servlet

    To be honest i really don't know. I mainly write my code in EditPlus and if it's jsp code, when im finished, i just drop it in the proper Jakarta directories and execute via my browser.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Running servlet

    I understand, and I thank you. Really, thanks.

  7. #7
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Running servlet

    Dosen't Eclipse their own forums? Maybe you could post over there and see if you get an answer. I know im always over at Sun's forum's or MySql's when i need to get specific help.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Running servlet

    Sorry for the late reply D, I can't seem to find Eclipse forum. I just registered to Suns' forum but quite not comfortable on posting there, so I just decided finding it myself and hopefully if I can, I'll post it here.

    I just love VBF. Ehehe.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Running servlet

    Ok, I just found out it has to do with the web.xml in our project or what they call the Deployment Descriptor. Also to be set on server.xml as the Contextpath to our project. Sample is like this... First structure our project like this
    VB Code:
    1. C:\projects\java\test
    2.                     + WEB-INF
    3.                             + src
    4.                             + classes
    5.                             + lib
    6.                             + web.xml
    Though lib is optional and might not be putting our code on src folder but this is just a test, so we do it that way. In the src folder we might have a class that extends HttpServlet that might look like this
    VB Code:
    1. import java.io.*;
    2. import javax.servlet.*;
    3. import javax.servlet.http.*;
    4. public class webtest extends HttpServlet{
    5.     public void doGet(HttpServletRequest request,
    6.     HttpServletResponse response) throws IOException,ServletException{
    7.         response.setContentType("text/html");
    8.         PrintWriter out=response.getWriter();
    9.         out.println("<h1>servlet test</h1>");
    10.     }
    11. }
    Just a test so what matters for now is just the name of the servlet. Compiling this with javac -d ../classes webtest.java will have a webtest.class under the classes folder. Now, we have to define the descriptor of our project. In our web.xml we might have something like this
    VB Code:
    1. <?xml version="1.0" encoding="ISO-8859-1"?>
    2. <!DOCTYPE web-app
    3.      PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    4.     "http://java.sun.com/dtd/web-app_2_3.dtd">
    5. <web-app>
    6.     <servlet>
    7.         <servlet-name>webtest</servlet-name>
    8.         <servlet-class>webtest</servlet-class>
    9.     </servlet>
    10.     <servlet-mapping>
    11.         <servlet-name>webtest</servlet-name>
    12.         <url-pattern>/webtest</url-pattern>
    13.     </servlet-mapping>
    14. </web-app>
    That is to inform Tomcat we have a servlet named webtest and we want to access it using the url pattern (/webtest). The question now is how the heck does Tomcat know where is that thing. In our %CATALINA_HOME%/conf folder, we have a server.xml that is needed in Tomcat's configuration. We have to add something... In this case we might be adding it on the last line under the <Host> tag
    VB Code:
    1. . . .
    2. <Context path="/webtest" reloadable="true" docBase="C:\projects\java\test"/>
    3.  
    4.       </Host>
    5.  
    6.     </Engine>
    7.  
    8.   </Service>
    9.  
    10. </Server>
    The . . . just above the <Context path tag implies that there's something up and that's just the default config tags provided by Tomcat.

    By then we will be able to access the servlet like this http://localhost:8080/webtest/webtest. The dark orange signifies the project and the red for the servlet. This btw, assumes we have our port listening to 8080 (though here we have 8888, recommended by our seniors, hehehe, can't do anything but to follow). Running this will have a header(1) labeled servlet test.
    Last edited by nebulom; Jul 1st, 2005 at 04:54 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width