Tuesday, 30 October 2012

excel format for JSP

<%
response.setContentType(
"application/vnd.ms-excel");
response.setHeader(
"Content-Disposition","attachment;filename=\"CurrentCustomerList.xls\"");
response.setHeader(
"Pragma", "public");
response.setHeader(
"Cache-Control", "max-age=0");
Iterator iterator = (Iterator) request.getAttribute(
"iterator");%>

Comparator for User defined object compare

import

java.util.Comparator;
public
class CountyDTO {
private
String state;
private
String country;
public
void setState(String state) {
this
.state = state;
}
public
void setCountry(String country) {
this
.country = country;
}
public
String getState() {
return
state;
}
public
String getCountry() {
return
country;
}
/**
*
@author BM54284
* Comparater for sorting the States for Task 2
*/
public
static enum CountyComparator implements Comparator<CountyDTO> {
STATE_SORT
{
public
int compare(CountyDTO o1, CountyDTO o2) {
if
((null != o1 && null != o1.getState())
&& (null
!= o2 && null != o2.getState())) {
return
o1.getState().compareTo(o2.getState());
} else
{
return
0;
}
}
},
COUNTRY_SORT
{
public
int compare(CountyDTO o1, CountyDTO o2) {
if
((null != o1 && null != o1.getCountry())
&& (null
!= o2 && null != o2.getCountry())) {
return
o1.getCountry().compareToIgnoreCase(o2.getCountry());
} else
{
return
0;
}
}
};
public
static Comparator<CountyDTO> getComparator(
final
CountyComparator... multipleOptions) {
return
new Comparator<CountyDTO>() {
public
int compare(CountyDTO o1, CountyDTO o2) {
for
(CountyComparator option : multipleOptions) {
int
result = option.compare(o1, o2);
if
(result != 0) {
return
result;
}
}
return
0;
}
};
}
}
}




//Call to that comparater


List<CountyDTO> contryandstate = getCountyFinder().getState();
contryandstate.addAll(getAdditionalCounty());
Collections.sort(contryandstate, CountyComparator.getComparator(CountyComparator.COUNTRY_SORT
,CountyComparator.STATE_SORT));




Difference between class level and object locking and static object lock

1) Class level locking will lock entire class, so no other thread can access any of other synchronized blocks. 2) Object locking will lo...