Java.Servlet.How is JSP configured in the deployment descriptor.

🔵 1. Configuring the JSP Servlet (Optional)

The server (like Tomcat) has an internal servlet for handling JSPs, called the JSP servlet.
You can explicitly declare and configure it in web.xml if needed.

✅ Example:

<servlet>
    <servlet-name>jsp</servlet-name>
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
    <init-param>
        <param-name>fork</param-name>
        <param-value>false</param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>

✅ What’s happening:

  • You map all URLs ending with .jsp to the internal JspServlet.
  • fork=false is a configuration option (you can set many others, like buffering size, development mode, etc.).
  • load-on-startup=3 means the servlet will be initialized when the server starts (priority 3).

🔵 2. Configuring JSP Properties (Advanced)

You can also configure special behaviors for JSP pages globally using <jsp-config> in web.xml.

✅ Example:

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
        <scripting-invalid>true</scripting-invalid>
        <el-ignored>false</el-ignored>
        <include-prelude>/WEB-INF/jspf/header.jspf</include-prelude>
        <include-coda>/WEB-INF/jspf/footer.jspf</include-coda>
    </jsp-property-group>
</jsp-config>

✅ What this does:

  • Applies to all .jsp pages (url-pattern).
  • Sets UTF-8 encoding.
  • Disables scripting (no <% %> allowed if scripting-invalid=true — enforces clean JSP).
  • Forces EL (Expression Language) to be enabled (el-ignored=false).
  • Automatically includes a header and footer file on every JSP page.

🔵 3. Error Page Mapping (JSP Error Handling)

You can also map errors to JSP error pages:

<error-page>
    <error-code>404</error-code>
    <location>/errors/notfound.jsp</location>
</error-page>

<error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/errors/error.jsp</location>
</error-page>

✅ Now if an error occurs, the user is forwarded to a friendly JSP error page.

🧠 Quick Overview Table

TaskHow it’s configured
Map JSP files to JspServlet<servlet> + <servlet-mapping>
Set page settings (encoding, EL, includes)<jsp-config>
Define global error pages<error-page>

📢 Important Notes:

  • In most simple or modern Spring Boot applications, you don’t manually configure JSPs in web.xml — it’s handled automatically.
  • Explicit JSP servlet mapping is mainly used when you need custom tuning or fine-grained control.
  • Custom settings like development mode, checkInterval, genStringAsCharArray, etc., can also be configured as <init-param>s.

🎯 Final Quick Summary:

AspectExplanation
JSP servlet mappingMap .jsp files to the container’s JSP servlet.
JSP propertiesSet page encoding, scripting rules, EL behavior, automatic includes.
Error pagesRedirect errors to JSP error handlers.
Optional nowBut still useful for fine control.

🚀 Practical Real-World Tip:

If you ever need to disable scriptlets (<% %>) across all JSPs,
✅ set:

<scripting-invalid>true</scripting-invalid>

in web.xml!

✅ This forces developers to write clean tag-based JSP code — much better for real-world apps.

This entry was posted in Без рубрики. Bookmark the permalink.