Calling the Controller Servlet from JSP Pages
All that remains to complete the conversion of the Project Billboard application from a pure JSP application to an application that uses a mix of filters, listeners, servlets, and JSP pages is to modify the JSP pages to invoke the Controller servlet. To make the application a little bit more interesting, let’s add information about the number of active sessions (loosely, the number of logged in users) to the main page.
By moving all request processing to other components, there are only
three JSP pages left: login.jsp,
main.jsp, and entermsg.jsp.
The single change needed in the login.jsp page
is the form element’s action
attribute:
<form action="<c:url value="/ch19/authenticate.do" />" method="post">
This application uses resources on different levels in the URI structure, and the login page can be invoked directly by the user as well as by a forwarded request for a resource in the protected directory if the user isn’t logged in. The base URI differs depending on how it was invoked—/ora/ch19/login.jsp if it’s invoked directly or /ora/ch19/protected/main.jsp if it’s invoked through a forward caused by an unauthenticated request for the main page. Thus, a relative path as the action value doesn’t work; the browser converts a relative path in a page to an absolute path based in the URI that generated the response, as described in Chapter 17. The solution is to use an absolute path instead. To avoid hardcoding the context path in the page, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access