The last of the JSP syntax-related elements are something called implicit objects. Within JSP scriptlets, you have access to these implicit objects to interact with the executing servlet environment for the JSP page. Much of the access to the implicit objects should be minimized. However, there are instances in which access is acceptable. To fully utilize the implicit object set requires an understanding of the latest Java Servlet API.
The following table represents the set of implicit objects available to you.
|Implicit Object Description|
|request||The client request, which includes parameters from GET/POST requests|
|response||The page's response back to the client|
|pageContext||The attributes of the page are managed here|
|session||The session associated with the request|
|application||The context in which the servlet is running|
|out||The output stream used to send the response|
|config||The servlet configuration object|
|page||The JSP page itself|
|exception||For error pages, the uncaught exception|
So, what are all these for, and how do you use them? Well, basically, within your scriptlets you can use them to access the servlet executing the JSP code. Without going into too many details of the Servlet API, let's examine some of the things you can do with them:
- Instead of using an expression, you can directly access the implicit out object to print something to the response:
<% out.println("Hello"); %>.
- Instead of sending a parameter directly to a JavaBean, you can get the parameter value from the request object:
<% String name=request.getParameter("name"); out.println(name); %>.
As you develop more with JSP, if you create JavaBeans or find yourself putting too much Java source code into your JSP files, you'll need to create supporting Java classes, which encourages reuse and decreases JSP page translation time. When you need to create Java class files, you must:
- Add the JDSWK installation \bin directory to your PATH. In yourautoexec.bat file's PATH line, add C:\1.2.2\bin; to the end of that line.
- Copy the JAR file to the \jre\lib\ext directory with the command:
copy c:\jswdk-1.0.1\lib\servlet.jar c:\jdk1.2.2\jre\lib\ext.