CodeIn House

  • Laravel
  • WordPress
  • jQuery
  • Javascript
  • Contact
Home   JAVA   JSP Fundamentals – Implicit Objects in JSP

JSP Fundamentals – Implicit Objects in JSP

September 3, 2022 by SNK

Implicit Objects in JSP

Today we are going to learn core fundamentals of JSP which helps you to create web pages in java by making use of objects in JSP. If you don’t know about these implicit server objects, its nearly impossible to create web pages in core java.

So, today we are going to learn about Implicit objects in jsp and its use cases to see how they are used in the code.

These are most commonly used implicit JSP server objects that are given to us for free by java that we can use in our JSP page :-

  1. request – Contains HTTP request headers and form data
  2. out – JSP writer which echoes out and print content in the HTML page
  3. session – Provides a way to save unique session for user. Mostly used for creating login and authentication.
  4. application – Provides shred data for all users of the web application.
  5. response – Provides support for HTTP Response.

Now we are going to view the use cases of each object and see how can we use it in our project. So before that i hope you have made your jsp project ready by now.

First we should know JSP expression and how can we use it in our JSP page. They are as follows :-

  1. JSP Expressions.
  2. JSP Scriptlet.
  3. JSP Delarations.

JSP Expressions is to print out the expression in a line. For example you want to print date in jsp using the expression, you can do it this way <%= java.util.Calendar.getInstance().getTime() %>. Another use case could be <%= 25 * 4 %> which will print the result of the expression.

JSP Scriptlet is used to write the block of code inside the JSP page. Take a look at the example below.

Scriptlet
Java
1
2
3
4
5
<%
for(int i=0; i<10; i++) {
  out.println(i);
}
%>

JSP Declarations  allow your to declare a method in the JSP page. For e.g you create some method that returns some string or a number. Then use that method in expression.

JSP Declarations
Java
1
2
3
4
5
6
7
<%!
    String printName(name) {
       return "Printing some awesome text by website " + name;
    }
%>
 
<%= printName("codeinhouse") %>

We can also call the method directory from the class. Let’s we have a class named Commons.java inside com.example.jspdemo package. The simple class would be

Commons.java
Java
1
2
3
4
5
6
7
8
package com.example.jspdemo;
 
public class Commons {
 
    public static String printSomething(String text) {
        return "Testing something awesome by" + text;
    }
}

Now you can call the method in your jsp page by doing <%= com.example.jspdemo.Commons.printSomething("codeinhouse") %>.

1. Request

Request is used to  send over the header information and body information from browser to jsp page. let’s say form data where you send name email and phone to jsp page and do some processing and give the output the user like your user info is subsmitted. Take a look at the example below.

Request usage sample
Java
1
2
3
4
5
6
7
8
<h1>Request Object Usage Sample</h1>
<form action="#" method="POST">
    First Name: <input type="text" name="firstName" />
    Last Name: <input type="text" name="lastName" />
    <input type="submit" value="submit" />
</form>
<%= request.getParameter("firstName") %>
<%= request.getParameter("lastName") %>

You can also use short hand to get the items of the form by typing ${param.fistName}

2. Out

Out is the method from JSP writer class which helps us to print something on jsp page. Simple example would be <% out.println("Print something awesome") %>

3. Session

Sessions are the user specific values or actions that are recorded in the web server by a user. Its mainly used to store the user specific values in the server. In General sessions are used to create the login authentication and authorization system in the web application.

Below we are going to have simple demo on how to create a session in JSP page.

JSP Session Object
Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<%
    session.setAttribute("firstName", "John");
    session.setAttribute("lastName", "Doe");
 
    // To Retrieve session values;
    String name = (String) session.getAttribute("firstName");
    String lastName = (String) session.getAttribute("lastName").toString();
 
    out.print(name);
    out.print(lastName);
 
    // All the user info can also be saved inside one property for e.g
    HashMap<String, String> userMap = new HashMap<>();
    userMap.put("firstName", "John");
    userMap.put("lastName", "Doe");
    session.setAttribute("userInfo", userMap);
 
    // Ways to obtain the value
    HashMap<String, String> userInfo = (HashMap<String, String>) session.getAttribute("userInfo");
    out.println(userInfo.get("firstName"));
    out.println(userInfo.get("lastName"));
%>

4. Application

JSP application object is of the type servletContext. It is created by the web container once when application or project is deployed on the server. It holds the parameters needed for the application at runtime.

Basically these configuration is stored in web.xml file which is created while creating the JSP project. This is also called deployment descriptor file which holds the configuration which can be accessed by every single page in the project by using the applicationContext.

We will checkout the simple demo on to see its use case below :-

Open your deployment descriptor file web.xml and inside <webapp ..........></webapp> add the initial parameters.

web.xml
XHTML
1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
         version="5.0">
 
    <context-param>
        <param-name>databaseName</param-name>
        <param-value>aNameOfYourDatabase</param-value>
    </context-param>
</web-app>

So, to use this inside your application, we are going to use application object.

1
2
String databaseName = application.getInitParameter("databaseName");
out.print(driver); // aNameOfYourDatabase

This is just on example. There are tons of methods inside the applicationContext where you can use in your project to initParameters(), getRealth(), getContextPath(). Most of these methods returns string. You need to check and see what it returns.

5. Response

Response object comes from the HttpServletResponse which web container creates the instance at the runtime. It gets created each time the request is made from jsp page to the server. This response may contains data that is being requested fromt the server.

This response object can be used to handle response related task such as redirecting to different page after making a request or sending error messages and many other things. I hope you could explore more methods yourself.

Response
Java
1
2
3
<%
    response.sendRedirect("login-page.jsp");
%>

So, these are the most important JSP objects that you need to know while developing web application with JSP. If you want to explore more always head to documentation to checkout more methods inside the objects to check what else we can do with it.

SHARE ON
Buffer

Enjoyed this article?

Like us on

JAVA implicit objects in jsp jsp implicit objects jsp life cycle

Avatar for SNK

About SNK

Hello Welcome to my Blog. I develop Websites Using Laravel Framwork & WordPress. Get Latest updates on Facebook | Twitter

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get Connected !! With Us

TOP POSTS

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

TUTORIALS TILL DATE

  • September 2022 (6)
  • June 2021 (7)
  • October 2020 (5)
  • September 2020 (6)
  • September 2018 (14)
  • August 2018 (3)
  • July 2018 (4)
  • March 2018 (8)
  • February 2018 (5)
  • January 2018 (1)
  • December 2017 (2)
  • August 2017 (8)
  • May 2017 (1)
  • April 2017 (1)
  • March 2017 (4)
  • February 2017 (3)
  • January 2017 (4)

CATEGORIES

  • Angular (2)
  • CSS3 (3)
  • D3 (3)
  • HTML5 (7)
  • JAVA (11)
  • Javascript (20)
  • jQuery (8)
  • Laravel (35)
  • Others (3)
  • PHP (11)
  • Spring (2)
  • WordPress (10)

Top Categories

  • Angular
  • CSS3
  • D3
  • HTML5
  • JAVA
  • Javascript
  • jQuery
  • Laravel
  • Others
  • PHP
  • Spring
  • WordPress

Get in Touch

DMCA.com Protection Status

Recent Articles

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

© 2012-22 CodeIn House.  •  All Rights Reserved.