Mark Simms

Ranch Hand
+ Follow
since Jul 20, 2000
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Mark Simms

Do you have any links explaining the new certification ?
Is it "Java Web Developer" or what ?
I saw no mention of this new cert in the certification forum on this site.
17 years ago
Be sure to set the headers so the login page expires immediately:
<% response.addHeader("Pragma", "No-cache");
response.addHeader("Cache-Control", "no-cache");
response.addDateHeader("Expires", 1);
%>
(above assumes JSP for the login page)
if it's HTML, then I believe you can do the same thing in Javascript !
17 years ago
The example below shows how a servlet can determine both parameter names and values:
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import coreservlets.ServletUtilities;
/** Shows all the parameters sent to the servlet via either
* GET or POST. Specially marks parameters that have
* no values or multiple values.
* <P>
* Taken from Core Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2000 Marty Hall; may be freely used or adapted.
*/
public class ShowParameters extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Reading All Request Parameters";
out.println(ServletUtilities.headWithTitle(title) +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1 ALIGN=CENTER>" + title + "</H1>\n" +
"<TABLE BORDER=1 ALIGN=CENTER>\n" +
"<TR BGCOLOR=\"#FFAD00\">\n" +
"<TH>Parameter Name<TH>Parameter Value(s)");
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
out.print("<TR><TD>" + paramName + "\n<TD>");
String[] paramValues =
request.getParameterValues(paramName);
if (paramValues.length == 1) {
String paramValue = paramValues[0];
if (paramValue.length() == 0)
out.println("<I>No Value</I>");
else
out.println(paramValue);
} else {
out.println("<UL>");
for(int i=0; i<paramValues.length; i++) {
out.println("<LI>" + paramValues[i]);
}
out.println("</UL>");
}
}
out.println("</TABLE>\n</BODY></HTML>");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Below shows how to access SESSION information:
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;
import java.util.*;
/** Simple example of session tracking. See the shopping
* cart example for a more detailed one.
* <P>
* Taken from Core Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2000 Marty Hall; may be freely used or adapted.
*/
public class ShowSession extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Session Tracking Example";
HttpSession session = request.getSession(true);
String heading;
// Use getAttribute instead of getValue in version 2.2.
Integer accessCount =
(Integer)session.getValue("accessCount");
if (accessCount == null) {
accessCount = new Integer(0);
heading = "Welcome, Newcomer";
} else {
heading = "Welcome Back";
accessCount = new Integer(accessCount.intValue() + 1);
}
// Use setAttribute instead of putValue in version 2.2.
session.putValue("accessCount", accessCount);

out.println(ServletUtilities.headWithTitle(title) +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1 ALIGN=\"CENTER\">" + heading + "</H1>\n" +
"<H2>Information on Your Session:</H2>\n" +
"<TABLE BORDER=1 ALIGN=\"CENTER\">\n" +
"<TR BGCOLOR=\"#FFAD00\">\n" +
" <TH>Info Type<TH>Value\n" +
"<TR>\n" +
" <TD>ID\n" +
" <TD>" + session.getId() + "\n" +
"<TR>\n" +
" <TD>Creation Time\n" +
" <TD>" +
new Date(session.getCreationTime()) + "\n" +
"<TR>\n" +
" <TD>Time of Last Access\n" +
" <TD>" +
new Date(session.getLastAccessedTime()) + "\n" +
"<TR>\n" +
" <TD>Number of Previous Accesses\n" +
" <TD>" + accessCount + "\n" +
"</TABLE>\n" +
"</BODY></HTML>");
}
/** Handle GET and POST requests identically. */

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
To create your own session variables, use session.setAttribute("name") method......and natch getAttribute("name") to access them.
17 years ago
OK - here is a little servlet you can employ....all you do is pass the JSP file reference to it.
This code can be modified to selectively seek the tags you wish to highlight and add the proper CSS class= designation or FONT attributes, etc.
---------------------------------------------------------------
import java.net.*;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class JSPSourceServlet extends HttpServlet {
/**
* Handles a GET request by sending the contents of the resource
* specified by the servlet path to the client.
*
* @param req an HttpServletRequest
* @param res an HttpServletResponse
* @exception IOException
*/
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException {
// Get the pathInfo
String path = req.getPathInfo();

// Check for invalid characters in the path
if (path.indexOf("..") != -1 | | path.toUpperCase().indexOf("WEB-INF") != -1) {
res.sendError(HttpServletResponse.SC_FORBIDDEN,
"Illegal characters found in path " + path);
return;
}
sendFile(path, res);
}
/**
* Copies the "in" reader to the "out" writer.
*
* @param in a BufferedReader
* @param out a PrintWriter
*/
private void copyFile(BufferedReader in, PrintWriter out)
throws IOException {
int chars;
char[] buf = new char[4096];
while ((chars = in.read(buf, 0, buf.length)) != -1) {
out.write(buf, 0, chars);
out.flush();
}
}

/**
* Sends the contents of a file to the client.
*
* @param pathInfo the file URI
* @param res an HttpServletResponse
* @exception IOException
*/
private void sendFile(String path, HttpServletResponse res) throws IOException {
String fileName = getServletContext().getRealPath(path);
if (fileName == null) {
res.sendError(HttpServletResponse.SC_NOT_FOUND,
path + " not found.");
return;
}

File file = new File(fileName);
if (file.isDirectory() | | !file.canRead()) {
res.sendError(HttpServletResponse.SC_FORBIDDEN,
"No read access to " + file.getAbsolutePath());
return;
}
res.setStatus(HttpServletResponse.SC_OK);
// Use "text/html" and convert HTML characters to
// character entities, to fool IE as explained above
res.setContentType("text/html");
long lastMod = file.lastModified();
if (lastMod != 0) {
res.setDateHeader("Last-modified", lastMod);
}
PrintWriter out = res.getWriter();
// Add HTML header
out.println("<html><head><title>" + path + "</title></head>");
out.println("<body><pre>");

// Read the file and convert it
BufferedReader in = new BufferedReader(new FileReader(file));
StringWriter rawText = new StringWriter();
copyFile(in, new PrintWriter(rawText));
in.close();
out.write(toHTMLString(rawText.toString()));

// Add HTML footer
out.println("</pre></body></html>");
out.close();
}
}
// convert special characters to browser-safe equivalents
public static String toHTMLString(String in) {
StringBuffer out = new StringBuffer();
for (int i = 0; in != null && i < in.length(); i++) {
char c = in.charAt(i);
if (c == '\'') {
out.append("'");
}
else if (c == '\"') {
out.append(""");
}
else if (c == '<') {
out.append("<");
}
else if (c == '>') {
out.append(">");
}
else if (c == '&') {
out.append("&");
}
else {
out.append(c);
}
}
return out.toString();
}
17 years ago
Heck - there's a good example provided with Tomcat:
Just remember, cookies are nothing more than small files stored on the user's hard drive that have a certain expiration date. You can store up to 4k of string data in any cookie. If you encode your data properly, you can remember all sorts of things....userid, password, preferences, etc.
Always remember, the user can delete their cookies at any time or turn them off at the browser....therefore it is always good practice to check for this condition first.....
Tomcat and other apps use a single cookie to track the sessionid of the user and the cookie name is the same as the session id.

/* $Id: CookieExample.java,v 1.1.1.1 1999/10/09 00:19:59 duncan Exp $
*
*/
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* Example servlet showing request headers
*
* @author James Duncan Davidson <duncan@eng.sun.com>
*/
public class CookieExample extends HttpServlet {
ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body bgcolor=\"white\">");
out.println("<head>");
String title = rb.getString("cookies.title");
out.println("<title>" + title + "</title>");
out.println("</head>");
out.println("<body>");
// relative links
// XXX
// making these absolute till we work out the
// addition of a PathInfo issue

out.println("<a href=\"/examples/servlets/cookies.html\">");
out.println("<img src=\"/examples/images/code.gif\" height=24 " +
"width=24 align=right border=0 alt=\"view code\"></a>");
out.println("<a href=\"/examples/servlets/index.html\">");
out.println("<img src=\"/examples/images/return.gif\" height=24 " +
"width=24 align=right border=0 alt=\"return\"></a>");
out.println("<h3>" + title + "</h3>");
Cookie[] cookies = request.getCookies();
if (cookies.length > 0) {
out.println(rb.getString("cookies.cookies") + "<br>");
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
out.print("Cookie Name: " + cookie.getName() + "<br>");
out.println(" Cookie Value: " + cookie.getValue() +
"<br><br>");
}
} else {
out.println(rb.getString("cookies.no-cookies"));
}
String cookieName = request.getParameter("cookiename");
String cookieValue = request.getParameter("cookievalue");
if (cookieName != null && cookieValue != null) {
Cookie cookie = new Cookie(cookieName, cookieValue);
response.addCookie(cookie);
out.println("<P>");
out.println(rb.getString("cookies.set") + "<br>");
out.print(rb.getString("cookies.name") + " " + cookieName +
"<br>");
out.print(rb.getString("cookies.value") + " " + cookieValue);
}

out.println("<P>");
out.println(rb.getString("cookies.make-cookie") + "<br>");
out.print("<form action=\"");
out.println("CookieExample\" method=POST>");
out.print(rb.getString("cookies.name") + " ");
out.println("<input type=text length=20 name=cookiename><br>");
out.print(rb.getString("cookies.value") + " ");
out.println("<input type=text length=20 name=cookievalue><br>");
out.println("<input type=submit></form>");


out.println("</body>");
out.println("</html>");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}
}
17 years ago
Check the Tomcat start-up script....it may not be checking for ALL jars in the directory.....
if not, simply modify and add the J2EE.jar to the CLASSPATH setting.
In the windows version, it automatically detects all jars in the lib directory...
17 years ago
www.hostjsp.com looks popular.....
not cheap, but they offer a nice package with Allaire's JRun...
and they use "big pipes"
17 years ago
Be sure you have configured Apache's HTTP.CONF file...and see instructions below (from the Tomcat docs):

This most likely means that Tomcat is trying to use a port that is already being used by someone else - usually Apache or another instance of Tomcat. By default, Tomcat's comes configured to run an HTTP server on port 8080. If you examine the supplied server.xml file, you'll see the following element:
<!-- Normal HTTP -->
<Connector className="org.apache.tomcat.service.PoolTcpConnector">
<Parameter name="handler"
value="org.apache.tomcat.service.http.HttpConnectionHandler"/>
<Parameter name="port"
value="8080"/>
</Connector>
To disable this, just comment out the entire <Connector> element. Otherwise, just change it to a port that doesn't conflict with Apache. If you're not going to use the Tomcat HTTP server, and you probably won't be if you are reading this document, disable it.
If you're running Apache / JServ, JServ may be clashing with Tomcat on port 8007
17 years ago
The application-wide setting can be controlled in the deployement descriptor file: web.xml
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
Remember, there is usually a default web.xml and then one that applies to your application ...which can override the default settings.
Note: there is also a ServletConfig method for changing this setting dynamically.
17 years ago
1) make sure you close, then reopen your browser when you do this test
2) check your app server configuration....
server.xml with Tomcat.
Tomcat is notorious for doing this.....
check your Contextpath settings....
17 years ago
The Javascript only acts if placed within the CALLED url.
In the CALLING JSP:
<%
url = url+"#popup; // indicate a pop-up (new) window is needed
response.sendRedirect(url);
%>
In the CALLED JSP, make sure this code is present:
<script>
if (window.location.hash=="#popup")
window.open(window.location)
</script>
17 years ago
try adding:
<% response.setIntHeader("Refresh",10) %>
This should cause the browser to refresh the page every 10 seconds.
17 years ago
You've got to force a response somehow.....
best advice ?
Wait till servlet 2.3 specs are completed.....
the new event driven model will help you do this....
and it will be much easier.
17 years ago
http://www.javaworld.com/javaworld/jw-05-2001/jw-0504-cache.html
see above link for a cool, new tag library that will cache in the server session, any object.....any period of time....
this is not an EJB solution however, strictly JSP/Servlets...
Check on the netscape support website.....
but I would be very surprised if you could do this.....
that type of control is handled by the mail client internally....
it will probably work OK as long as the user has his client enabled to transmit in HTML format...
otherwise, it will be converted to text.
17 years ago