Tag Archives: JSP

StringBean.java Bean used to demonstrate jsp:useBean, etc. Remember to install it in the WEB-INF/classes/cwp directory.

Huge Sell on Popular Electronics

StringBean.java  Bean used to demonstrate jsp:useBean, etc. Remember to install it in the WEB-INF/classes/cwp directory. 

package cwp;

/** A simple bean that has a single String property
 *  called message.
 *  <P>
 *  Taken from Core Web Programming Java 2 Edition
 *  from Prentice Hall and Sun Microsystems Press,
 *  .
 *  May be freely used or adapted.
 */

public class StringBean {
  private String message = "No message specified";

  public String getMessage() {
    return(message);
  }

  public void setMessage(String message) {
    this.message = message;
  }
}

PluginApplet.jsp Page that demonstrates the use of jsp:plugin.

Huge Sell on Popular Electronics

PluginApplet.jsp  Page that demonstrates the use of jsp:plugin. Requires you to compile and install PluginApplet.java, TextPanel.java, DrawingPanel.java, and WindowUtilities.java  Since these are classes sent to the client to used by applets, the .class files should be in the same directory as the JSP page, not in the WEB-INF/classes directory where classes the server uses go. Uses the JSP-Styles  style sheet. Warning: Allaire JRun 3.0 SP2 does not properly support jsp:plugin.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- 
Example of using jsp:plugin for an applet that uses Java 2. 

Taken from Core Web Programming Java 2 Edition
from Prentice Hall and Sun Microsystems Press,
.
May be freely used or adapted.
-->
<HTML>
<HEAD>
<TITLE>Using jsp:plugin</TITLE>
<LINK REL=STYLESHEET
      HREF="JSP-Styles.css"
      TYPE="text/css">
</HEAD>

<BODY>

<TABLE BORDER=5 ALIGN="CENTER">
  <TR><TH CLASS="TITLE">
      Using jsp:plugin</TABLE>
<P>
<CENTER>
<jsp:plugin type="applet" 
            code="PluginApplet.class"
            width="370" height="420">
</jsp:plugin>
</CENTER>

</BODY>
</HTML>

PluginApplet.java

import javax.swing.*;

/** An applet that uses Swing and Java 2D and thus requires
 *  the Java Plug-In.
 *  <P>
 *  Taken from Core Web Programming Java 2 Edition
 *  from Prentice Hall and Sun Microsystems Press,
 *  .
 *  May be freely used or adapted.
 */

public class PluginApplet extends JApplet {
  public void init() {
    WindowUtilities.setNativeLookAndFeel();
    setContentPane(new TextPanel());
  }
}


TextPanel.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/** JPanel that places a panel with text drawn at various angles
 *  in the top part of the window and a JComboBox containing
 *  font choices in the bottom part.
 *  <P>
 *  Taken from Core Web Programming Java 2 Edition
 *  from Prentice Hall and Sun Microsystems Press,
 *  .
 *  May be freely used or adapted.
 */

public class TextPanel extends JPanel
                       implements ActionListener {
  private JComboBox fontBox;
  private DrawingPanel drawingPanel;

  public TextPanel() {
    GraphicsEnvironment env =
      GraphicsEnvironment.getLocalGraphicsEnvironment();
    String[] fontNames = env.getAvailableFontFamilyNames();
    fontBox = new JComboBox(fontNames);
    setLayout(new BorderLayout());
    JPanel fontPanel = new JPanel();
    fontPanel.add(new JLabel("Font:"));
    fontPanel.add(fontBox);
    JButton drawButton = new JButton("Draw");
    drawButton.addActionListener(this);
    fontPanel.add(drawButton);
    add(fontPanel, BorderLayout.SOUTH);
    drawingPanel = new DrawingPanel();
    fontBox.setSelectedItem("Serif");
    drawingPanel.setFontName("Serif");
    add(drawingPanel, BorderLayout.CENTER);
  }

  public void actionPerformed(ActionEvent e) {
    drawingPanel.setFontName((String)fontBox.getSelectedItem());
    drawingPanel.repaint();
  }
}


DrawingPanel.java
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

/** A window with text drawn at an angle. The font is
 *  set by means of the setFontName method.
 *  <P>
 *  Taken from Core Web Programming Java 2 Edition
 *  from Prentice Hall and Sun Microsystems Press,
 *  .
 *  May be freely used or adapted.
 */

class DrawingPanel extends JPanel {
  private Ellipse2D.Double circle =
    new Ellipse2D.Double(10, 10, 350, 350);
  private GradientPaint gradient =
    new GradientPaint(0, 0, Color.red, 180, 180, Color.yellow,
                      true); // true means to repeat pattern
  private Color[] colors = { Color.white, Color.black };

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D)g;
    g2d.setPaint(gradient);
    g2d.fill(circle);
    g2d.translate(185, 185);
    for (int i=0; i<16; i++) {
      g2d.rotate(Math.PI/8.0);
      g2d.setPaint(colors[i%2]);
      g2d.drawString("jsp:plugin", 0, 0);
    }
  }

  public void setFontName(String fontName) {
    setFont(new Font(fontName, Font.BOLD, 35));
  }
}


WindowUtilities.java

import javax.swing.*;
import java.awt.*;

/** A few utilities that simplify using windows in Swing.
 *  <P>
 *  Taken from Core Web Programming Java 2 Edition
 *  from Prentice Hall and Sun Microsystems Press,
 *  .
 *  May be freely used or adapted.
 */

public class WindowUtilities {

  /** Tell system to use native look and feel, as in previous
   *  releases. Metal (Java) LAF is the default otherwise.
   */

  public static void setNativeLookAndFeel() {
    try {
      UIManager.setLookAndFeel
        (UIManager.getSystemLookAndFeelClassName());
    } catch(Exception e) {
      System.out.println("Error setting native LAF: " + e);
    }
  }

  public static void setJavaLookAndFeel() {
    try {
      UIManager.setLookAndFeel
        (UIManager.getCrossPlatformLookAndFeelClassName());
    } catch(Exception e) {
      System.out.println("Error setting Java LAF: " + e);
    }
  }

  public static void setMotifLookAndFeel() {
    try {
      UIManager.setLookAndFeel
        ("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
    } catch(Exception e) {
      System.out.println("Error setting Motif LAF: " + e);
    }
  }

  /** A simplified way to see a JPanel or other Container.
   *  Pops up a JFrame with specified Container
   *  as the content pane.
   */

  public static JFrame openInJFrame(Container content,
                                    int width,
                                    int height,
                                    String title,
                                    Color bgColor) {
    JFrame frame = new JFrame(title);
    frame.setBackground(bgColor);
    content.setBackground(bgColor);
    frame.setSize(width, height);
    frame.setContentPane(content);
    frame.addWindowListener(new ExitListener());
    frame.setVisible(true);
    return(frame);
  }

  /** Uses Color.white as the background color. */

  public static JFrame openInJFrame(Container content,
                                    int width,
                                    int height,
                                    String title) {
    return(openInJFrame(content, width, height, title, Color.white));
  }

  /** Uses Color.white as the background color, and the
   *  name of the Container's class as the JFrame title.
   */

  public static JFrame openInJFrame(Container content,
                                    int width,
                                    int height) {
    return(openInJFrame(content, width, height,
                        content.getClass().getName(),
                        Color.white));
  }
}

BGColor.jsp Page that demonstrates JSP scriptlets. Uses the JSP-Styles style sheet.

Huge Sell on Popular Electronics

BGColor.jsp  Page that demonstrates JSP scriptlets. Uses the JSP-Styles  style sheet.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--    
Taken from Core Web Programming Java 2 Edition
from Prentice Hall and Sun Microsystems Press,
.
May be freely used or adapted.
-->
<HTML>
<HEAD>
  <TITLE>Color Testing</TITLE>
</HEAD>
<% 
String bgColor = request.getParameter("bgColor");
boolean hasExplicitColor = true;
if (bgColor == null) {
  hasExplicitColor = false;
  bgColor = "WHITE";
} 
%>
<BODY BGCOLOR="<%= bgColor %>">
<H2 ALIGN="CENTER">Color Testing</H2>
<%
if (hasExplicitColor) {
  out.println("You supplied an explicit background color of " +
              bgColor + ".");
} else {
  out.println("Using default background color of WHITE. " +
              "Supply the bgColor request attribute to try " +
              "a standard color or RRGGBB value, or to see " +
              "if your browser supports X11 color names.");
}
%>
</BODY>
</HTML>

Excel.jsp Page that demonstrates the use of JSP to build Excel spreadsheets

Huge Sell on Popular Electronics

Excel.jsp  Page that demonstrates the use of JSP to build Excel spreadsheets

First    Last    Email Address
Marty    Hall    hall@corewebprogramming.com
Larry    Brown    brown@corewebprogramming.com
Bill    Gates    gates@sun.com
Larry    Ellison    ellison@microsoft.com
<%@ page contentType="application/vnd.ms-excel" %>
<%-- There are tabs, not spaces, between columns. --%>

Expressions.jsp Page that demonstrates JSP expressions. Uses the JSP-Styles style sheet.

Huge Sell on Popular Electronics

Expressions.jsp  Page that demonstrates JSP expressions. Uses the JSP-Styles  style sheet. 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- 
Example of JSP Expressions. 
   
Taken from Core Web Programming Java 2 Edition
from Prentice Hall and Sun Microsystems Press,
.
May be freely used or adapted.
-->
<HTML>
<HEAD>
<TITLE>JSP Expressions</TITLE>
<META NAME="keywords"
      CONTENT="JSP,expressions,JavaServer,Pages,servlets">
<META NAME="description"
      CONTENT="A quick example of JSP expressions.">
<LINK REL=STYLESHEET
      HREF="JSP-Styles.css"
      TYPE="text/css">
</HEAD>

<BODY>
<H2>JSP Expressions</H2>
<UL>
  <LI>Current time: <%= new java.util.Date() %>
  <LI>Your hostname: <%= request.getRemoteHost() %>
  <LI>Your session ID: <%= session.getId() %>
  <LI>The <CODE>testParam</CODE> form parameter:
      <%= request.getParameter("testParam") %>
</UL>
</BODY>
</HTML>

ImportAttribute.jsp Page that demonstrates the import attribute of the page directive. Uses the ServletUtilities class

Huge Sell on Popular Electronics

ImportAttribute.jsp  Page that demonstrates the import attribute of the page directive. Uses the ServletUtilities class (Check Servlet Section)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--
Example of the import attribute of the page directive.

Taken from Core Web Programming Java 2 Edition
from Prentice Hall and Sun Microsystems Press,
.
May be freely used or adapted.
-->
<HTML>
<HEAD>
<TITLE>The import Attribute</TITLE>
<LINK REL=STYLESHEET
HREF="JSP-Styles.css"
TYPE="text/css">
</HEAD>

<BODY>
<H2>The import Attribute</H2>
<%-- JSP page directive --%>
<%@ page import="java.util.*,cwp.*" %>

<%-- JSP Declaration --%>
<%!
private String randomID() {
int num = (int)(Math.random()*10000000.0);
return("id" + num);
}

private final String NO_VALUE = "<I>No Value</I>";
%>

<%-- JSP Scriptlet --%>
<%
Cookie[] cookies = request.getCookies();
String oldID =
ServletUtilities.getCookieValue(cookies, "userID", NO_VALUE);
if (oldID.equals(NO_VALUE)) {
String newID = randomID();
Cookie cookie = new Cookie("userID", newID);
response.addCookie(cookie);
}
%>

<%-- JSP Expressions --%>
This page was accessed on <%= new Date() %> with a userID
cookie of <%= oldID %>.

</BODY>
</HTML>

AccessCounts.jsp Page that demonstrates JSP declarations.

Huge Sell on Popular Electronics

AccessCounts.jsp  Page that demonstrates JSP declarations.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--   
Taken from Core Web Programming Java 2 Edition
from Prentice Hall and Sun Microsystems Press,
.
May be freely used or adapted.
-->
<HTML>
<HEAD>
<TITLE>JSP Declarations</TITLE>
<META NAME="keywords"
      CONTENT="JSP,declarations,JavaServer,Pages,servlets">
<META NAME="description"
      CONTENT="A quick example of JSP declarations.">
<LINK REL=STYLESHEET
      HREF="JSP-Styles.css"
      TYPE="text/css">
</HEAD>

<BODY>
<H1>JSP Declarations</H1>

<%! private int accessCount = 0; %>
<H2>Accesses to page since server reboot: 
<%= ++accessCount %></H2>

</BODY>
</HTML>

ContactSection.jsp A snippet of a JSP page. It defines a field (accessCount), so pages that include it and want to directly utilize that field must use the include directive, not jsp:include.

Huge Sell on Popular Electronics

ContactSection.jsp হচ্ছে JSP পৃষ্ঠার একটি অংশ।এটি একটি ফিল্ডকে সঙ্গায়িত করে (accessCount), সুতরাং যে পৃষ্ঠায় এটি অন্তর্ভুক্ত থাকে এবং সরাসরি উক্ত ফিল্ড ব্যবহার করাতে চায় তাকে আবশ্যই include directive ব্যবহার করতে হবে, jsp:include নয়।.

<%@ page import="java.util.Date" %>

<%-- The following become fields in each servlet that
     results from a JSP page that includes this file. --%>
<%! 
private int accessCount = 0;
private Date accessDate = new Date();
private String accessHost = "<I>No previous access</I>";
%>

<P>
<HR>
This page © 2000 
<A HREF="http//www.my-company.com/">my-company.com</A>.
This page has been accessed <%= ++accessCount %>
times since server reboot. It was last accessed from 
<%= accessHost %> at <%= accessDate %>.

<% accessHost = request.getRemoteHost(); %>
<% accessDate = new Date(); %>

Expressions.jsp Page that demonstrates JSP expressions. Uses the JSP-Styles style sheet.

Huge Sell on Popular Electronics

Expressions.jsp  Page that demonstrates JSP expressions. Uses the JSP-Styles  style sheet. 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- 
Example of JSP Expressions. 
   
Taken from Core Web Programming Java 2 Edition
from Prentice Hall and Sun Microsystems Press,
.
May be freely used or adapted.
-->
<HTML>
<HEAD>
<TITLE>JSP Expressions</TITLE>
<META NAME="keywords"
      CONTENT="JSP,expressions,JavaServer,Pages,servlets">
<META NAME="description"
      CONTENT="A quick example of JSP expressions.">
<LINK REL=STYLESHEET
      HREF="JSP-Styles.css"
      TYPE="text/css">
</HEAD>

<BODY>
<H2>JSP Expressions</H2>
<UL>
  <LI>Current time: <%= new java.util.Date() %>
  <LI>Your hostname: <%= request.getRemoteHost() %>
  <LI>Your session ID: <%= session.getId() %>
  <LI>The <CODE>testParam</CODE> form parameter:
      <%= request.getParameter("testParam") %>
</UL>
</BODY>
</HTML>

JSP মধ্যে কাস্টম ট্যাগ্স নির্মাণ

Huge Sell on Popular Electronics

JSP মধ্যে কাস্টম ট্যাগ্স নির্মাণ:

JSP মধ্যে কাস্টম ট্যাগ তৈরি করতে কিভাবে?
আপনি JSP মধ্যে কাস্টম ট্যাগ তৈরি করতে পারেন.
ধাপ:
১. আপনি কাস্টম ট্যাগ অপারেশন নির্ধারণ করা হবে এমন একটি জাভা ফাইল তৈরি করতে হবে ।
২. জাভা ফাইলের জন্য আপনাকে javax.servlet.jsp.tagext.BodyTagSupport প্রসারিত করতে হবে ।
৩. আপনার বাস্তবায়ন, আপনি পুনরায় লিখুন(পুনর্লিখন) doStartTag(), doEndTag()এবং doAfterBody() পদ্ধতিগুল ।
৪. ট্যাগ লাইব্রেরি বর্ণনাকারী ফাইল (.TLD এক্সটেনশন সঙ্গে XML ফাইল) তৈরি করুন ।

উদাহরণঃ

একটি পংক্তি reverses যে একটি ট্যাগ বাস্তবায়ন

import java.io.IOException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class ReverseTag extends BodyTagSupport
{
private static final long serialVersionUID = 1L;

//override doStartTag
public int doStartTag() throws JspTagException{
return EVAL_BODY_TAG;
}

//override doEndTag
public int doEndTag() throws JspTagException
{
try {
JspWriter out = pageContext.getOut();
} catch (Exception ex) {
throw new JspTagException("Exception" + ex);
}
return SKIP_BODY;
}

//reverse the text
public int doAfterBody() throws JspTagException
{
BodyContent body = getBodyContent();
try {
JspWriter out = body.getEnclosingWriter();
//get text inside the tag
String bodyContent = body.getString();
//reverse the text
if (bodyContent != null) {
for (int i = bodyContent.length() - 1; i >= 0; i--) {
out.print(bodyContent.charAt(i));
}
}
out.println();
body.clearBody(); // Clear for next evaluation
} catch (IOException ioe) {
throw new JspTagException("Exception at doAfterBody " + ioe);
}
return (SKIP_BODY);
}
}

৫. Taglib বর্ণনাকারী তৈরি করুন

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.
//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname></shortname>
<info></info>
<tag>
<name>stringreverse</name>
<tagclass>net.justetc.taglibrary.ReverseTag</tagclass>
<info>
Reverse the text
</info>
</tag>
</taglib>

৬. কাস্টম ট্যাগ উদাহরণ ব্যবহার

<%@ taglib uri="/WEB-INF/reverse.tld" prefix="reverse" %>

<html>
<head>
<title>Custom Tag library</title>
</head>

<body bgcolor="#ffffff">

<hr />
<reverse:stringreverse>
justetc
</reverse:stringreverse>
<hr />
</body>
</html>

2. Provide the link to our article that you used to write your article:
Your link:
http://salearningschool.com/searchResult.php?queryStr=java&submit=Search+Database

sub link from it:

http://salearningschool.com/displayArticle.php?table=Articles&articleID=668