5

We use JSPs (among other things) to build HTML and plain-text emails. In the plain-text JSPs, we have to be very careful of newlines when importing classes and taglibs.

What we've been doing is to end an import on the same line as we start the next one, like so:

<%@ page language="java" contentType="text/plain; charset=UTF-8" pageEncoding="UTF-8" %><%--
Forgive the funky formating BUT being a plain text email all 
white space, including linebreaks for JSP tags gets carried over 
to the final results!!!!
--%><%@ 
taglib uri="/tags/struts-bean" prefix="bean" %><%@ 
taglib uri="/tags/struts-logic" prefix="logic" %><%@
page import="java.util.*" %><%@ 
page import="foo.package.integration.value.*" %><%@ 
page import="foo.package.integration.value.languages.LanguageType" %><%@ 
page import="foo.package.integration.bd.*" %><%@
page import="foo.package.presentation.resource.DBConstants" %><%@ 
page import="foo.package.presentation.resource.MessageUtilities" %><%
Locale notificationLocale = (Locale) pageContext.getAttribute("notificationLocale");
Inspection inspection = (Inspection) request.getSession().getAttribute("inspection");
String survey = MessageUtilities.getMessageDetailForAnonymousSurvey(inspection, notificationLocale, false);
String appName = foo.package.presentation.resource.notification.NotificationBrander.getApplicationNameOrDefault(request);
%><bean:message key="notification.text.header.client.applicationName" arg0="<%= appName %>" locale="notificationLocale"/>

I would like to put all of these includes into one <%@ ... %> block. Is that possible? If so, how?

Mar
  • 7,765
  • 9
  • 48
  • 82
  • 3
    The issue isn't JSP itself, it's that you're doing your JSPs almost entirely wrong. Scriptlets, imports, all this stuff--this is broken. Also, JSP for plain text is, in general, a pain. – Dave Newton Dec 02 '14 at 19:21
  • Please stop doing this. [Don't use scriptlets anymore](http://stackoverflow.com/q/3177733/1065197) and use MVC approach using Servlets and EL + JSTL. – Luiggi Mendoza Dec 02 '14 at 19:30

2 Answers2

12
<%@ page import="java.io.PrintWriter, java.io.FileOutputStream, java.io.File, etc... %>

Just use commas to separate the imports.

brso05
  • 13,142
  • 2
  • 21
  • 40
0

What i would do is , create a separate file for all includes

**includes.jsp**
<%@taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
. . .

And add it to the page as,

As you cant group the taglibs like page imports, above method keeps your code look clean

Santhosh
  • 8,181
  • 4
  • 29
  • 56