Archive

Archive for the ‘JSP’ Category

JavaBeans

February 1st, 2009 admin No comments

Requirements for JavaBeans:

  • there have to be set/get methods specified
  • has to have public default constructor (without parameters)
  • it should be in a package
  • serializable
  • get/set methods has to have the same data type!
  • bean cannot modify content of a page!

… in general, JavaBean is a standard Class.

It can be used as a simple component in a JSP page.

- get method has to be fast, and it should be plain and simple.

usage in a JSP page:

<jsp:useBean id=”nameOfBean” scope=”session” class=”myPackage.Counter”>

<jsp:setProperty name=”nameOfBean” property=”myCount”>

this is the same as

<%= Counter.myCount %>

with JSTL Expression language:

<c:out value=”${Counter.myCount}”>

automatically set the bean attributes from names (!important!):
<jsp:setProperty name=”pocetMiest” property=”*”>

Scope can be:

  • application - the bean is available in all application, when that’s in more JSP pages, one of them can create it once and other JSP pages can use it. It’s something like a global object for all application.
  • session - stored data, just for this session
  • request - just for request or forward
  • page - smallest scope, just on a page that is called (forwarded or included)

Popularity: 1% [?]

Categories: JSP Tags:

Open a File at any format in JSP

December 22nd, 2008 admin No comments

How to Open a File in Different Format

1.Create a HTML Page or JSP Page
2.Insert <%@ page contentType=”application/vnd.ms-excel” %> instead of <%@ page contentType=”application/html” %> in HTML Page or JSP Page
3.Open a File,Now its open in a Excel Sheet with Style apply in the Sheet.

For Microsoft Word

<%@ page contentType=”application/vnd.msword” %>

For PDF File

<%@ page contentType=”application/vnd.pdf” %>

For Wordpad

<%@ page contentType=”application/vnd.rtf” %>

For Microsoft Powerpoint

<%@ page contentType=”application/vnd.ms-powerpoint” %>

For Winzip

<%@ page contentType=”application/vnd.zip” %>

Popularity: 3% [?]

Categories: JSP Tags:

Conditional Tags in JSP

November 14th, 2008 admin No comments

The if tag allows the conditional execution of its body according to the value of the test attribute

<c:if test=”${not empty details.add}”>
It is Not a Empty ArrayList
</c:if>

<c:if test=”${empty details.add}”>
It is a Empty ArrayList
</c:if>

The choose tag performs conditional block execution by the embedded when subtags. It renders the body of the first when tag whose test condition evaluates to true. If none of the test conditions of nested when tags evaluates to true, then the body of an otherwise tag is evaluated, if present.

<c:choose>
<c:when test=”${bank.account== ’saving’}”>
Saving Account
</c:when>
<c:when test=”${bank.account == ‘RD’}”>
RD Account
</c:when>
<c:when test=”${bank.account == ’salary’}”>
Salary Account
</c:when>
<c:otherwise>

</c:otherwise>
</c:choose>

Popularity: 1% [?]

Categories: JSP Tags:

Taglibs in JSP

November 8th, 2008 admin No comments

Different Taglibs and its Usuage in JSP:

1.Core:http://java.sun.com/jsp/jstl/core
This Taglibs is used for Looping,Variable Support,Flow control,URL Management and miscellaneous

<%@taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c”%>

2.XML:http://java.sun.com/jsp/jstl/xml
This Taglibs is Used for Core,Flow Control and Transformation

<%@taglib uri=”http://java.sun.com/jsp/jstl/xml” prefix=”xl”%>

3.Internationalization:http://java.sun.com/jsp/jstl/fmt
This Taglibs is Used For Locale, Message formatting and Number & date formatting

<%@taglib uri=”http://java.sun.com/jsp/jstl/fmt” prefix=”fmt”%>

4.SQL:http://java.sun.com/jsp/jstl/sql
This Taglibs is Used For Sql Query Purpose

<%@ taglib uri=”http://java.sun.com/jsp/jstl/sql” prefix=”sql”%>

5.Functions:http://java.sun.com/jsp/jstl/functions
This Taglibs is Used For Collection length and String manipulation

<%@ taglib uri=”http://java.sun.com/jsp/jstl/functions” prefix=”fn”%>

Popularity: 2% [?]

Categories: JSP, Spring Tags:

Compare two ArrayList in JSP

November 2nd, 2008 admin No comments

Compare two ArrayList:

1.ArrayList are ListOfData and SecondList

2.Include this tag in the JSP Page

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>

3.Find the Length of the ArrayList Using fn:length tag

4.Compare the ArrayList as

<c:if test=”${not empty ListOfData}” >
<c:forEach var=”j” begin=”0″ end=”${fn:length(SecondList)-1}”>
<c:forEach var=”i” begin=”0″ end=”${fn:length(ListOfData)-1}”>

<tr>
<c:if test=”${SecondList[j].empid==ListOfData[i].empid}”>
<td>${ListOfData[i].empid} </td>
<td>${ListOfData[i].empname} </td>
<td>${ListOfData[i].empdest} </td>
</c:if>
</tr>
</c:forEach>
</c:forEach>
</c:if>

Popularity: 1% [?]

Categories: JSP Tags:

Pagination in JSP

October 27th, 2008 admin No comments

Pagination Using Display Tag :

If you would like to make use of the display taglib in your own application, do the following

1.Drop the displaytag-version.jar file in your application WEB-INF/lib directory
2.Make sure that following libraries are in your WEB-INF/lib directory (or made available via the classpath to your application server). Refer to the dependencies document for the correct version of these libraries. The following is the list of dependencies:

  • commons-logging
  • commons-lang
  • commons-collection
  • commons-beanutils
  • log4j
  • 3.Include this tag in the JSP File

    <%@ taglib uri=”http://displaytag.sf.net” prefix=”display”%>

    4.Using Display tag the Code is:
    1.Header Data Can be given in title and Corresponding Display data will be given in property in display:column tag
    Ex:

    <display:column property=”name” title=”Name”/>

    2.Array List name will be given in name,Default page Size will be given in pagesize and id will denote arraylist variable in display:table tag

    <display:table name=”requestScope.arraydetails” defaultsort=”7″ defaultorder=”ascending” pagesize=”10″ id=”list” requestURI=”" >

    <display:setProperty name=”paging.banner.group_size”<5>/display:setProperty>
    <display:setProperty name=”css.tr.even”<even_grid>/display:setProperty>
    <display:setProperty name=”css.tr.odd”<odd_grid>/display:setProperty>
    <display:setProperty name=”basic.msg.empty_list” value=” ” / >
    <display:setProperty name=”paging.banner.one_item_found” value=” ” />
    <display:setProperty name=”paging.banner.all_items_found” value=”Page ” />
    <display:setProperty name=”paging.banner.some_items_found” value=” ” />
    <display:setProperty name=”paging.banner.no_items_found” value=” ” />
    <display:setProperty name=”paging.banner.placement” value=”bottom” />

    <display:column property=”name” title=”Name”/>
    <display:column property=”age” title=”Age”/>
    <display:column property=”designation” title=”Designation”/>

    </display:table >

    Popularity: 2% [?]

    Categories: JSP Tags:

    JSP Applet tag

    October 26th, 2008 admin No comments

    How to Include Applet Program in a JSP Page:

    1.Create a Applet Java Program

    sample.java

    import javax.swing.JApplet;
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;  
     
    public class sample extends Applet {
     
    sampleCanvas canvas;       // The drawing area to display arcs
    public static String s=null;
     
    public void init() {
    setLayout(new BorderLayout());
    canvas = new sampleCanvas();
    add("Center", canvas);
    s=this.getParameter("data");
    }
     
    public void destroy() {
    remove(canvas);
    }
     
    public void start() {
    }
     
    public void stop() {
    }
     
    public void processEvent(AWTEvent e) {
    if (e.getID() == Event.WINDOW_DESTROY) {
    System.exit(0);
    }
    }
     
    }
     
    class sampleCanvas extends Canvas {
    font = new java.awt.Font("SansSerif", Font.PLAIN, 12);
     
    public void paint(Graphics g) {
    Rectangle r = getBounds();
     
    if(sample.s==null)
    sample.s="0";
    int a = Integer.parseInt(sample.s);
    a=(a*600)/1000;
     
    g.fillRect(10, 200,a, 25);
     
    }
     
    public void redraw(int value) {
    this.filled = filled;
    this.startAngle = value;
    repaint();
    }
    }

    2.Complie the Applet Program

    3.Import the applet Program in the JSP File

    <%@page import="com.sample">

    4.Include this Tag in the JSP File

    <jsp:plugin type="applet" code="sample.class" width="620" height="450">
    <jsp:params>
    <jsp:param name="data" value="10"/>
    </jsp:params>
    </jsp:plugin> 

    Popularity: 2% [?]

    Categories: JSP, Java Tags: , ,

    JSP Custom tags Example

    October 26th, 2008 admin No comments


    Custom tags are usually distributed in the form of a tag library, which defines a set of related custom tags and contains the objects that implement the tags.

    A Simple Custom tags Example:

    1.Create a Java Program

    2.import javax.servlet.jsp.*,javax.servlet.jsp.tagext.* packages

    3.Extends the TagSupport Class

    4.Java Program Contain two Default Methods.
    a)doStartTag
    b)doEndTag

    doStartTag:
    1.The doStartTag method is invoked when the start tag is encountered
    2.This method returns SKIP_BODY because a simple tag has no body

    doEndTag:
    1.The doEndTag method is invoked when the end tag is encountered
    2.This method returns SKIP_BODY because a simple tag has no body

    TagClass.java

    import javax.servlet.jsp.*;
    import javax.servlet.jsp.tagext.*;

    public class TagClass extends TagSupport{

    private String data=null;

    public void setData(String value){
    data= value;
    }

    public String getData(){
    return(data);
    }

    public int doStartTag()throws JspException{
    try{
    JspWriter out=pageContext.getOut();
    out.println(name);

    }catch(Exception e){}
    return SKIP_BODY;
    }

    public int doEndTag(){
    return SKIP_BODY;
    }

    }

    5.The Java Program Contain one Variable data(which has Getter and Setter Methods)

    6.Create tld File.

    7.We have to Create attribute For the Variable data

    8.Syntax For Creating attribute:

    <taglib>

    <tag>

    <attribute>
    <name>data</name>//Name Can be Same as we can given within the tags
    <required>true</required>//Requried Field Show Whether it is Mandatory or Not
    </attribute>

    <attribute>
    <name>id</name>
    <required>false</required>
    </attribute>

    </tag>

    </taglib>

    taglib.tld

    <?xml version=”1.0″ encoding=”ISO-8859-1″ ?>
    <!DOCTYPE taglib PUBLIC “-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN” “http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd”>

    <taglib>

    <tlib-version>1.0</tlib-version>

    <jsp-version>1.2</jsp-version>

    <short-name>form</short-name>

    <tag>

    <name>Show</name>
    <tag-class>com.TagClass</tag-class>
    <body-content>JSP</body-content>
    <description>Simple Customs tags Example</description>

    <attribute>
    <name>data</name>
    <required>true</required>
    </attribute>

    </tag>

    </taglib>

    9.Create a JSP Page.

    10.Include this tag at the begining of the JSP Page

    <%@ taglib uri=”/WEB-INF/tld/taglib.tld” prefix=”custom” %>

    11.Create this Tag at the JSP Page

    <custom:Show data=”Custom Tag Example”></custom:Show>

    12.Output:

    Custom Tag Example

    Popularity: 6% [?]

    Categories: JSP, Spring Tags:

    Custom tags in JSP

    October 26th, 2008 admin No comments

    Custom tags :
    Custom tags are usually distributed in the form of a tag library, which defines a set of related custom tags and contains the objects that implement the tags.

    Ex:-
    <%@ taglib uri="/WEB-INF/tld/taglib.tld" prefix="custom" %>

    what is Custom tags?

    A custom tag is a user-defined JSP language element. When a JSP page containing a custom tag is translated into a servlet, the tag is converted to operations on an object called a tag handler. The Web container then invokes those operations when the JSP page’s servlet is executed.

    Custom tags have a rich set of features. They can

    • Be customized via attributes passed from the calling page.
    • Access all the objects available to JSP pages.
    • Modify the response generated by the calling page.
    • Communicate with each other. You can create and initialize a JavaBeans component, create a variable that refers to that bean in one tag, and then use the bean in another tag.
    • Be nested within one another, allowing for complex interactions within a JSP page.

    Popularity: 1% [?]

    Categories: JSP Tags: