Solved

Include "include" in a script??

Posted on 2003-12-06
21
778 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
 
LVL 14

Accepted Solution

by:
kennethxu earned 500 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now