views:

60

answers:

1

I've got a .jsp file that is working fine. It's maybe a bit special in that it is calling a factory (ArticlesFactory) that returns a singleton (but that is a detail) of class Articles (it does so by automatically fetching shared Google Docs that are transformed to html and then stored into ".../text/en" but that is a detail too).

The following is working fine: it does exactly what I need, it fetches the articles automatically and I can access my Articles instance fine.

<%@ page pageEncoding="UTF-8" contentType="text/html;charset=utf-8" %>
<%@ page import="com.domain.projectname.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head></head>
<body>

    <% Articles articles = ArticlesFactory.create( getServletContext().getRealPath( "text/en" )); %>
    We have <%= articles.getNbItems()%>

</body>
</html>

However, I must transform it to some notation I don't know nor understand, I'm not even sure what the name for that is and obviously I've got some issue.

I don't know if it's a namespace issue or if there's a problem with the ArticlesFactory factory's static factory method creating the Articles singleton:

<?xml version="1.0" encoding="UTF-8"?>
<jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page"
    xmlns:c="urn:jsptld:http://java.sun.com/jsp/jstl/core"&gt;
<jsp:directive.page import="com.domain.project.ArticlesFactory"/>
<jsp:directive.page contentType="text/html; charset=UTF-8" />

We have ${variable.nbItems}  <!-- What to put here !? -->

</jsp:root>

I tried many things and couldn't figure it out.

Basically I need to: - call the static create method from the ArticlesFactory class - by passing it the result of getServletContext().getRealPath( "text/en" ))

(which should give back an Articles instance)

  • then I want to put the result of getNbItems() in a variable that I want to display

Note that I don't want to have to call getServletContext from any servlet/dispatcher: I want to do it just like in the first working example (ie directly from inside the .jsp).

+2  A: 

You're basically looking for "JSP in XML syntax". Most is already explained in this (old) tutorial. You yet have to replace <% %> by <jsp:scriptlet> and <%= %> by <jsp:expression>.

The xmlns:c namespace is by the way unnecessary here, unless you'd like to use any of the JSTL core tags.

The Expression Language (those ${} things) which is explained in this (also old) tutorial is by the way a separate subject. It only acts on objects in the page, request, session or application scope. In scriptlets however, variables are only defined in local scopes (methodlocal actually), those aren't available in EL. You would need to do the following in the scriptlet to make it available in EL:

pageContext.setAttribute("articles", articles); // Put in page scope (recommended).
request.setAttribute("articles", articles); // Or in request scope. Also accessible by any include files.
session.setAttribute("articles", articles); // Or in session scope. Accessible by all requests in same session.
application.setAttribute("articles", articles); // Or in application scope. Accessible by all sessions.

This way it's available by ${articles} in EL.

BalusC
@BalusC: +1... thanks for the explanation, I'm trying this as soon as I'll come back from dinner and then accept the answer if I can make it work or comment with more silly questions :)
Webinator
It's by the way an old and not widely used technique. It quickly got obsolete with introduction of JSTL and EL in the next JSP release which also made all the scriptlets superfluous (with here and there refactoring in *real* Java classes like servlets and so on). Scriptlets were namely invalid in server-side XML because of `<` and `>` tags and you would need to wrap them in `CDATA` blocks which is plain cumbersome.
BalusC