In this tutorial we are going to talk about two things:
- JSP Built-in Server Objects.
- How to include files in JSP?
Built-in Server Objects
Now, these objects are given to you for free, it’s not like you have to go ahead and pay to use them (like you do for some APIs), so there is no need to create them, you can just use them in your JSP pages. Here are a couple of the most commonly used server objects:
- Request: Contains information about the HTTP request headers and the form data.
- Response: This object is used for sending the HTTP specific response for the received request.
- Out: This object can be used for adding data to the HTML page using “out.println”.
- Session: This object is used to maintain sessions for all the users of your web application.
- Application: Shared data for all the users of your web application.
All of these implicit Objects maps to something from the Servlet/JSP API.
You’ll just have to research the object you require and then use it in your JSP Page.
Now you have a regular browser communicating with your JSP page. The browser will send an HTTP request object to the JSP. This object will contain the header and body information. The JSP will process this information and send back the response, creating a proper request-response communication channel.
Coding the Object
Now let’s take a look at how to use the request object. We’ll use the existing project.
- In the Web-Content folder, create a new file and name it whatever you want. Since we are trying to test the built-in objects I’ll be naming the file “builtin-test.jsp”. Name your file and click finish.
- Now populate the file with the following code snippet:
<html> <body> <h3>Testing the Request Object</h3> Request User Agent: <%= request.getHeader("User-Agent")%> <br/><br/> Request User Language: <%= request.getLocale()%> </body> </html>
- Now go ahead right click on the file and run as “Run on Server”. This will show you the requested information i.e. the user’s environment (their browser and a bit of their hardware info) and the language on their system.
Request User Agent: <%= request.getHeader("User-Agent")%>
In the first object call we are getting the user’s environment information through the header. You can see that I am using the built-in explorer for chrome and that I am on a Mac machine running OSX 10.11.1.
Request User Language: <%= request.getLocale()%>
In the second call, we are getting the locale set up on the User PC so that we can provide them with a customized experience on every page.
Just refer to the official documentation to get to know about your required objects.
Including Files in JSP
A common use case for including files in JSP is for including common headers and footers. For instance, if you are making a website and want the same information to be displayed on the header and the footer throughout, you can make separate files and include them in your JSP pages. And you can include “html” as well as “JSP” files in your jsp file. Here is how you do it:
“<jsp:include page=”my-header.html”/>” “<jsp:include page=”my-footer.jsp”/>”
Now let’s try this out.
Coding the files to be included
In the same project, under the “Web-Content” folder create a new file named “my-header.html”. Populate the HTML file as such:
<h1 align=”center”>JSP Tutorial</h1>
Now let’s move ahead and create the footer. Under the “Web-Content” folder create a new file named “my-footer.jsp” and click finish. Now populate the file as such:
<p align=”center”> Date: <%= new java.util.Date()%> </p>
Save this file.
Coding the main page
Now that we have our files ready, let’s move on to code the main page which will include all these files. Under the Web-Content folder, create a new file by the name of “main-page.jsp” or “homepage.jsp”. After naming the file, click on Finish. I’ll show you how to include the files.
<html> <body> <jsp:include page="my-header.html" /> Blah blahblah .... <br/><br/> Blah blahblah .... <br/><br/> Blah blahblah .... <br/><br/> <jsp:include page="my-footer.jsp" /> </body> </html>
Save the file. And run it, you’ll see a browser window with information like this:
And we are done with the fundamentals of JSP. In the next tutorial, we are going to start reading data from HTML forms.