Best Practices
Ideally, one shouldn't have any <% // scriptlet %>
blocks in JSPs any more.
JSPs have evolved so much from simply hiding Java code behind Standard Actions and Custom Tags to using Expression Language, JSTL and now OGNL expressions to fetch the results (for the request just processed) from pre-populated JavaBeans available in any one of the application scopes (like session, request, page etc.) or sophisticated data stores (like ValueStack in Struts 2).
So, a proper solution would look something like (can't use EL because we need a "user" reference)
<jsp:useBean id="user" class="foo.User" /> <!-- scope="page" by default -->
This line of code when added to Title.jsp
would create a new User
bean and add it to the page
scope and this same line of code in Header.jsp
would then retrieve the User
bean from the page
scope and give it a reference named user
that can then be used throughout the rest of the file.
But, since the rest of the Java code isn't written with tags this won't make much sense. So, you could also simply pass the bean between the two JSPs as a request attribute using one of your <% // scriptlet %> blocks.
<% // in Title.jsp
request.setAttribute ("user", new User());
%>
<% // in Header.jsp
User user = request.getAttribute ("user");
user.setName ("John Doe");
%>
Answer
If the code base is too huge to salvage and using any of the best practices is just plain impractical; you could configure your Eclipse IDE to ignore JSP syntax validation errors or better only turn them off for JSP fragments or specific files and folders.
With your web project selected in the workspace go to Project > Properties > Validation > JSP Syntax
. Then Enable project specific settings
and turn off Validate JSP fragments
as shown below.
A JSP fragment is a .jspf
file that contains a JSP/HTML segment without opening and closing header tags (unless it's a header or a footer fragment of course). So, although you would have to rename your files to .jspf
for Eclipse to recognize them as fragments (and not validate); the main advantages are:
- files can be anywhere in your folder structure
- the extension clearly indicates it's an include
- new fragments will get identified automatically
If the number of include files is huge or they can't be renamed for some reason your next best option would be to move them into a separate folder (like includes
) and then exclude the folder itself from JSP syntax validation.
Again, go to Project > Properties > Validation and with project specific settings enabled click on the browse type [...]
button for accessing the JSP Syntax Validator
settings.
In here, first create an Exclude Group
and then Add Rule
to exclude a folder (like WebContent/includes in the image) from JSP syntax validation.
Now Eclipse would stop reporting errors for any JSP file dropped in this includes folder. You could use the same approach for individual files as well but how practical it is would again depend on how many such fragments you have.
References:
How to avoid Java Code in JSP-Files?