Include "include" in a script??

I have a chunk of code  that I want to have include a dynamic message based on a variable that is passed in. I the example the variable would be called "thanksMessage" and in this case would hold "test". How can I do this?? Code sample below.

////////////////////////////////

<%      
if( email != null ){
      
            String subject = "Brocade Partner Registration Inquiry";
            
                // WANT TO INCLUDE (test.inc)  BELOW MESSAGE HERE!
            
            EmailProperties.initialize();
            Mailer.sendMail(EmailProperties.ServerName, "sandiscovery@brocade.com", email, subject, msg,"mjohns@brocade.com");
}
%>

//////////////////////////////////////
Include sample include (test.inc)
//////////////////////////////////////

String msg = "";
msg += "Thank you for participating in the SAN Discovery Program. Instructions to generate XML files of your SANs and your unique link to upload those XML files to generate your SAN Visio diagrams are included below. ";
msg += "\n\n";
msg += "Please follow these steps to run the program:";

894359Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
don't think you can. page has already been converted into a servlet when the request is processed.
0
objectsCommented:
you'd need to do something like:

String msg = "";
if (thanksMessage.equlas("test"))
{
  msg += "Thank you for participating in the SAN Discovery Program. Instructions to generate XML files of your SANs and your unique link to upload those XML files to generate your SAN Visio diagrams are included below. ";
   msg += "\n\n";
   msg += "Please follow these steps to run the program:";
}
else if (thanksMessage.equlas("xyz"))
{
 ....
0
vzilkaCommented:
I think that should work:

<%    
if( email != null ){
     
          String subject = "Brocade Partner Registration Inquiry";
%>          
<@jsp:include .../>
<%          
          EmailProperties.initialize();
          Mailer.sendMail(EmailProperties.ServerName, "sandiscovery@brocade.com", email, subject, msg,"mjohns@brocade.com");
}
%>
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

kennethxuCommented:
This should be able to fulfil your requirement:

////////////////////////////////
<%@ page import="java.io.*" %>
<%    
if( email != null ){
     
          String subject = "Brocade Partner Registration Inquiry";
         
          ByteArrayOutputStream bais = new ByteArrayOutputStream();
          InputStream in = request.getResourceAsStream( thanksMessage + ".inc" );
          int c; while( (c=in.read()) != -1 ) bais.write( c );
          in.close()
          String msg = bais.toString();
         
          EmailProperties.initialize();
          Mailer.sendMail(EmailProperties.ServerName, "sandiscovery@brocade.com", email, subject, msg,"mjohns@brocade.com");
}
%>

//////////////////////////////////////
Include sample include (test.inc)
//////////////////////////////////////

Thank you for participating in the SAN Discovery Program. Instructions to generate XML files of your SANs and your unique link to upload those XML files to generate your SAN Visio diagrams are included below.

Please follow these steps to run the program:

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vzilkaCommented:
kennethxu, this will only support pure text files.
using the jsp:include directive will allow embedding code and HTML segments in the included JSP file.
0
objectsCommented:
you can't include a dynamic include at request time. To execute different code depending on the request instead check the condition in your code as I suggested above.
0
kennethxuCommented:
vzilka, I think what you suggested will NOT work. maybe you can try the by yourself?
I believe what asker want is to get different message from different file, so a pure text is good enough. if asker need more sophisticated data, we can use xml file and dynabean to fulfil the requirement.

What object suggested is correct except that if the thanksMessage is not known or need to change over time.
0
objectsCommented:
> if the thanksMessage is not known or need to change over time.

the inc would need to be changed also if this was the case.
The code I suggested could be an include also.
0
kennethxuCommented:
I cannot be sure but it could be the idea of let application user (not developer) to add more inc files. otherwise I cannot see the advantage of useing inc file in the first place.
0
objectsCommented:
yes that appears what they are attempting, with the approach I suggested in would mean instead of adding an extra include you would add an extra condition.
If you want to include code I don't see any alternative.
0
kennethxuCommented:
I was about the suggest to use dynamic include plus request.setAttribute() to transfer data. but again I think a text file is sufficient and also easier to handle for just a message. user can always add more text files as they need.
maybe,  894359 can explain the actual requirement so we might be able come up with a better alternative.
0
KuldeepchaturvediCommented:
Well instead of adding new file for every new message can't we do this..?( and extension to kenneth's solution).
<%@ page import="java.io.*" %>
<%    
if( email != null ){
   
          String subject = "Brocade Partner Registration Inquiry";
          msgProperties = new Properties();    
         msgProperties.load(new FileInputStream("/yourrelativepath/messages.inc");
         String msg =msgProperties.getProperty(thanksMessage.trim());
         if(msg==null) msg="No Message found";
         EmailProperties.initialize();
         Mailer.sendMail(EmailProperties.ServerName, "sandiscovery@brocade.com", email, subject, msg,"mjohns@brocade.com");
}
%>

and messages.inc will look like
test="Thank you for participating in the SAN Discovery Program. Instructions to generate XML files of your SANs and your unique link to upload those XML files to generate your SAN Visio diagrams are included below. \n\nPlease follow these steps to run the program:";
test1="some new message";

This way system will not have to handle multiple files and will retain the dynamic nature of messages as well....

Hope this helps  
0
KuldeepchaturvediCommented:
Oooops.......
missed the include of java.util.*;
you will have to put <%@ page import="java.util.*" %> at the top as well...
0
objectsCommented:
If you need to execute code dynamically you could encapsulate the required code in a class and use reflection to execute it.

0
TimYatesCommented:
Or you can write your own taglib to handle the includes

http://www.javaworld.com/javaworld/jw-12-2003/jw-1205-dynamic.html?
0
894359Author Commented:
I did have code that might be included in the include!
0
kennethxuCommented:
894359, you cannot do the way you wanted. but there is a lot of possible alternative solutions to your problem. But we don't know which will suit you best unless you would explain more detail of what exactly you want to achieve, or what's the business logic behind this?
0
jarasaCommented:
Hi kennethxu

I do not agree with you, I believe that 894359 can make an include on his JSP but what he has to notice is that it will be there always. once compiled, is not posible to make it dinamic unless he does compile it everytime it get accessed, wich will take a lot of time to access. I bet your answer is the best solution thought.

Javier
0
kennethxuCommented:
Javier, if you use <%@include file=...%> you cannot DYNAMIC, the file name must be static. if you use <jsp:include> you can only include the result, not part of code. Give it a try and you know what I mean.
0
KuldeepchaturvediCommented:
Include can not have any part dynamic abt it... it you try to put in some piece of code inside include your jsp won't even compile...
give it a shot if you want to..
0
jarasaCommented:
I knew you were going to tell me that just a second after I hit the submit button, and you're right, I missmatch that!!

:c)

Javier
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.