|
Beginner’s Guide to Web Development – JSP Fundamentals
This part of the guide focuses on the fundamentals you will need to learn in order to write
functional JSP's. These fundamentals can be broken down into scripting elements, attributes, and built-in variables.
SCRIPTING ELEMENTS
There are three types of scripting elements you need to be aware of. They are expressions, scriptlets, and declarations.
Below are some examples.
EXPRESSION:
An expression is used to print values directly to the screen. Here is the syntax:
<%= [java expression goes here] %>
Here is some example code:
<%= "hello" %>
The above code simply prints hello to the screen.
SCRIPTLET:
A scriptlet is a block of java code. Scriptlets are what we've used in our JSP examples thus far.
Here is the syntax:
<% [java code goes here] %>
Here is some example code:
<% String s = "hello";
out.print(s);
%>
The above scriptlet also prints hello to the screen.
DECLARATION:
A declaration is a special form of scriptlet that allows you to define variables or methods
outside the normal context of a JSP page. We'll talk more about methods later on. Here is an example of
a variable declared inside a declaration:
<%! int x = 44; %>
ATTRIBUTES
There are two attributes we need to go over here since we will be using them later on in the tutorial. They are the import
attribute and the errorPage attribute.
import:
The import attribute allows you to reference Java classes in your JSP code. For example, if you wanted to write to a file
from a JSP page, you would need to use classes found in the java.io package. To use classes in the java.io package, you need to
import that package. This can be done with the import statement. Here is the syntax:
<%@ page import="package" %>
An example of importing the entire java.io package would be the following:
<%@ page import="java.io.*" %>
errorpage:
The errorPage attribute allows you to specify a page to process any errors that are thrown on
your current page. This can be used to display a user-friendly error message in case of a page problem. Here is the
syntax:
<%@ page errorPage="URL" %>
An example of using an error page that you created and entitled error.jsp is the following:
<%@ page errorPage="error.jsp" %>
BUILT-IN VARIABLES
There are many built-in variables in JSP. We've used some of these already. Here is an overview of all
the built-in variables you will need to be familiar with for this guide.
out
The variable "out" is used to print output to the web page. "out" is often used inside scriptlets to
print values of stored data into the HTML code.
request
The request variable gives you access to information contained in the HTTP request. We've used the getParameter method
of the request object. This method gives you access to information submitted to your page. Access to HTTP header information,
protocol information, form method type, etc. can also be accessed using the request variable.
session
The session variable gives you access to the HTTP session corresponding to the HTTP request. You can use
the session variable to store data in the HTTP session and retrieve data from the HTTP session. The session is, by
default, automatically available for every JSP page. Once data is stored into the session, it can be retrieved from
the session by any JSP page running on the same JSP server as long as the session remains active. Sessions usually remain
active for about 30 minutes by default.
This concludes our coverage of JSP fundamentals. Now it's time to get into a more advanced JSP topic: Writing Methods. The next
part of the tutorial covers this topic.
|