BreadcrumbsHome / JSP Tutorial #12 – Java Server Pages Tutorial – JSP Built-In Server Objects
JSP Tutorial #12 – Java Server Pages Tutorial – JSP Built-In Server Objects
Last Updated on Thursday, 10 December 2015 Written by Chad Darby Friday, 13 November 2015
In this video, I show you how to use the JSP Built-In Server Objects.
Please SUBSCRIBE to my YouTube channel.
Do You Need More Details?
- How to read form data with JSP?
- Develop MVC application with JSP and Servlets?
Read more about the premium course at http://www.luv2code.com/jsp
Follow luv2code with the links below:
JSP Tutorial Transcript:
There’s two others here, session, it’s a unique session for each user. You can think of the Session Object as like the shopping cart, each user has their own unique shopping cart, we will use this on the following video. Finally there’s the Application Object, this is shared for all users of a given web app and I’ll discuss various use cases on how to use the Application Object. Just for disclaimer, this is not an exhaustive list, there’re other server objects out there but these are the ones that are commonly used for JSP web apps. Here’s the idea, you have the browser communicating with the JSP, behind the scenes the HTTP protocol, they actually send our request object, this request object contains header information and body information. JSP can perform some work on that information and then send back a response, so we have the whole request response protocol going. In the demo I’ll show you how to make use of the request object.
Let’s go ahead and move into Eclipse, what I wanted to do is continue to use the existing project JSP demo and I would like to move down to the folder your web content because we’re going to create a new file. I just say right click, I say new file. The name I’ll give here, I’ll call it builtin-test.jsp because we’re of course testing some of the built-in objects. Once you’re happy with the name we’ll go ahead and click finish. Let’s expand the window here for a second and I will bring file and start writing some code. Again, I start off with my HTML and my body information. I’ll just set up a header three just to give us a little name here for the page. Now what I want to do is, I want to read some information about the request so I want to find out what type of browser the user is using to access my JSP page. You can access this information using the request object.
So I can say, request.getHeader and then you get the header name of user-Agent. This will tell you the actual browser that the client is using, also it will tell you the operating system that they’re running on. Let me go ahead and add some line breaks here. I want to find out what language the client is making use of in their browser so if they’re using english, Spanish, German, French. Here I say request.getLocale and this will get me the actual locale that the user has set up on their system. We can use this to customize the type of pages we send the user based on their actual locale, like English or German or Spanish. That’s basically it for this very basic demo here. Go ahead and save your file and let’s go ahead and run this program. I move to the file builtin-test.jsp, I’ll do a right click and I’ll say run as and then run on server so the application runs.
Note here request user-Agent so they will tell us the type of browser that’s coming through. Here we’re using this internal browse that’s built-in as part of this Eclipse tool but note it also says that we’re using Mac OS X so we know the operating system and the browser that the user’s running on. Let me copy this information and put it into a different browser, I’ll drop it in the Chrome and I’ll just paste it. I’ll hit enter and so now we get the same information but note here it says Chrome so they give us the actually Chrome browser that we’re using and the version, Mac OS and also there request language is English. Again, you could possibly use this to customize the different pages you want to send back to the user based on their browser type, OS type or their language type. Good job, an example of using that built-In Request Object.
Let’s go ahead and wrap up this video, in this video I’ll show you how to use some of the Built-in Server Objects. We may use the request object to find out the user’s operating system and browser. In the following videos we’ll do a drill down into some of the other objects and you can come and see them working out real world for various use cases.