🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
JavaServer Pages (JSP) directives are instructions that provide global information about the entire JSP page. The page directive is one of the most important JSP directives, allowing you to set various attributes that affect the overall behavior and environment of the JSP page.
What is the JSP Page Directive?
The page directive is used to define global settings for a JSP page. It provides various attributes to control aspects such as the imported classes, content type, buffer size, error pages, session management, and more. These settings are applied to the entire JSP page and influence how the page is processed and rendered.
Syntax
The page directive is defined using the following syntax:
<%@ page attribute="value" %>
Common Attributes of the Page Directive
Here are some of the most commonly used attributes of the page directive:
- import
- contentType
- pageEncoding
- errorPage
- isErrorPage
- session
- buffer
- autoFlush
- isThreadSafe
- language
- extends
- info
1. import
The import attribute is used to import Java classes or packages into the JSP page. Multiple classes or packages can be imported by separating them with commas.
Example
<%@ page import="java.util.*, java.text.*" %>
2. contentType
The contentType attribute sets the MIME type and character encoding for the response generated by the JSP page.
Example
<%@ page contentType="text/html;charset=UTF-8" %>
3. pageEncoding
The pageEncoding attribute specifies the character encoding for the JSP page itself.
Example
<%@ page pageEncoding="UTF-8" %>
4. errorPage
The errorPage attribute specifies a URL to which the request should be forwarded if an uncaught exception occurs on the JSP page.
Example
<%@ page errorPage="error.jsp" %>
5. isErrorPage
The isErrorPage attribute indicates whether the current JSP page can act as an error page.
Example
<%@ page isErrorPage="true" %>
6. session
The session attribute specifies whether the JSP page should participate in an HTTP session. It can be set to true or false.
Example
<%@ page session="true" %>
7. buffer
The buffer attribute sets the size of the buffer used for the JSP page output.
Example
<%@ page buffer="8kb" %>
8. autoFlush
The autoFlush attribute specifies whether the buffer should be automatically flushed when it is full. It can be set to true or false.
Example
<%@ page autoFlush="true" %>
9. isThreadSafe
The isThreadSafe attribute indicates whether the JSP page is thread-safe. It can be set to true or false.
Example
<%@ page isThreadSafe="false" %>
10. language
The language attribute specifies the scripting language used in the JSP page. The default value is "java".
Example
<%@ page language="java" %>
11. extends
The extends attribute specifies the fully qualified name of the superclass that the generated servlet should extend.
Example
<%@ page extends="com.example.MyServlet" %>
12. info
The info attribute provides a description of the JSP page. This information can be accessed using the Servlet.getServletInfo() method.
Example
<%@ page info="This is a sample JSP page" %>
Example Usage
Let's look at a complete example that uses several attributes of the page directive.
<%@ page import="java.util.*, java.text.*" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page errorPage="error.jsp" %>
<%@ page session="true" %>
<html>
<head>
<title>JSP Page Directive Example</title>
</head>
<body>
<h1>Welcome to JSP Page Directive Example</h1>
<%
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
out.println("Current date and time: " + sdf.format(now));
%>
</body>
</html>
Explanation
- import: Imports the
java.utilandjava.textpackages. - contentType: Sets the content type to
text/htmlwithUTF-8character encoding. - errorPage: Specifies
error.jspas the error page. - session: Enables session management for the JSP page.
Conclusion
The JSP page directive is used for configuring global settings that affect the behavior and environment of a JSP page. By understanding and utilizing the various attributes of the page directive, developers can create well-structured and efficient JSP applications.
Comments
Post a Comment
Leave Comment