Hello All,
This article is supposed to give a nice and brief overview over the basics that have to be gone through and considered when you want to create your first Java Server Page or in short JSP:
General Information
- “Remind me, What was a JSP again?”: A JSP is an HTML page with the possibility to (some extend) include Java-Code.
Creating Your First JSP-Page
Preliminaries
- Set-Up a Tomcat/JBoss (For detailed information on how to do so see Setting up Tomcat or JBoss)
- Create an appropriate (project) File-Directory-Structure as follows:
- <project-folder> (This can have any name, depending on what you call your project)
- <accessible_folders_files> (This should be files that need to be accessible from clients, e.g. your jsp-files or more generally your View)
- WEB-INF (!This folder is mandatory! – It also has the nice property, to “hide” what is inside of it and its sub-folder strucutres*)
- web.xml (!This file is mandatory!)
- lib (should you use any external libraries, that need to be accessible by your code, then store them here)
- classes (if you should extend your JSPs with JavaBeans, you should store the respective classes here)
- <_anything-else_>, as I already mentioned above, you can store any other folders and files within the WEB-INF folder (even a jsp folder with your source files (which you shouldnt for safety reasons*)) and it would not be visible to an external accessee
Example
- HelloWorld
- jsp
- hello.jsp
- WEB-INF
- lib
- javax_jsf.jar
- classes
- beans
- HelloWorldBean.class (NOTE: in our minimal example (Implementation) we won’t include the HelloWorldBean for simplicity)
- beans
- sources (*)
- beans
- HelloWorldBean.java (NOTE: in our minimal example (Implementation) we won’t include the HelloWorldBean for simplicity)
- lib
- web.xml
- jsp
- <project-folder> (This can have any name, depending on what you call your project)
- Move the File-Directory to the right spot
- “And where might that be?”: It depends:
- For Tomcat that is (01.10.2010): $TOMCAT_HOME_FOLDER$/webapps/
- For JBossthat is (01.10.2010): $JBOSS_HOME_FOLDER$/server/default/deploy/
- NOTE: The $ annotated parts represent variables, please insert your respective (here) folder or (generally) value there.
- So for our example (assuming we use Tomcat) the path could look like this: C:\Program Files\Tomcat_7.0.2\webapps, whereas the part in bold would be = $TOMCAT_HOME_FOLDER$.
- “And where might that be?”: It depends:
Implementation
- Create a .jsp file, e.g. hello.jsp from our example in the Preliminaries subsection.
- “How does that look like?”:
<html> <body> Hello World! <br/> I can count! 1 + 1 equals <%= 1 + 1 %> </body> </html>
- NOTE: More about JSPs can be read in Thomas Stark’s Java EE 5, the above example uses a JSP-Expression, we further have JSP-Scriplets (<% %>), JSP-Declarations (<%! %>) and JSP-Directives (<%@ %>), which are all explained in detail in that book.
- “How does that look like?”:
- Create a web.xml file
- “What for?”: Basically the web.xml file – also called the Web Deployment Descriptor – is the configuration for your particular project. For JSPs however, you don’t need any particular configuration information, since unlike for Servlets your Tomcat or JBoss translates the JSP file into a HTMl file all by itself without the need for any e.g. mappings (as it is the case for Servlets).
- “Ok, but what does “dont’t need any particular information mean?””:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name> Eine erste Webanwendung </display-name> </web-app>
Reflection
- Have a peek at your work.
- Start either one of the servers:
- For Tomcat that is (01.10.2010): $TOMCAT_HOME_FOLDER$/bin/startup.bat
- For JBossthat is (01.10.2010): $JBOSS_HOME_FOLDER$/bin/run.bat
- Then access the generated (by the server) the HelloWorld.jsp file:
- By typing the following URL: http://localhost:8080/HelloWorld/jsp/hello.jsp into your browser’s address bar.
- Congratulations you are looking at your first JSP-File :)!
- Start either one of the servers:
* Please NOTE: Even though WEB-INF is secured against external access within the Container, it is still accessible by the underlying filesystem! So DO NOT store your source files there!
Best Regards,
David