ITTreats.com
Enter your Search term and Hit "Enter"

Wed, 10 Mar 2010 9:39:00 AM +00:00

Latest 'ITTreats'


Using Bean in JSP page

Using Bean in JSP page

This Article illustrates a method of using a Bean in a JSP page. To use a bean in a JSP page, three attributes must be supplied – an id, which provides a local name for the bean, the bean’s class name, which is used to instantiate the bean if it does not exit, and a scope, which specifies the lifetime of the bean.

There are four scopes available – page, request, session, and application. A page-scoped bean is available only within the JSP page and is destroyed when the page has finished generating its output for the request. A request-scoped bean is destroyed when the response is sent. A session-scoped bean is destroyed when the session is destroyed. An application-scoped bean is destroyed when the web application is destroyed.

This example retrieves a session-scoped bean and makes it available using the id myBean. The bean is automatically created if it does not exist:

1
2
 
<jsp:useBean id=“myBean” class=“com.mycompany.MyBean” scope=“session” />

If it is necessary to initialize the bean after it is created, the initialization code can be placed in the body of the jsp:useBean action. The body will not be executed if the bean already exists. Here is an example:

1
2
3
4
5
6
<jsp:useBean id=“myBean2″ class=“com.mycompany.MyBean” scope=“session”>
<%this body is executed only if the bean is created –%>
 
<%– intialize bean properties –%>
<jsp:setProperty name=“myBean2″ property=“prop1″ value=123/>
</jsp:useBean>

The value of a bean property can be included in the generated output using the jsp:getProperty action:

1
2
<jsp:getProperty name=“myBean” property=“prop1″ />
<jsp:getProperty name=“myBean” property=“prop2″ />

The jsp:useBean action also declares a local Java variable to hold the bean object. The name of the local variable is exactly the value of the id attribute. Here is an example that accesses the bean within a scriptlet:

1
2
3
4
5
6
7
<%
if (myBean.getProp1() % 2 == 0) {
out.println(<font color=\”red\”>+myBean.getProp1()+</font>);
} else {
out.println(<font color=\”green\”>+myBean.getProp1()+</font>);
}
%>

Here is the bean used in the example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.mycompany;
 
public class MyBean {
// Initialize with random values
int prop1 = (int)(Integer.MAX_VALUE*Math.random());
String prop2 = “”+Math.random();
 
public int getProp1() {
return prop1;
}
public void setProp1(int prop1) {
this.prop1 = prop1;
}
 
public String getProp2() {
return prop2;
}
public void setProp2(String prop2) {
this.prop2 = prop2;
}
}

obtain middle date in java for given start date and end date

obtain middle date in java for given start date and end date

Inorder to get  exact middle date of  given  start date and end date,  normal  conditions includes  a bit  complex programming,

But  Here is  simple solution  to get middle date of  given start date and end date  in java.

by using the below  small function you will be  returned with the  middle date  between  given start date and end date in java .

Here is the function :

 public Date middle(Date d1, Date d2) {
        long t1 = d1.getTime();
        long t2 = d2.getTime();
        return new Date(t1 + (t2 - t1)/2);

    }

Hmm, simple isn’t it, use the above function in retrieving middle date between given start date and end date in java

How to Make Dynamic Download Here Link

How to Make Dynamic Download Here Link

Many times, we will come across some situations seeing  Download Here  Links  on so many websites, Will they  place actual File in the Download Here Link ??   Answer  is No.  Then  How to acheive this  through Programmatically,  This Tutorial will guide you how you can  develope your own Dynamic Download Here Links, with out  giving out access to the actual file name.

Lets get into the discussion.

Inorder to acheive this Dynamic Download Here Link we have to play with   header()  in PHP.

PHP will give you flexibility to choose your  actual content-type .

Lets  see two examples How we can obtain this Dynamic Downlod Here link  for  PDF  and EXE ..

Dynamic Download Here link  for PDF  File

Here is the piece of code

Create Download.php  and  paste the following code into the file

1
2
3
4
5
6
7
8
9
10
11
12
13
< ?php
 
$file="GIVE_PATH_TO_ACTUAL_FILE_with_extension";
 
header('Content-type: application/pdf');
 
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="GIVE_MASKED_NAME"');
 
// The PDF source is in original.pdf
readfile('$file');
 
?>

The Above Code  will focus on  Only Core activity of Downloading a PDF File.  Depending on  our requirements we can customize to suit our application requirements.

1.  Handling Sessions to give away PDF to the  Authenticated users  Preventing others from download
2. Custom Naming of File Depending on the Time

More . . .