Link to home
Start Free TrialLog in
Avatar of MJ
MJFlag for United States of America

asked on

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:";

Avatar of Mick Barry
Mick Barry
Flag of Australia image

don't think you can. page has already been converted into a servlet when the request is processed.
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"))
{
 ....
Avatar of vzilka
vzilka

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");
}
%>
ASKER CERTIFIED SOLUTION
Avatar of kennethxu
kennethxu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.
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.
> 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.
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.
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.
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.
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  
Oooops.......
missed the include of java.util.*;
you will have to put <%@ page import="java.util.*" %> at the top as well...
If you need to execute code dynamically you could encapsulate the required code in a class and use reflection to execute it.

Or you can write your own taglib to handle the includes

http://www.javaworld.com/javaworld/jw-12-2003/jw-1205-dynamic.html?
Avatar of MJ

ASKER

I did have code that might be included in the include!
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?
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
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.
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..
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