A comprehensive example of a user profile is used in the case study of our fictitious training company in Chapter 9,“Developing Custom Tag Libraries.” For this chapter, we will use a basic profile of our users. It will simply contain their favorite product type. Our demonstration is a variation of the shopping-cart application that you have been exposed to in various chapters in this book. Essentially, the shop provides users with the ability to purchase DVDs, CDs, and books from a Web site.The product information comes from a database. No information has been stored about the users in all the previous applications shown.The application has been changed to also maintain user information in a database and allow people to register with the site.
The User Profile
The user profile is shown by the SQL listing in Listing 17.1.This listing is an extract from the database set up script tablesSetUp.sql in the chapter17Personalization.war demonstration site.
Listing Extract from tablesSetUp.sqlcreate table users (name varchar(50) primary key,
The table called interests in Listing 17.1 is used to contain the users’ core interest, which as you can see is CD, DVD, or book. It could be a great deal more complex, storing all sorts of information about the user, but for this example, we’ll keep it simple.The usernames and passwords are stored in another table called users.
Setting Up Login Using the Database
When the provision of usernames and passwords was discussed in Chapter 16,“Security and JSP,” the usernames and passwords were maintained in the tomcat-users.xml file. This was because we were not adding or modifying user information.We are now using a database because you are storing more than just the usernames and passwords, and you also want to be able to dynamically add users as they register. To configure Tomcat to use your database to contain user information, you will need to add the following entry to server.xml:<Realm className=”org.apache.catalina.realm.JDBCRealm” debug=”99”
This is how Tomcat was specifying to use tomcat-users.xml to contain user information.
If you notice from the additional entry in server.xml, there is a reference to the users table and also a reference to a user_role table.This table maps users to roles, as did the tomcat-users.xml discussed in Chapter 16.We have used a single role called customer, and the SQL for that table is also shown in Listing 17.1.
The Login Page
We need a login page to enable users to log in, and we need a registration form to enable users to register.This is shown in Figure with its code shown in Listing 17.2.
Login and registration form.
You will also know that there must be an entry in the web.xml file. Basically, all the shopping pages are in a folder called shopping, and it is this folder that is being protected by the form-based login. Listing below shows web.xml for our application.Various aspects of this will be referred to as we progress.
Listing web.xml<?xml version=”1.0” encoding=”ISO-8859-1”?>
Note the security aspects toward the bottom of Listing .The <securityconstraint> element and the <login-config> elements are specifying both the login form and also the fact that anything within the shopping subfolder is protected. It is this folder that contains our shopping Web site.
The Profile Object
For the application to access user information in a convenient way, an object used to encapsulate the user information will be used.This is set up for us with the help of a servlet and a Profile bean.The controller servlet acts as the gateway into the Web application after people have logged in.The controller servlet source code is shown in Listing below.
Listing ShoppingCartController.java// this servlet is used as the entry point into the application
The controller servlet has two main functions. It sets up the shopping cart. and it also creates something that we have called the profile object, which represents the current user.The profile object used here is a very basic object in terms of its properties. It contains the username and the preferred product type, which are represented by the two properties, name and interest.The issue to consider is this; how does it know what the interest value should be? The value is within the database, and it needs to be looked up from there.The code is shown in Listing below.
Listing Profile.java// this class represents the current user
Then the profile object is created, the database is accessed and the interest value is obtained. Clearly, there could be many more profile properties. Note that the database access code is within the bean.This could be moved out and placed in a separate bean, but we left it in for simplicity so you could clearly see exactly what is going on.
This profile bean was created in the controller servlet, and then placed into a sessionscoped attribute called currentUser.This means that the pages now have convenient access to this data.The welcome page, welcome.jsp, uses this data for personalization. Figure shows what welcome.jsp looks like when viewed in a browser, logged in as the user called Mel.This user has books as his preferred product type. Notice the circled part of the page.
The circled part of the diagram is personalized to the user based on his preferences. Figure shows exactly the same the page, but for a different user. Both users get different content presented.The source code for welcome.jsp.
welcome.jsp as seen by user Mel.
welcome.jsp as seen by a user Mark.
Listing welcome.jsp<%@ taglib uri=”http://java.sun.com/jstl-el/core” prefix=”c” %>
The most important part of this listing is the last section. It is querying the currentUser.interest property.Which if block gets output is dependent on the value of the currentUser.interest property. Mark preferred CDs and was presented with information about a CD, whereas Mel preferred books and was presented with a book.
Finally, we need to look at how users register with the Web site in the first place.The users that we have been working with were already set up.
Listing 1RegistrationServlet.java// this servlet is used for the registration of new users
Notice that a number of prepared statements are used in this registration servlet to update the database with the new entries.Three tables are modified—the user table, the interests table, and the user_roles table. Note that there is no error checking in this servlet. It has been left out for clarity. A diagrammatic representation of our personalization can be seen in Figure.
Essentially, the page uses rules to decide which content to show based on the user information provided.
JSP Related Interview Questions
|J2EE Interview Questions||Core Java Interview Questions|
|JDBC Interview Questions||Java Servlets Interview Questions|
|Hibernate Interview Questions||JavaServer Faces (JSF) Interview Questions|
|JSTL(JSP Standard Tag Library) Interview Questions||JBOSS Interview Questions|
|Log4j Interview Questions||NHibernate Interview Questions|
|Apache Struts 2 Interview Questions|
Jsp, J2ee, And The Role Of The Servlet
The Basic Syntax Of Jsp
Further Jsp Syntax
Deploying Jsp In A Web Container
The Jsp Standard Tag Library
Jsp And Javabeans
Jsp Application Architecture
Session Tracking Using Jsp
Developing Custom Tag Libraries
Utilizing Xml From Jsp
Transforming Xml Using Xslt And Xslfo
Invoking Web Services From Jsp
Locating Resources Using Jndi
Databases And Jsp
Jsp And Ejb Interaction
Security And Jsp
Personalization And Web Applications
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.