Mike
Mike

Reputation: 1336

How to insert content in a keywords metatag with JSP

I have the following JSP page:

<%@page import="com.myPath.JSPHelper"%>
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html> 
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <meta name="keywords" content="${jspHelper.getKeywordsMetatag()}">
    </head>
<body>
    <%
        JSPHelper jspHelper = new JSPHelper();
        jspHelper.loadData(request.getAttribute("id").toString()); // load data from database
    %>
    <script type="text/javascript">
       <%=jspHelper.getScriptContent()%>
    </script>
</body>
</html>

What I'm trying to do is to fill in the contents of the keywords meta tag using a function getKeywordsMetatag() that is defined in a companion class `JSPHelper.java'.

But this is not working, I get the following error:

The function getKeywordsMetatag must be used with a prefix when a default namespace is not specified

I'm new to JSP so I've tried many things without success.

What am I doing wrong here?

What really bothers me is that the function getScriptContent() perfectly works, dumping javascript code in the html page. Why does getScriptContent() work but not getKeywordsMetatag()?

Thanks!

Upvotes: 0

Views: 10446

Answers (1)

developerwjk
developerwjk

Reputation: 8659

I would move the declaration of jspHelper up above its first use. I would also drop the ${} syntax but that might not be necessary.

 <%@page import="com.myPath.JSPHelper"%>
 <%@page language="java" contentType="text/html; charset=ISO-8859-1"
     pageEncoding="ISO-8859-1"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

     <%
         JSPHelper jspHelper = new JSPHelper();
     %>
 <html> 
     <head>
         <meta http-equiv="content-type" content="text/html; charset=utf-8">
         <meta name="keywords" content="<%=jspHelper.getKeywordsMetatag()%>">
     </head>
 <body>
     <%
         jspHelper.loadData(request.getAttribute("id").toString()); // load data from database
     %>
     <script type="text/javascript">
        <%=jspHelper.getScriptContent()%>
     </script>
 </body>
 </html>

Upvotes: 1

Related Questions