Variables are what programmers use to store simple values. These values are dynamic, and can be easily changed in the program. They can be numbers, single characters, bits, and more. But the most commonly used variables are called String variables. Strings store groups of characters, and thus can be used to store words, sentences, and more.
By using strings, you can store information in your page that can be easily accessed anywhere else in that page. For example, when you begin using included files, you will soon notice that now every page has to have the same title. Using variables can allow you to have control of this again.
In your header.jsp file, change the information between <TITLE> and </TITLE> to this:
<%= Title %>
This will output the value of a variable called Title. Now, in your JSP pages, simply set the variable at the beginning of your page. Your new pageusing the include and the Title variablewill look like this:
<% String Title = new String("Title of this page in quotes."); %> <%@include file="/header.jsp" %>
Page content goes here:
<%@include file="/footer.jsp" %>
This will create a String variable that contains the value you enter in quotation marks. Then, any time you output the Title variable in your page, the value entered there will automatically be output at the appropriate point in your HTML page. Remember to create a title for every page, or "null" will be output as the page title.
You can use variables to control a number of other elements in the page, including the user's name, style information (you could let the user pick a color and then save the hexadecimal RGB value in a variable), email addresses, and more.
For the advanced user, JSP allows you to store variables in different scopes. The variables mentioned here exist only in a single page, but once you go to the next page, that variable will need to be re-created.
JSP has a session scope that stores variables that relate to a user, and it can be accessed any time during that user's visit. There is also an application scope that stores information that can be accessed by any page within that entire JSP application.