This free online course will teach you how to create a JSP page, and how to use the Model 2 Design pattern to build extensibility and scalability into your J2EE web applications.
Course Prep:
- Read Chapter 3 in the Core Servlets and JavaServer Pages textbook.
- View the JavaServer Pages Powerpoint slides (also available in pdf format here).
If you have not worked with servlets before, then I highly recommend that you start there first. JSP pages are compiled into servlets by the web server, so it's very important to understand how servlets work under the hood before trying to work with JSP pages.
Course Overview:
You'll first convert the servlet from the J2EE Course - Building Servlet Web Applications to a JSP page with the same functionality. The resulting JSP page will be small compared to the servlet code, and you should start to see how easy it is to create JSP pages with a line or two of Java inserted between the HTML tags.
The second part of this course starts us down the path of splitting out business logic from display logic - which is a very important distinction to understand. Business logic is anything that relates to the business that the application represents. In this case, we're building a financial application, and so the business logic is defined inside a method called getAccountBalance that resides in a Javabean object. The display logic is all that should be inside a JSP page, and the JSP page should merely call out to the Javabean to execute business logic and return the account balance.
This leaves us with a nice separation of concerns, and a Model2 design pattern based on the familiar Model-View-Controller pattern that will serve us well. For example, in future courses when we hook up the getAccountBalance method to an actual database, the JSP page should not have to even know about this change... it will still just happily serve up HTML back to the browser, and won't have to worry about how the getAccountBalance method does its dirty work.
This is also a good way to scale an application. You can start small (and fast) with a regular Javabean accessing the database directly, and then as the number of users increases you can swap in EJBs behind the getAccountBalance method, and nothing in the display logic has to know or care about this change.
Course Exercises:
PART 1:
- Create a JSP page called Login.jsp that has the same functionality as the LoginServlet from the J2EE Course - Building Servlet Web Applications.
- Download the pre-built HTML account login form: login.html. (NOTE: right-click on the link and select "Save Target As"). Place login.html in the root folder of your Tomcat project (the same location as index.html from the Creating a Tomcat Web Application course).
- Change the HTML form action in login.html to post to Login.jsp instead of servlet/LoginServlet.
- Include a file at the bottom of login.jsp that will act as the page footer, such that it is included at the time the page is requested. You may use a pre-built HTML page (footer.htm), or create your own, but it must be a different page than your JSP page.
- Package the web application as assign04.war (or any file name with a .war file extension), deploy it to Tomcat, and run it in your web browser.
- Refer to the Creating a Tomcat Web Application course for instructions on how to package a web application into a war file.
Check your work: assign04_Solution.war
PART 2:
- Create a JavaBean called AccountBean.java that resides in a com.financials package. It should have public get and set methods for UserName and Password properties, and a parameter-less getAccountBalance() method that returns a hard-coded float value (to simulate a database hit).
- Convert the web application from Part 1 to use the Model 2 design pattern. The end result will include the following web components:
LoginServlet.java - controller component that will 1) get the username and password request parameters and will set properties on the AccountBean for each of them, 2) store the AccountBean in the Request as an attribute using the request.setAttribute() method, and 3) forward the request to Login.jsp using a RequestDispatcher.
Login.jsp - view component that will access the AccountBean stored in the Request as an attribute.
AccountBean.java - model component described above.
- Package the web application as assign04MVC.war, deploy it to Tomcat, and run it in your web browser.
Check your work: assign04MVC_Solution.war
How and where do I create a JSP?
how and where do I create a .jsp?
I thought they were like servlets, which in the servlet course I created a java class and put
that under the WEB_INF folder. When I tried that with the jsp assignment, it wouldn't let
me enter the .jsp at the end of the name. So I must be creating it incorrectly.
Please help...
Create JSP pages at the root of your project
You'll create a new file in the same location as the login.html page...
at the root of your project folder. Give the new file a .jsp file
extension.
Rick