Running Servlets! Tomcat and JDK configs.

Though this post is worthless for a developer who is in touch of these at priori, but it matters much for the newbies who do it without any help and succeed. That newbie is of course, i am!
Well.. past a week, Ritu (one of 2nd yrs.) asked me about the problem she was facing with the Java servlets and mapping. As i was aware of running only simple Java programs (of cors, without any help of apache), i made some permanent changes in the system environment path variable so that she might not face any problem with the paths of the servlet or java codes. For mapping purpose, i was unable to instruct her anything because it was quite a new thing for me (Goddamnit… 😦 i never tried these things even on my UbuntuBox). Yesterday, i was pinged by Amit (another 2nd yr. guy) for more or less the same problem. So, i thought that it would be better if i somehow do it myself on my box and then on others. After such a long time, i prepared myself to boot windows and do the things as required. I uninstalled the previous JDK and downloaded the fresh one from Sun’s site. I already had Tomcat 5.5.20 downloaded from DC++ when i was in BHU (Thanks to IT-BHU guys..they share whole lot of things whether you require it or not), though never used before purposefully. If you don’t have, download it from Tomcat’s site.
I installed Java Development Kit followed by Tomcat with its default settings. Started the server manually, and tested it by typing http://localhost:8080 (i used firefox…i feel people get stuck when they try it on Internet Explorer, without changing the port 8080 to 80. Microsoft itself has it’s IIS). The index page was shown, i felt a bit relaxed as apache was running successfully.
I also explored the documentation a bit.
Thereafter a bunch of tragic things persistently came to bother me. Let’s chuck them off and see some easy steps to make the things work:

a) I turned on servlet reloading, by editing install_dir/conf/context.xml and changed

    <Context>

to

    <Context reloadable="true">

b) Enabled the invoker servlet which lets us run servlets without first making changes to our Web application’s deployment descriptor (i.e., the WEB-INF/web.xml file)
To enable the invoker servlet, uncommented the following servlet and servlet-mapping elements in install_dir/conf/web.xml.

    <servlet>
        <servlet-name>invoker</servlet-name>
        <servlet-class>
          org.apache.catalina.servlets.InvokerServlet
        </servlet-class>
        ...
    </servlet>
    ...
    <servlet-mapping>
        <servlet-name>invoker</servlet-name>
        <url-pattern>/servlet/*</url-pattern>
    </servlet-mapping>

c) Created a Development directory in C:, where i could write all the servlet codes and compile them sideways.

d) Then arrived a situation when i had to set the CLASSPATH in a batch file named autoexec.bat in C: drive itself (where the development directory lies). OMG! i edited this file with the following approximately 20 times when i got success:

set CLASSPATH=.;
C:\Servlets+JSP;
install_dir\common\lib\servlet-api.jar;
install_dir\common\lib\jsp-api.jar

I Googled to find out this link which says these lines should be in one line. Servlets+JSP is the directory i had just created.
i saved the autoexec.bat file there in C, and put a sample servlet code in the development directory. Compiled it with javac, only to find some errors which said that javax.servlet.blah blah is not found.
I was seriously not enjoying the thing as it was turning bad to worse after doing any changes anywhere. As a newbie..i uninstalled the jdk, reinstalled it with no changes, but compilation failure was a must. I left no stone unturned last night around 3 am to serve the purpose but, in vain before i slept. Today, in the evening, i was trying my luck to do the same thing. Did everyting well with keeping my mind cool, and edited the autoexec.bat file once again with a slight change of spacing between the semicolon (;) and the next path. Saved it, and tried to compile the servlet code once again, i was amazed to see that the code was compiled successfully leaving the .class file there in the development directory.

e) and then came the final step of running the servlet in the browser. Before doing this i placed this .class files into the folder install_dir/webapps/ROOT/WEB-INF/classes. This is a need. put http://localhost:8080/servlet/HelloServlet in the browser’s address bar, hit enter and saw the servlet running in my browser with a big Hello.

This was certainly one of the best experiences i ever had on my PC as i have heard people finding it very difficult to do what i have done in less than two days.
Whole credit goes to my juniors…!

Advertisements

3 comments on “Running Servlets! Tomcat and JDK configs.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s