<html> <head> <script type="text/javascript"> function getElements() { var x=document.getElementsByName("myInput[]"); alert(x.length); alert(x[0].value); alert(x[1].value); alert(x[2].value); } </script> </head> <body> <input name="myInput[]" type="checkbox" size="20" value='10'/>10 <input name="myInput[]" type="checkbox" size="20" value='20' />20 <input name="myInput[]" type="checkbox" size="20" value='30' />30
Aug 29
GetElementsByName an example: Checkbox
Aug 29
IfExample.jsp Page that uses the custom nested tags
IfExample.jsp Page that uses the custom nested tags <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <!-- Illustration of IfTag tag. Taken from Core Web Programming Java 2 Edition from Prentice Hall and Sun Microsystems Press, . May be freely used or adapted. --> <HTML> <HEAD> <TITLE>If Tag Example</TITLE> <LINK REL=STYLESHEET HREF="JSP-Styles.css" TYPE="text/css"> </HEAD> <BODY> <H1>If Tag Example</H1> <%@ taglib uri="cwp-taglib.tld" prefix="cwp" %> <cwp:if> <cwp:condition>true</cwp:condition> <cwp:then>Condition is true</cwp:then> <cwp:else>Condition is false</cwp:else> </cwp:if> <P> <cwp:if> <cwp:condition><%= request.isSecure() %></cwp:condition> <cwp:then>Request is using SSL (https)</cwp:then> <cwp:else>Request is not using SSL</cwp:else> </cwp:if> <P> Some coin tosses:<BR> <cwp:repeat reps="10"> <cwp:if> <cwp:condition><%= Math.random() < 0.5 %></cwp:condition> <cwp:then><B>Heads</B><BR></cwp:then> <cwp:else><B>Tails</B><BR></cwp:else> </cwp:if> </cwp:repeat> </BODY> </HTML>
Aug 29
Driver template to create and start a Thread object.
** Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ public class DriverClass extends SomeClass { ... public void startAThread() { // Create a Thread object. ThreadClass thread = new ThreadClass(); // Start it in a separate process. thread.start(); ... } }
Aug 29
Pointers
/* The following code example is taken from the book * "The C++ Standard Library - A Tutorial and Reference" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */ #include <algorithm> #include <iostream> using namespace std; /* function that compares two pointers by comparing the values to which they po int */ bool int_ptr_less (int* a, int* b) { return *a < *b; } int main() { int x = 17; int y = 42; int* px = &x; int* py = &y; int* pmax; // call max() with special comparison function pmax = max (px, py, int_ptr_less); cout << *pmax; //... } /* 42 */
Aug 29
Valarray Example
/* The following code example is taken from the book * "The C++ Standard Library - A Tutorial and Reference" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */ #include <iostream> #include <valarray> using namespace std; // print valarray template <class T> void printValarray (const valarray<T>& va) { for (int i=0; i<va.size(); i++) { cout << va[i] << ' '; } cout << endl; } int main() { // create and initialize valarray with nine elements valarray<double> va(9); for (int i=0; i<va.size(); i++) { va[i] = i * 1.1; } // print valarray printValarray(va); // double values in the valarray va *= 2.0; // print valarray again printValarray(va); // create second valarray initialized by the values of the first plus 10 valarray<double> vb(va+10.0); // print second valarray printValarray(vb); // create third valarray as a result of processing both existing valarrays valarray<double> vc; vc = sqrt(va) + vb/2.0 - 1.0; // print third valarray printValarray(vc); } /* 0 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 0 2.2 4.4 6.6 8.8 11 13.2 15.4 17.6 10 12.2 14.4 16.6 18.8 21 23.2 25.4 27.6 */
Aug 29
Statics.java Demonstrates static and non-static methods.
*/ public class Statics { public static void main(String[] args) { staticMethod(); Statics s1 = new Statics(); s1.regularMethod(); } public static void staticMethod() { System.out.println("This is a static method."); } public void regularMethod() { System.out.println("This is a regular method."); } }
Aug 29
Batton’s java
import java.applet.Applet; import java.awt.*; /././././././ public class Buttons extends Applet { private Button button1, button2, button3; public void init() { button1 = new Button("Button One"); button2 = new Button("Button Two"); button3 = new Button("Button Three"); add(button1); add(button2); add(button3); } } /././././././././.
Aug 28
IfTag.java, IfConditionTag.java, IfThenTag.java, and IfElseTag.java, Custom tags that make use of tag nesting
IfTag.java, IfConditionTag.java, IfThenTag.java, and IfElseTag.java, Custom tags that make use of tag nesting IfTag.java package cwp.tags; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; import javax.servlet.*; /** A tag that acts like an if/then/else. * <P> * Taken from Core Web Programming Java 2 Edition * from Prentice Hall and Sun Microsystems Press, * . * May be freely used or adapted. */ public class IfTag extends TagSupport { private boolean condition; private boolean hasCondition = false; public void setCondition(boolean condition) { this.condition = condition; hasCondition = true; } public boolean getCondition() { return(condition); } public void setHasCondition(boolean flag) { this.hasCondition = flag; } /** Has the condition field been explicitly set? */ public boolean hasCondition() { return(hasCondition); } public int doStartTag() { return(EVAL_BODY_INCLUDE); } } package cwp.tags; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; import javax.servlet.*; /** The condition part of an if tag. * <P> * Taken from Core Web Programming Java 2 Edition * from Prentice Hall and Sun Microsystems Press, * . * May be freely used or adapted. */ public class IfConditionTag extends BodyTagSupport { public int doStartTag() throws JspTagException { IfTag parent = (IfTag)findAncestorWithClass(this, IfTag.class); if (parent == null) { throw new JspTagException("condition not inside if"); } return(EVAL_BODY_TAG); } public int doAfterBody() { IfTag parent = (IfTag)findAncestorWithClass(this, IfTag.class); String bodyString = getBodyContent().getString(); if (bodyString.trim().equals("true")) { parent.setCondition(true); } else { parent.setCondition(false); } return(SKIP_BODY); } } package cwp.tags; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; import javax.servlet.*; /** The else part of an if tag. * <P> * Taken from Core Web Programming Java 2 Edition * from Prentice Hall and Sun Microsystems Press, * . * May be freely used or adapted. */ public class IfElseTag extends BodyTagSupport { public int doStartTag() throws JspTagException { IfTag parent = (IfTag)findAncestorWithClass(this, IfTag.class); if (parent == null) { throw new JspTagException("else not inside if"); } else if (!parent.hasCondition()) { String warning = "condition tag must come before else tag"; throw new JspTagException(warning); } return(EVAL_BODY_TAG); } public int doAfterBody() { IfTag parent = (IfTag)findAncestorWithClass(this, IfTag.class); if (!parent.getCondition()) { try { BodyContent body = getBodyContent(); JspWriter out = body.getEnclosingWriter(); out.print(body.getString()); } catch(IOException ioe) { System.out.println("Error in IfElseTag: " + ioe); } } return(SKIP_BODY); } } package cwp.tags; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; import javax.servlet.*; /** The then part of an if tag. * <P> * Taken from Core Web Programming Java 2 Edition * from Prentice Hall and Sun Microsystems Press, * . * May be freely used or adapted. */ public class IfThenTag extends BodyTagSupport { public int doStartTag() throws JspTagException { IfTag parent = (IfTag)findAncestorWithClass(this, IfTag.class); if (parent == null) { throw new JspTagException("then not inside if"); } else if (!parent.hasCondition()) { String warning = "condition tag must come before then tag"; throw new JspTagException(warning); } return(EVAL_BODY_TAG); } public int doAfterBody() { IfTag parent = (IfTag)findAncestorWithClass(this, IfTag.class); if (parent.getCondition()) { try { BodyContent body = getBodyContent(); JspWriter out = body.getEnclosingWriter(); out.print(body.getString()); } catch(IOException ioe) { System.out.println("Error in IfThenTag: " + ioe); } } return(SKIP_BODY); } }
Aug 28
FilterExample.jsp Page that uses the FilterTag custom tag
FilterExample.jsp Page that uses the FilterTag custom tag <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <!-- Illustration of FilterTag tag. Taken from Core Web Programming Java 2 Edition from Prentice Hall and Sun Microsystems Press, . May be freely used or adapted. --> <HTML> <HEAD> <TITLE>HTML Logical Character Styles</TITLE> <LINK REL=STYLESHEET HREF="JSP-Styles.css" TYPE="text/css"> </HEAD> <BODY> <H1>HTML Logical Character Styles</H1> Physical character styles (B, I, etc.) are rendered consistently in different browsers. Logical character styles, however, may be rendered differently by different browsers. Here's how your browser (<%= request.getHeader("User-Agent") %>) renders the HTML 4.0 logical character styles: <P> <%@ taglib uri="cwp-taglib.tld" prefix="cwp" %> <TABLE BORDER=1 ALIGN="CENTER"> <TR CLASS="COLORED"><TH>Example<TH>Result <TR> <TD><PRE><cwp:filter> <EM>Some emphasized text.</EM><BR> <STRONG>Some strongly emphasized text.</STRONG><BR> <CODE>Some code.</CODE><BR> <SAMP>Some sample text.</SAMP><BR> <KBD>Some keyboard text.</KBD><BR> <DFN>A term being defined.</DFN><BR> <VAR>A variable.</VAR><BR> <CITE>A citation or reference.</CITE> </cwp:filter></PRE> <TD> <EM>Some emphasized text.</EM><BR> <STRONG>Some strongly emphasized text.</STRONG><BR> <CODE>Some code.</CODE><BR> <SAMP>Some sample text.</SAMP><BR> <KBD>Some keyboard text.</KBD><BR> <DFN>A term being defined.</DFN><BR> <VAR>A variable.</VAR><BR> <CITE>A citation or reference.</CITE> </TABLE> </BODY> </HTML> package cwp.tags; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; import cwp.*; /** A tag that replaces <, >, ", and & with their HTML * character entities (<, >, ", and &). * After filtering, arbitrary strings can be placed * in either the page body or in HTML attributes. * <P> * Taken from Core Web Programming Java 2 Edition * from Prentice Hall and Sun Microsystems Press, * . * May be freely used or adapted. */ public class FilterTag extends BodyTagSupport { public int doAfterBody() { BodyContent body = getBodyContent(); String filteredBody = ServletUtilities.filter(body.getString()); try { JspWriter out = body.getEnclosingWriter(); out.print(filteredBody); } catch(IOException ioe) { System.out.println("Error in FilterTag: " + ioe); } // SKIP_BODY means we're done. If we wanted to evaluate // and handle the body again, we'd return EVAL_BODY_TAG. return(SKIP_BODY); } }
Aug 28
Demonstrates using a TexturePaint to fill an shape with a tiled image
^^^^^^^^^^^^^^^^^ TiledImages.java Demonstrates using a TexturePaint to fill an shape with a tiled image. Uses the following class and images: * ImageUtilities.java Simplifies creating a BufferedImage from an image file. ~~~~~~~~~~~~~~~~~~ import javax.swing.*; import java.awt.*; import java.awt.geom.*; import java.awt.image.*; /** An example of using TexturePaint to fill objects with tiled * images. Uses the getBufferedImage method of ImageUtilities * to load an Image from a file and turn that into a * BufferedImage. * **************** public class TiledImages extends JPanel { private String dir = System.getProperty("user.dir"); private String imageFile1 = dir + "/images/marty.jpg"; private TexturePaint imagePaint1; private Rectangle imageRect; private String imageFile2 = dir + "/images/bluedrop.gif"; private TexturePaint imagePaint2; private int[] xPoints = { 30, 700, 400 }; private int[] yPoints = { 30, 30, 600 }; private Polygon imageTriangle = new Polygon(xPoints, yPoints, 3); public TiledImages() { BufferedImage image = ImageUtilities.getBufferedImage(imageFile1, this); imageRect = new Rectangle(235, 70, image.getWidth(), image.getHeight()); imagePaint1 = new TexturePaint(image, imageRect); image = ImageUtilities.getBufferedImage(imageFile2, this); imagePaint2 = new TexturePaint(image, new Rectangle(0, 0, 32, 32)); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D)g; g2d.setPaint(imagePaint2); g2d.fill(imageTriangle); g2d.setPaint(Color.blue); g2d.setStroke(new BasicStroke(5)); g2d.draw(imageTriangle); g2d.setPaint(imagePaint1); g2d.fill(imageRect); g2d.setPaint(Color.black); g2d.draw(imageRect); } public static void main(String[] args) { WindowUtilities.openInJFrame(new TiledImages(), 750, 650); } } >>>>>>>>>>>>>> ImageUtilities.java Simplifies creating a BufferedImage from an image file. >>>>>>>>>>>>>>> import java.awt.*; import java.awt.image.*; /** A class that simplifies a few common image operations, in * particular, creating a BufferedImage from an image file and * using MediaTracker to wait until an image or several images * are done loading. * ******************** public class ImageUtilities { /** Create Image from a file, then turn that into a * BufferedImage. */ public static BufferedImage getBufferedImage(String imageFile, Component c) { Image image = c.getToolkit().getImage(imageFile); waitForImage(image, c); BufferedImage bufferedImage = new BufferedImage(image.getWidth(c), image.getHeight(c), BufferedImage.TYPE_INT_RGB); Graphics2D g2d = bufferedImage.createGraphics(); g2d.drawImage(image, 0, 0, c); return(bufferedImage); } /** Take an Image associated with a file, and wait until it is * done loading (just a simple application of MediaTracker). * If you are loading multiple images, don't use this * consecutive times; instead, use the version that takes * an array of images. */ public static boolean waitForImage(Image image, Component c) { MediaTracker tracker = new MediaTracker(c); tracker.addImage(image, 0); try { tracker.waitForAll(); } catch(InterruptedException ie) {} return(!tracker.isErrorAny()); } /** Take some Images associated with files, and wait until they * are done loading (just a simple application of * MediaTracker). */ public static boolean waitForImages(Image[] images, Component c) { MediaTracker tracker = new MediaTracker(c); for(int i=0; i