I'm now attending a Java class in Carnegie Mellon University and I'm now practicing building Java Servlet and JSP by following the textboox--Head First Servlet & JSP.
In the very beginning, I practiced by using text editor to conduct all the codes from textbook. However, I found that it must be more convenient to use Eclipse.
And I have already installed Eclipse 3.4.2 on my Mac. Before I successfully run a simple servlet by Eclipse, there were a bunch of hassles I've encountered.
Therefore, I'd like to write these down so that I won't forget how I overcame them.
The details of my development environment is Eclipse J2EE 3.4.2, Tomcat 6, Macbook Pro,
And the HOME directory of Tomcat is /Library/Tomcat
The obstacle that I encountered today was I cut and paste my old servlet codes from text editor to Eclipse and it gave me the following message:
"The import javax.servlet can not be resolved."
I really spent a lot of time googling a possible solution but in vain.
So I meticulously considered about the error message and tried to figure it out.
It seemed that the Eclipse can not find the servlet-api.jar which is located in /Library/Tomcat/lib
So if I can find a way to add the .jar file into the Eclipse then it must be ok.
Eventually I found the right route which is shown below:
1. Right click on the project name, as shown above, right click the "TestJSP" in the left column. Then choose "Properties".
2. Choose "Java Build Path" in the left column of the pop-up dialog.
3. Choose "Libraries" tag in the right column.
4. Click "Add External JARs" in the right hand side.
5. Navigate to the /Library/Tomcat/lib/servlet-api.jar, then click "Open"
6. There must be a new library called servlet-api.jar appears in the list.
7. Click "OK" in the bottom right, and you're all good to go.