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.
Extending the request info servlet to also support WML is a nice idea, so I
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
//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
//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
--
--------------------------------------------------------------
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]