?
Solved

include code fragments in jsp

Posted on 2007-10-16
9
Medium Priority
?
575 Views
Last Modified: 2011-09-20
I have a dozen lines of code that I want to include in every jsp program I am writing for an application. I have Searched EE for ways of doing this, but am more confused than when I started what with <@ include ...,> <jsp:include ...>, etc. I am successfully using <%@ include file="..." %> to include html in my web page. That's not what I want. I want to include jsp code as if it were in-line with the rest of my code. I have tried: %> <%@ include file="myFragment.jsp"%><%, but that actually dumps the code onto the web page! And my program ends up missing the lines and not executing the logic. Same thing happens with <jsp:include page="dbConnection.jsp"/>

How to I actually include CODE that is executed in-line?
0
Comment
Question by:jmarkfoley
  • 5
  • 4
9 Comments
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 20090032
When I do this:

<%@ include file="testJSPfragment.jsp"%>
<br/>Fragment = <%=testFragment%>

it works.  The testJSPfragment.jsp page declares the testFragment variable and sets it.  The .jsp page with this include displays its value correctly.

What code are you using that doesn't work?
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 20103330
Here is the code. If I have the part between slashes in an separate file and do:
<%@ include file="myFragment.jsp"%>, con is null. If I include this fragment in my program the connection is made:

Connection con = null;
Statement stmt = null;
ResultSet rs = null;
///////////////INCLUDED FROM HERE////////////////////////////////////
    String dbDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    String dbURL = "myString";

    try {
        Class.forName( dbDriver );
        con = DriverManager.getConnection( dbURL );
    }
    catch (SQLException e) {
        failMsg = "Sorry, unable to connect with Database Server. " +
            "Please try again later. Status: " + e.getErrorCode();
    }
/////////////////////END OF INCLUDE/////////////////////////

if (con != null) try {
    stmt = con.createStatement();
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 20103586
I don't know -- your code works for me.
Maybe your real problem is that your driver or url strings are not correct?

Do you have a plain jsp page that does this (no included fragments)?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:jmarkfoley
ID: 20103803
Actually, yes, that's basically the code except I pulled out the connect string part. Here's the whole thing copy/pasted right from the program. I wanted the "include" the part starting with "String dbDriver ..." and go through the "catch (SQLException e)" [just before the if (con ...]. I'll try again, but my use of the include dumped the file onto the webpage.

Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String dbDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL = null;
String query = null;

// GET DATABASE CONNECTION STRING

Context ctx = new javax.naming.InitialContext();
Context env = (Context) ctx.lookup("java:comp/env");
dbURL = (String) env.lookup("connURL");

try {
    Class.forName( dbDriver );
    con = DriverManager.getConnection( dbURL );
}
catch (SQLException e) {
    failMsg = "Sorry, unable to connect with Database Server. " +
        "Please try again later. Status: " + e.getErrorCode();
}

// CONNECTED OK. GET USER INFO

if (con != null) try {
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 20103833
So why is dbURL "myString" in your fragment, and (String) env.lookup("connURL"); in your working code?
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 20113641
It's not. I said:
> I wanted the "include" the part starting with "String dbDriver ..." and go through the "catch (SQLException e)"

env.lookup is within that range.

I tried it again, just chopping some lines out of the working code. Here's the main program:

String dbDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL = null;
String query = null;

Integer payrollYear = null;

/////////////////////////////////////////
%><%@ include file="blah.jsp" %><%
///////////////////////////////////////////
// CONNECTED OK. GET USER INFO

if (con != null) try {
:

and here is the contents are blah.jsp:

// GET DATABASE CONNECTION STRING

Context ctx = new javax.naming.InitialContext();
Context env = (Context) ctx.lookup("java:comp/env");
dbURL = (String) env.lookup("connURL");

try {
    Class.forName( dbDriver );
    con = DriverManager.getConnection( dbURL );
}
catch (SQLException e) {
    failMsg = "Sorry, unable to connect with Database Server. " +
        "Please try again later. Status: " + e.getErrorCode();
}


blah.jsp got dumped verbatim to the web page. It is not including it as code. It is treating it as html.

Your saying this works on your system? Can you see anything I am doing wrong?

0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 20113901
Well, as I said, the code works for me.  In fact, I haven't been able to break it.

Is it possible that your mapping for blah.jsp somehow does not translate to a .jsp file?
0
 
LVL 27

Accepted Solution

by:
mrcoffee365 earned 2000 total points
ID: 20125212
I just realized what the problem might be.  You made blah.jsp a .jsp file, right?  You've been posting fragments of your code, so I didn't notice.

The fragment has to have .jsp directives -- the <% and %>.  The include command then tells Tomcat to compile the fragment, and include it in the larger .jsp file.

So blah.jsp should look like this:

<%
// GET DATABASE CONNECTION STRING

Context ctx = new javax.naming.InitialContext();
Context env = (Context) ctx.lookup("java:comp/env");
dbURL = (String) env.lookup("connURL");

try {
    Class.forName( dbDriver );
    con = DriverManager.getConnection( dbURL );
}
catch (SQLException e) {
    failMsg = "Sorry, unable to connect with Database Server. " +
        "Please try again later. Status: " + e.getErrorCode();
}
%>

If you don't put jsp directives in a jsp file, then it is treated as non-jsp text by the jsp compiler.
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 20265479
mrcoffee365: yes, I did figure that out. You are exactly right. I did not have my included fragment surrounded by <% %> in the include file. This is my first jsp program, so I am getting use to the conventions. Thanks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Often times it's very very easy to extend a volume on a Linux instance in AWS, but impossible to shrink it. I wanted to contribute to the experts-exchange community a way of providing a procedure that works on an AWS instance. It can also be used on…
Tech giants such as Amazon and Google have sold Alexa and Echo to such an extent that they have become household names. And soon they are expected to be used by commoners in their homes, ordering takeout, picking out a song, answering trivia questio…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

840 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