Sometime back (a few months ago, i believe) I sent code to Jon which did the
same thing. It never made it into the distribution - perhaps there were bugs
in it, or perhaps it was not well written.
But I can tell you some of what I learned in the process:
- your hrefs=, src=, etc tags need to be updated to include absolute URLS
for links, images, etc... to still function. My version included code to
parse the HTML and update these parameters automatically.
- Any javascript in your legacy page will possibly break when displaed as a
portlet.
- IE is forgiving about html tags (for example, you can have a <TD> tag
without a </TD> tag). This sometimes causes problems in a portlet-version.
If you are interested, I'll look for the code I submitted which you could
cut-paste into your code if you think it would be useful.
Steve B.
----- Original Message -----
From: <[EMAIL PROTECTED]>
To: "JetSpeed" <[EMAIL PROTECTED]>
Sent: Tuesday, October 31, 2000 3:18 PM
Subject: Re: Source Code for Configurable ServletInvokerPortlet
>
>
>
> Santiago,
>
> thanks for fixing the little second/millisecond bug so quickly.
>
> >+1 on having this kind of examples for helping to handle legacy webapps.
>
> >But the servlet should not be printing HTML, HEAD, and BODY tags, just
the
> HTML
> >fragments. Also, it should possibly check the getContentType() call, and
> deliver
> >HTML or WML as requested by the Portlet. But this last thing is not that
> important,
> >as the current psml allows to specify mime types in the registry.
>
> I just posted the serlvet as an example for a legacy servlet; HTML, HEAD
> and BODY
> tags were in there to verify that the portlet actually works for legacy
> servlets
> that return complete HTML pages. Netscape and IE seem to ignore the HTML,
> HEAD and
> BODY tags when they appear in a table, so it worked.
>
> Extending the request info servlet to also support WML is a nice idea, so
I
> just
> did it (see below). I also commented out the surrounding tags so that the
> servlet
> now returns fragments suitable for inclusion in portlets.
>
> >Also, we should point people using it that it is not the way to go for
new
> >development, just a way to get this servlet you had written before
> displayed in a
> >portlet.
>
> Right, usually content should be rendered by the portlet, e.g. by invoking
> a JSP or
> executing a stylesheet. We are currently also working on support of
> JSPs/servlets
> through the Portlet API, we plan to submit a proposal for discussion soon.
>
> ---- snip ----
>
> import java.io.*;
> import java.util.*;
>
> import javax.servlet.*;
> import javax.servlet.http.*;
>
> /**
> * This example servlet returns some information about the incoming
> request.
> *
> * @author Thomas Schaeck ([EMAIL PROTECTED])
> */
> public class RequestInfoServlet extends HttpServlet
> {
> public void doGet (HttpServletRequest req, HttpServletResponse res)
> throws ServletException, IOException {
> if (req.getHeader("accept").indexOf("text/vnd.wap.wml") != -1) {
> generateWML(req, res);
>
> // add additional content types here
> } else {
> // default to HTML
> generateHTML(req, res);
> }
> }
>
> public void generateHTML(HttpServletRequest req, HttpServletResponse
res)
>
> throws ServletException, IOException {
> PrintWriter out;
>
> res.setContentType("text/html");
> out = res.getWriter ();
>
> // Uncomment this tp obtain a valid HTML document
> file://out.println("<HTML><HEAD><TITLE>Request Info
> Servlet</TITLE></HEAD><BODY BGCOLOR=\"#FFFFEE\">");
>
> out.println("<h4>Request Information:</h4>");
> out.println("<TABLE Border=\"2\" WIDTH=\"65%\" BGCOLOR=\"#DDDDFF\">");
> out.println("<tr><td>Remote user</td><td>" + req.getRemoteUser() +
> "</td></tr>");
> out.println("<tr><td>Remote address</td><td>" + req.getRemoteAddr() +
> "</td></tr>");
> out.println("<tr><td>Remote host</td><td>" + req.getRemoteHost() +
> "</td></tr>");
> out.println("</table><BR><BR>");
>
> Enumeration e = req.getHeaderNames();
> if (e.hasMoreElements()) {
> out.println("<h4>Request headers:</h4>");
> out.println("<TABLE Border=\"2\" WIDTH=\"65%\"
> BGCOLOR=\"#DDDDFF\">");
> while (e.hasMoreElements()) {
> String name = (String)e.nextElement();
> out.println("<tr><td>" + name + "</td><td>" + req.getHeader(name)
+
> "</td></tr>");
> }
> out.println("</table><BR><BR>");
> }
>
> e = req.getParameterNames();
> if (e.hasMoreElements()) {
> out.println("<h4>Servlet parameters:</h4>");
> out.println("<TABLE Border=\"2\" WIDTH=\"65%\"
> BGCOLOR=\"#DDDDFF\">");
> while (e.hasMoreElements()) {
> String name = (String)e.nextElement();
> out.println("<tr><td>" + name + "</td><td>" +
> req.getParameter(name) + "</td></tr>");
> }
> out.println("</table><BR><BR>");
> }
>
> HttpSession session = req.getSession(false);
> if (session != null) {
> out.println("<h4>Session information:</h4>");
> out.println("<TABLE Border=\"2\" WIDTH=\"65%\"
> BGCOLOR=\"#DDDDFF\">");
> out.println("<tr><td>Session ID</td><td>" + session.getId());
> out.println("<tr><td>Last accessed time</td><td>" + new
> Date(session.getLastAccessedTime()).toString() + "</td></tr>");
> out.println("<tr><td>Creation time</td><td>" + new
> Date(session.getCreationTime()).toString() + "</td></tr>");
> String mechanism = "unknown";
> if (req.isRequestedSessionIdFromCookie()) {
> mechanism = "cookie";
> } else if (req.isRequestedSessionIdFromURL()) {
> mechanism = "url-encoding";
> }
> out.println("Session-tracking mechanism" + mechanism);
> out.println("</table><BR><BR>");
>
> String[] vals = session.getValueNames();
> if (vals != null) {
> out.println("<h4>Session values</h4>");
> out.println("<TABLE Border=\"2\" WIDTH=\"65%\"
> BGCOLOR=\"#DDDDFF\">");
> for (int i=0; i<vals.length; i++) {
> String name = vals[i];
> out.println("<tr><td>" + name + "</td><td>" +
> session.getValue(name) + "</td></tr>");
> }
> out.println("</table><BR><BR>");
> }
> }
> // Uncomment this tp obtain a valid HTML document
> // out.println("</body></html>");
> }
>
> public void generateWML(HttpServletRequest req, HttpServletResponse res)
> throws ServletException, IOException {
> PrintWriter out;
>
> res.setContentType("text/vnd.wap.wml");
> out = res.getWriter ();
>
> // Uncomment these lines to obtain a valid WML document instead of a
> fragment
> // out.println("<?xml version=\"1.0\"?>");
> // out.println("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\"
> \"http://www.wapforum.org/DTD/wml_1.1.xml\">");
> // out.println("<wml>");
> // out.println("<card id=\"RequestInfo\" title=\"Request Info\">");
>
> out.println("<p><b>Request Information</b></p>");
> out.println("<p>Remote user: " + req.getRemoteUser() + "</p>");
> out.println("<p>Remote address: " + req.getRemoteAddr() + "</p>");
> out.println("<p>Remote host: " + req.getRemoteHost() + "</p>");
>
> Enumeration e = req.getHeaderNames();
> if (e.hasMoreElements()) {
> out.println("<p><b>Request headers</b></p>");
> while (e.hasMoreElements()) {
> String name = (String)e.nextElement();
> out.println("<p>" + name + ": " + req.getHeader(name) + "</p>");
> }
> }
>
> e = req.getParameterNames();
> if (e.hasMoreElements()) {
> out.println("<p><b>Servlet parameters</b></p>");
> while (e.hasMoreElements()) {
> String name = (String)e.nextElement();
> out.println("<p>" + name + ": "+ req.getParameter(name) + "</p>");
> }
> }
>
> HttpSession session = req.getSession(false);
> if (session != null) {
> out.println("<p><b>Session Info</b></p>");
> out.println("<p>Session ID" + ": "+ session.getId() + "</p>");
> out.println("<p>Last accessed time" + ": "+ new
> Date(session.getLastAccessedTime()).toString() + "</p>");
> out.println("<>Creation time" + ": "+ new
> Date(session.getCreationTime()).toString() + "</p>");
> String mechanism = "unknown";
> if (req.isRequestedSessionIdFromCookie()) {
> mechanism = "cookie";
> } else if (req.isRequestedSessionIdFromURL()) {
> mechanism = "url-encoding";
> }
> out.println("<p><b>Session-Tracking" + mechanism + "</b></p>");
>
> String[] vals = session.getValueNames();
> if (vals != null) {
> out.println("<p>Session values</p>");
> for (int i=0; i<vals.length; i++) {
> String name = vals[i];
> out.println("<p>" + name + ": "+ session.getValue(name) +
> "</p>");
> }
> }
> }
>
> // Uncomment these lines to obtain a valid WML document instead of a
> fragment
> file://out.println("</card></wml>");
> }
> }
>
> ---- snap ----
>
> Best regards,
>
> Thomas
>
> Thomas Schaeck
> IBM Pervasive Computing Division
> Phone: +49-(0)7031-16-3479 e-mail: [EMAIL PROTECTED]
> Address: IBM Deutschland Entwicklung GmbH,
> Schoenaicher Str. 220, 71032 Boeblingen, Germany
>
>
> Santiago Gala <[EMAIL PROTECTED]> on 30.10.2000 22:47:53
>
> Please respond to "JetSpeed" <[EMAIL PROTECTED]>
>
> To: JetSpeed <[EMAIL PROTECTED]>
> cc:
> Subject: Re: Source Code for Configurable ServletInvokerPortlet
>
>
>
>
>
>
> [EMAIL PROTECTED] wrote:
>
> > I've written a configurable ServletInvokerPortlet that invokes
> > servlets/JSPs.
> > (It uses the workaround for embedding JSPs in ECS trees recently posted
> > by Christian Sell, see EcsServletElement.)
> >
> > Maybe this is useful for others as well:
> >
> > The URL of the servlet to be invoked can be set by specifying a
parameter
> > named "url" in the portlet entry in the portlet registry section in the
> > file "jetspeed-config.jcfg":
> >
> > ---- snip ----
> > <portlet-entry type="abstract" name="ServletInvokerPortlet">
> >
> >
>
<classname>org.apache.jetspeed.portal.portlets.ServletInvokerPortlet</classn
ame>
>
> > </portlet-entry>
> >
> > <portlet-entry type="ref" parent="ServletInvokerPortlet"
> > name="RequestInfoPortlet">
> > <parameter name="url" value="/RequestInfo/"/>
> > <meta-info>
> > <title>Request Info</title>
> > <description>This portlet shows some request info.</description>
> > </meta-info>
> > </portlet-entry>
> > ---- snap ----
> >
> > The example defines a portlet named "RequestInfoPortlet" that invokes a
> > servlet
> > accessible under the URL "/RequestInfo/" whenever it is displayed.
> >
> > The entry in default.psml or a personal psml file may look like this:
> > ...
> > <entry type="ref" parent="RequestInfoPortlet">
> > <layout position="1"/>
> > </entry>
> > ...
> >
> > This is the source for the ServletInvokerPortlet:
> >
> > ---- snip ----
> > package org.apache.jetspeed.portal.portlets;
> >
> > import org.apache.ecs.ConcreteElement;
> > import org.apache.ecs.ElementContainer;
> > import org.apache.ecs.StringElement;
> >
> > import org.apache.jetspeed.portal.*;
> > import org.apache.jetspeed.util.*;
> >
> > import org.apache.turbine.util.*;
> >
> > import org.apache.jetspeed.portal.portlets.AbstractPortlet;
> > import org.apache.jetspeed.util.servlet.EcsServletElement;
> >
> > /**
> > * The ServletInvokerPortlet invokes a servlet or JSP and displays the
> > result.
> > *
> > * @author Thomas Schaeck ([EMAIL PROTECTED])
> > */
> > public class ServletInvokerPortlet extends AbstractPortlet {
> >
> > /**
> > * Returns an ECS concrete element that includes the servlet/JSP.
> > *
> > * The servlet/JSP will be invoked when the ECS tree is written
> > * to the servlet output stream and add its output to the stream.
> > */
> > public ConcreteElement getContent() {
> >
// ------------------------------------------------------------------
> > // Note: Rundata should not be obtained from the portlet config,
> > // but I found no other way to get it in the current JetSpeed
version
> > // (as of 10/30/200). Normally, this should be per-request info, not
> > // per-portlet config info. If a new request comes in before this
> code
> > // is reached and the rundata in the portlet config is overwritten,
> > // we might get the wrong request here.
> >
// ------------------------------------------------------------------
> > RunData rundata = this.getPortletConfig().getRunData();
> > PortletConfig pc = this.getPortletConfig();
> >
> > String servletURL = null;
> > try {
> > servletURL = (String)
> > this.getPortletConfig().getInitParameter("url");
> > return new EcsServletElement(rundata, servletURL);
> > } catch (Exception e) {
> > String message = "ServletInvokerPortlet: Error invoking "
> > + servletURL + ": " + e.getMessage();
> > Log.error(message, e);
> > return new StringElement(message);
> > }
> > }
> > }
> > ---- snap ----
> >
> > Here is a modified version of the code originally posted by Christian
> Sell.
> >
> > ---- snip ----
> > package org.apache.jetspeed.util.servlet;
> >
> > import java.io.IOException;
> > import java.io.OutputStream;
> > import java.io.PrintWriter;
> > import java.io.OutputStreamWriter;
> > import javax.servlet.*;
> >
> > import org.apache.turbine.util.RunData;
> > import org.apache.turbine.util.Log;
> > import org.apache.ecs.ConcreteElement;
> > import org.apache.ecs.Element;
> >
> > /**
> > * EcsServletElement encapsulates a servlet/JSP within the context of
ECS
> > * HTML-generation.
> > *
> > * This is a workaround to allow invoking servlets from JetSpeed
> Portlets.
> > * The servlet will be invoked when traversal of an ECS tree during
> writing
> > * reaches the EcsServlet element.
> > *
> > * This is a modified version of Christian Sell's original code.
> > */
> > public class EcsServletElement extends ConcreteElement
> > {
> > /** RunData object to obtain HttpServletRequest/Response from. */
> > private RunData rundata;
> >
> > /** URL of servlet to include */
> > private String url;
> >
> > /**
> > * Construct an ECS element from a given rundata object and URL.
> > *
> > * @param rundata Rundata object that holds the
> > HttpServletRequest/Response
> > * objects to be used for servlet invocation.
> > * @param url The URL of the servlet to invoke.
> > */
> > public EcsServletElement(RunData rundata, String urlString) {
> > this.url = urlString;
> > this.rundata = rundata;
> > }
> >
> > public void output(OutputStream out) {
> > output(new PrintWriter(out));
> > }
> >
> > /**
> > * Add element to the designated PrintWriter.
> > */
> > public void output(PrintWriter out) {
> > ServletContext ctx = rundata.getServletContext();
> > RequestDispatcher dispatcher = ctx.getRequestDispatcher(url);
> > try {
> > // Include the servlet or JSP.
> > dispatcher.include(rundata.getRequest(),rundata.getResponse());
> > } catch (Exception e) {
> > String message = "JSPPortlet: Could not include the following
> URL:
> > "
> > + url + " : " + e.getMessage();
> > Log.error( message, e );
> > out.print(message);
> > }
> > }
> > }
> > ---- snap ----
> >
> > This is the example servlet I used. It prints some of the request info
in
> a
> > table.
> >
> > ---- snip ----
> > import java.io.*;
> > import java.util.*;
> >
> > import javax.servlet.*;
> > import javax.servlet.http.*;
> >
> > /**
> > * This example servlet returns some information about the incoming
> > request.
> > *
> > * @author Thomas Schaeck ([EMAIL PROTECTED])
> > */
> > public class RequestInfoServlet extends HttpServlet
> > {
> > public void doGet (HttpServletRequest req, HttpServletResponse res)
> > throws ServletException, IOException {
> > PrintWriter out;
> >
> > res.setContentType("text/html");
> > out = res.getWriter ();
> >
> > out.println("<HTML><HEAD><TITLE>Example Servlet</TITLE>" +
> > "</HEAD><BODY BGCOLOR=\"#FFFFEE\">");
> > out.println("<h4>Request Information:</h4>");
> > out.println("<TABLE Border=\"2\" WIDTH=\"65%\"
> BGCOLOR=\"#DDDDFF\">");
> > out.println("<tr><td>Remote user</td><td>" + req.getRemoteUser() +
> > "</td></tr>");
> > out.println("<tr><td>Remote address</td><td>" + req.getRemoteAddr()
+
> > "</td></tr>");
> > out.println("<tr><td>Remote host</td><td>" + req.getRemoteHost() +
> > "</td></tr>");
> > out.println("</table><BR><BR>");
> >
> > Enumeration e = req.getHeaderNames();
> > if (e.hasMoreElements()) {
> > out.println("<h4>Request headers:</h4>");
> > out.println("<TABLE Border=\"2\" WIDTH=\"65%\"
> > BGCOLOR=\"#DDDDFF\">");
> > while (e.hasMoreElements()) {
> > String name = (String)e.nextElement();
> > out.println("<tr><td>" + name + "</td><td>" +
req.getHeader(name)
> +
> > "</td></tr>");
> > }
> > out.println("</table><BR><BR>");
> > }
> >
> > e = req.getParameterNames();
> > if (e.hasMoreElements()) {
> > out.println("<h4>Servlet parameters:</h4>");
> > out.println("<TABLE Border=\"2\" WIDTH=\"65%\"
> > BGCOLOR=\"#DDDDFF\">");
> > while (e.hasMoreElements()) {
> > String name = (String)e.nextElement();
> > out.println("<tr><td>" + name + "</td><td>" +
> > req.getParameter(name) + "</td></tr>");
> > }
> > out.println("</table><BR><BR>");
> > }
> >
> > HttpSession session = req.getSession(false);
> > if (session != null) {
> > out.println("<h4>Session information:</h4>");
> > out.println("<TABLE Border=\"2\" WIDTH=\"65%\"
> > BGCOLOR=\"#DDDDFF\">");
> > out.println("<tr><td>Session ID</td><td>" + session.getId());
> > out.println("<tr><td>Last accessed time</td><td>" + new
> > Date(session.getLastAccessedTime()).toString() + "</td></tr>");
> > out.println("<tr><td>Creation time</td><td>" + new
> > Date(session.getCreationTime()).toString() + "</td></tr>");
> > String mechanism = "unknown";
> > if (req.isRequestedSessionIdFromCookie()) {
> > mechanism = "cookie";
> > } else if (req.isRequestedSessionIdFromURL()) {
> > mechanism = "url-encoding";
> > }
> > out.println("Session-tracking mechanism" + mechanism);
> > out.println("</table><BR><BR>");
> >
> > String[] vals = session.getValueNames();
> > if (vals != null) {
> > out.println("<h4>Session values</h4>");
> > out.println("<TABLE Border=\"2\" WIDTH=\"65%\"
> > BGCOLOR=\"#DDDDFF\">");
> > for (int i=0; i<vals.length; i++) {
> > String name = vals[i];
> > out.println("<tr><td>" + name + "</td><td>" +
> > session.getValue(name) + "</td></tr>");
> > }
> > out.println("</table><BR><BR>");
> > }
> > }
> > out.println("</body></html>");
> > }
> > ---- snap ----
> >
>
> +1 on having this kind of examples for helping to handle legacy webapps.
>
> But the servlet should not be printing HTML, HEAD, and BODY tags, just the
> HTML
> fragments. Also, it should possibly check the getContentType() call, and
> deliver
> HTML or WML as requested by the Portlet. But this last thing is not that
> important,
> as the current psml allows to specify mime types in the registry.
>
> Also, we should point people using it that it is not the way to go for new
> development, just a way to get this servlet you had written before
> displayed in a
> portlet.
>
> >
> > Best regards,
> >
> > Thomas
> >
> > Thomas Schaeck
> > IBM Pervasive Computing Division
> > e-mail: [EMAIL PROTECTED]
> > Address: IBM Deutschland Entwicklung GmbH,
> > Schoenaicher Str. 220, 71032 Boeblingen, Germany
> >
> > --
> > --------------------------------------------------------------
> > Please read the FAQ! <http://java.apache.org/faq/>
> > To subscribe: [EMAIL PROTECTED]
> > To unsubscribe: [EMAIL PROTECTED]
> > Archives and Other: <http://java.apache.org/main/mail.html>
> > Problems?: [EMAIL PROTECTED]
>
>
>
> --
> --------------------------------------------------------------
> Please read the FAQ! <http://java.apache.org/faq/>
> To subscribe: [EMAIL PROTECTED]
> To unsubscribe: [EMAIL PROTECTED]
> Archives and Other: <http://java.apache.org/main/mail.html>
> Problems?: [EMAIL PROTECTED]
>
>
>
>
>
>
> --
> --------------------------------------------------------------
> Please read the FAQ! <http://java.apache.org/faq/>
> To subscribe: [EMAIL PROTECTED]
> To unsubscribe: [EMAIL PROTECTED]
> Archives and Other: <http://java.apache.org/main/mail.html>
> Problems?: [EMAIL PROTECTED]
--
--------------------------------------------------------------
Please read the FAQ! <http://java.apache.org/faq/>
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
Archives and Other: <http://java.apache.org/main/mail.html>
Problems?: [EMAIL PROTECTED]