MOBI BOOT CAMP CORP. logoLearning Buddy
  • SIGN IN
  • Introduction
  • 1. Build Tools & Project Structure
  • 2. The Web Layer (Servlets & JSP)
    • HttpServlet
    • Deployment Descriptor (web.xml)
    • JSP & Expression Language
    • Session & Application Objects
  • 3. Design Patterns & Architecture
  • 4. Persistence Foundations (SQL & JDBC)
  • 5. Object-Relational Mapping (ORM)
  • 6. Modern Web Services & Microservices
  • 7. Hands-on Project

Java Server Pages

JavaServer Pages (JSP) is a Java standard technology that is an extension of servlet technology used to create dynamic server-side pages. In fact, a JSP is converted to a Java servlet when it is deployed. The difference is in how we write the code. With servlets, you write Java code in which you can embed client-side markup (like HTML), but with JSP, you write a markup file very similar to an HTML file and can optionally embed Java code through JSP tags or scriptlets.

The main difference between an HTML page and a JSP page is that the entire HTML markup is sent to the client as-is when the client requests it. In the case of a JSP page, the server runs the embedded Java code and sends the result of such execution along with the static HTML markup.

Since the server does some work (executes the embedded Java programs) on the page before it is sent to the client, servers that handle only static pages, like Apache Web Server, cannot serve a JSP page. Only dynamic web servers like Apache Tomcat, JBoss, WebSphere, and WebLogic can serve a JSP page.

Creating a JSP page

Create a JSP file using Eclipse's page creation functionality. Every JSP page ends with the .jsp extension. In a JSP file, however, the first line should be the page encoding directive, as shown below:

<%@ page contentType="text/html; charset=UTF-8" %>

After this line, you can have your regular HTML page markup along with optional Java code embedded in between. It is this embedded Java code that makes the page dynamic.

There are three ways of embedding Java in a JSP page:

  • JSP scriptlet - Just add pure Java in between these tags: <% your java code %>
  • EL (Expression Language) Tags - These tags simplify coding and are available right off the bat without adding any libraries. EL tags help traverse Java model components with simple constructs.
  • JSTL (JavaServer Pages Standard Tag Library) Tags - Similar to EL tags, these provide more sophisticated features that include iteration/conditional constructs, XML processing, text formatting, database access, etc. You have to add the JSTL library to your pom.xml or lib folder and also add the taglib directive in your JSP file to use them.

Reference:

  • https://docs.oracle.com/javaee/5/tutorial/doc/bnake.html
  • https://www.oracle.com/application-development/technologies/jdeveloper/jstl-el-adf.html

Ensure this line is added at the top:

<%@ page isELIgnored="false" %>

This enables EL expressions to work.

The following implicit objects are readily available in JSP:

  • request - HttpServletRequest object
  • response - HttpServletResponse object
  • session - HttpSession object
  • out - JspWriter object to write the output as part of the HTML file
  • config - ServletConfig
  • application - ServletContext
  • pageContext - PageContext
  • exception - Throwable

Example Usage

<% String a = (String)request.getAttribute("test"); %>
<%= a %>
<% out.print(a); %>

Implicit Objects Available in EL Expressions

  • pageScope - Maps the given attribute name with the value set in the page scope.
  • requestScope - Maps the given attribute name with the value set in the request scope.
  • sessionScope - Maps the given attribute name with the value set in the session scope.
  • applicationScope - Maps the given attribute name with the value set in the application scope.
  • param - Maps the request parameter to a single value.
  • paramValues - Maps the request parameter to an array of values.
  • header - Maps the request header name to a single value.
  • headerValues - Maps the request header name to an array of values.
  • cookie - Maps the given cookie name to the cookie value.
  • initParam - Maps the initialization parameter.
  • pageContext - Provides access to many objects like request, session, etc.

Example Usage

<h1>${param.name}</h1>
<h1>${param['name']}</h1>
Privacy Policy | Terms & Conditions