Friday, March 21, 2014

Eclipse + JavaServer Pages (JSP) beginner's tutorial

Eclipse is a widely used open-source integrated development environment (IDE).






Visit the Eclipse download page. Find the download for "Eclipse IDE for Java EE Developers". Select the appropriate version for your computer (it must match the JDK/JRE you have installed)!






Extract the archive's contents. There is no need to run any setup program.






Navigate to the newly extracted 'eclipse' folder and run 'eclipse.exe'.











The workspace directory is where Eclipse will store all your projects.






Let's make a new project! Click "File", then "New", then "Other..." (or press Ctrl+N).






If you don't see "Dynamic Web Project" the most common cause is because you did not download the "Java EE" edition of Eclipse.






Since this is the first JSP project, Eclipse doesn't know about the Tomcat installation. After configuring a "New Runtime" it should be available in the dropdown menu for future use.






Don't check "Create a new local server".






Remember where you installed Tomcat? Enter that path or browse to it.






Tomcat should now be an option :^)






A blank canvas awaits you...






Let's make our first JSP page. Right click on the 'WebContent' folder.






We want JSP - not HTML - since we are going to have some dynamically generated content. Although there is nothing wrong with using pure HTML files for static content.






Recall that files named 'index.*' will be the default "home page" served up by the webserver.






There are some predefined templates available. You don't have to use them, but they are convenient.






Enter the example code highlighted in yellow. What do you think it will do?






Click the green run button on the toolbar so we can find out.






Once you confirm your selection here, Eclipse shouldn't ask you again for this project.






Create a new Tomcat server on your localhost for development of this project.






Your project will be built and a Tomcat server will be started (see the "Servers" tab.)

Your default page will be loaded inside Eclipse's internal web browser!

If you see "4" (the result of "2+2") then you have successfully generated a dynamic page!






Browse to your local Tomcat instance in a "real" web browser.






View the source code of the page.






Check it out... you only see the result of the code executed between the start and end of the JSP tags. The actual source code is not revealed to the client.






Shut down your Tomcat server from within Eclipse by clicking the red stop button.







Further Reading


There are a number of JSP tutorials (of varying quality) freely available online.

Here's a decent one I stumbled across: http://www.jsptut.com/

I definitely suggest you read both tutorials and a JSP reference to understand the features, power, and flexibility of the language. Remember: it doesn't matter if you are outputting HTML, XML, JSON, whatever... it's all delivered over HTTP.

That's it! You're done

No comments:

Post a Comment