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 :-
- request – Contains HTTP request headers and form data
- out – JSP writer which echoes out and print content in the HTML page
- session – Provides a way to save unique session for user. Mostly used for creating login and authentication.
- application – Provides shred data for all users of the web application.
- 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 :-
- JSP Expressions.
- JSP Scriptlet.
- 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.
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.
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
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.
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.
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.
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.
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.
Leave a Reply