JSP simplifies view rendering. In this module, we will explore JSP implicit objects, scripting elements, validations, and database logins in JSP.
4.1. JSP Implicit Objects & Scripting Tags
Definition: Implicit objects are pre-declared container objects available to developers inside JSPs. Scripting tags allow embedding Java variables and statements.
Core Working Principles:
- Directives (
<%@ ... %>): Page-level configuration tags (e.g. imports). - Declarations (
<%! ... %>): Declares variables or methods inside the servlet class body. - Scriptlets (
<% ... %>): Embeds regular Java statements inside the request handler method. - Expressions (
<%= ... %>): Shorthand forout.print(); must not end with a semicolon.
<%@ page import="java.util.Date" %>
<%! private int count = 0; %>
<% count++; %>
<p>Active session: <%= new Date() %></p>
<p>Visit hit: <%= count %></p>
5-Marks Exam Scoring Points
- JSPs compile into servlets behind the scenes.
- The container injects nine implicit objects (request, response, session, application, out, pageContext, config, page, exception) into the service method.
- Directives set page-level configurations; do not produce visible HTML output.
- Declarations declare variables at the class level; shared across all users (not thread-safe).
4.2. Form Validation & Database Logins in JSP
Definition: Validating user inputs and performing database queries directly inside JSPs to authenticate users.
Core Working Principles:
- Form validation: Extract inputs and validate empty or incorrect fields on the server side.
- JDBC inside JSP: Import Java SQL packages using directives and run validation queries inside scriptlets.
5-Marks Exam Scoring Points
- request.getParameter() extracts inputs by name.
- Imports packages using the page directive:
<%@ page import="java.sql.*" %>. - Validates data and redirects users on successful login, saving username in the session.