[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Include "include" in a script??

Posted on 2003-12-06
21
Medium Priority
?
791 Views
Last Modified: 2013-11-18
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
Comment
Question by:894359
  • 6
  • 6
  • 3
  • +4
21 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9890858
don't think you can. page has already been converted into a servlet when the request is processed.
0
 
LVL 92

Expert Comment

by:objects
ID: 9890864
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
 
LVL 9

Expert Comment

by:vzilka
ID: 9891729
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 14

Accepted Solution

by:
kennethxu earned 2000 total points
ID: 9891958
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
 
LVL 9

Expert Comment

by:vzilka
ID: 9893795
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
 
LVL 92

Expert Comment

by:objects
ID: 9893835
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 9893950
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
 
LVL 92

Expert Comment

by:objects
ID: 9893995
> 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
 
LVL 14

Expert Comment

by:kennethxu
ID: 9894019
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
 
LVL 92

Expert Comment

by:objects
ID: 9894087
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 9894109
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
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 9894713
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
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 9894716
Oooops.......
missed the include of java.util.*;
you will have to put <%@ page import="java.util.*" %> at the top as well...
0
 
LVL 92

Expert Comment

by:objects
ID: 9894805
If you need to execute code dynamically you could encapsulate the required code in a class and use reflection to execute it.

0
 
LVL 35

Expert Comment

by:TimYates
ID: 9895229
Or you can write your own taglib to handle the includes

http://www.javaworld.com/javaworld/jw-12-2003/jw-1205-dynamic.html?
0
 

Author Comment

by:894359
ID: 9897963
I did have code that might be included in the include!
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9898332
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
 
LVL 6

Expert Comment

by:jarasa
ID: 9903476
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 9904547
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
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 9904739
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
 
LVL 6

Expert Comment

by:jarasa
ID: 9904773
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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question