• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 795
  • Last Modified:

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

0
894359
Asked:
894359
  • 6
  • 6
  • 3
  • +4
1 Solution
 
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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
 
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

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 6
  • 6
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now