I have a page which uses a tag template. My web.xml is very basic.
I simply want to run some code in the page.
And no, I'm not interested in tags or other alternative. I want to use the bad-practice scriptlet haha.
So far I'm getting this "HTTP ERROR 500" error:
Scripting elements ( %!, jsp:declaration, %=, jsp:expression, %, jsp:scriptlet ) are disallowed here.
While my files look like:
/WEB-INF/web.xml
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
/WEB-INF/tags/wrapper.tag
<%@tag description="Simple Wrapper Tag" pageEncoding="UTF-8"%>
<%@ attribute name="title" required="true" type="java.lang.String"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>${title}</title>
</head>
<body>
<jsp:doBody />
</body>
</html>
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags"%>
<t:wrapper>
<jsp:attribute name="title">My nice title</jsp:attribute>
<jsp:body>
<h1><%="some code generated text"%></h1>
</jsp:body>
</t:wrapper>
I have tried to modify web.xml to explicitly enable it, like this (not working):
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<scripting-invalid>false</scripting-invalid>
</jsp-property-group>
<jsp-property-group>
<url-pattern>*.tag</url-pattern>
<scripting-invalid>false</scripting-invalid>
</jsp-property-group>
</jsp-config>
So, how do I use pure scriptlets within my tag'ed JSP?
EDIT #1:
An ideal code would look like this, inside a page that uses my template ('wrapper' as the above):
<%@page import="java.util.Calendar"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags"%>
<t:wrapper>
<jsp:attribute name="title">My nice title</jsp:attribute>
<%
final int day_of_week = Calendar.getInstance().get(
Calendar.DAY_OF_WEEK);
if (day_of_week == Calendar.SATURDAY)
{
%>
<jsp:body>
<h1>Have a nice Saturday (<%=Integer.toString(day_of_week)%>)!</h1>
</jsp:body>
<%
}
else
{
%>
<jsp:body>
<h1>Have a nice rest-of-the-week (<%=Integer.toString(day_of_week)%>)!</h1>
</jsp:body>
<%
}
%>
</t:wrapper>
See? Scriptlets between & inside the '' tags. That's exactly what I'm trying to achieve.
When the scripting language is set to java, a scriptlet is transformed into a Java programming language statement fragment and is inserted into the service method of the JSP page's servlet. A programming language variable created within a scriptlet is accessible from anywhere within the JSP page.
The jsp scriptlet tag can only declare variables not methods. The jsp declaration tag can declare variables as well as methods. The declaration of scriptlet tag is placed inside the _jspService() method. The declaration of jsp declaration tag is placed outside the _jspService() method.
The scriptlet is everything inside the <% %> tags. Between these the user can add any valid Scriptlet i.e. any valid Java Code. In AppleScript, a scriptlet is a small script. In Windows, a scriptlet is COM component including a HTML code and a script which may be written in a variety of scripting languages.
In this case, the container doesn't care about the value of scripting-invalid
in web.xml because its looking at the tag meta-data for jsp:body
which has a body-content value of scriptless
. So when you see:
Scripting elements ( %!, jsp:declaration, %=, jsp:expression, %, jsp:scriptlet ) are disallowed here.
The container is complaining about the contents of jsp:body which must be scriptless. If you want to render scriptlet content in the body, you can set it as a page attribute outside of the jsp:body tag using a scriptlet and then render it using EL inside the body like so:
<% request.setAttribute("stuff", object); %>
<jsp:body>
${stuff}
</jsp:body>
Kind of late answer, but this should work:
<t:wrapper>
<jsp:attribute name="title">My nice title</jsp:attribute>
<c:set var="bodyContent">
<%
final int day_of_week = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
if (day_of_week == Calendar.SATURDAY)
{
%>
<h1>Have a nice Saturday (<%=Integer.toString(day_of_week)%>)!</h1>
<%
}
else
{
%>
<jsp:body>
<h1>Have a nice rest-of-the-week (<%=Integer.toString(day_of_week)%>)!</h1>
</jsp:body>
<%
}
%>
</c:set>
<jsp:body>
${bodyContent}
</jsp:body>
</t:wrapper>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With