What is backing bean ?
A JavaBeans component that corresponds to a JSP page that includes JavaServer Faces components. The backing bean defines properties for the components on the page and methods that perform processing for the component. This processing includes event handling, validation, and processing associated with navigation.
What does web module contain?The web module contains:
- JSP files,
- class files for servlets,
- GIF and HTML files, and
- a Web deployment descriptor.
Web modules are packaged as JAR files with a .war (Web ARchive) extension.
Can you write code to sort the following string values naturally (i.e. in alphabetical order)? (JEE, Java, Servlets, JMS, JNDI, JDBC, JSP, and EJB)
Here is the sample code that makes use of the default compareTo( ) provided in the String class as it implements the Comparable interface and the Collections utility class that provides a sorting method, which internally uses the efficient “merge sort” algorithm.
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Sort1 {
public static void main(String[] args) {
List<string> values = Arrays.asList(“JEE”, “Java”, “Servlets”, “JMS”, “JNDI”, “JDBC”, “JSP”, “EJB”);
Collections.sort(values); // uses the default compareTo(String anotherString) in the String class
System.out.println(values);
}
}
Output:
[EJB, JDBC, JEE, JMS, JNDI, JSP, Java, Servlets]
Is there anything wrong with the above code?
Yes, 2 things — firstly, the above sort is case sensitive, that is the uppercase takes priority over lowercase pushing ‘Java’ after ‘JSP’. Secondly, if the collection had any null values, it will throw a NullpointerException.
These two issues can be rectified by providing a custom sorting implementation that ignores case and handles null values by pushing them to the end. The Collections class’s sort method takes a Comparator implementation as a second argument. In the code below, the Comparator has been implemented as an anonymous inner class. The compare(…) method will be called a number of times by the Collections.sort(list, comparator).
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Sort2 {
public static void main(String[] args) {
List<String> values = Arrays.asList(“JEE”, “Java”, null, “Servlets”, null, “JMS”, “JNDI”, “JDBC”, “JSP”, null,”EJB”);
//The comparator is defined as an anonymous inner class, but it can be
//defined in its own class. Handles nulls and ignores case
Collections.sort(values, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
//push the null values to the end
if(o1 == null){
if(o2 == null) {
return 0;
}
return 1;
}
else if(o2 == null){
return -1;
}
return o1.compareToIgnoreCase(o2);
}
}); // anonymous inner class end
System.out.println(values);
}
}
What if you have a specific scenario where you want to first sort by rank and then alphabetically if the ranks are same?
Any number of Comparator classes can be created to sort them differently as shown below.
import java.util.Comparator;
public class JavaTechnologyComparator implements Comparator<JavaTechnology> {
@Override
public int compare(JavaTechnology t1, JavaTechnology t2) {
//handle null values here
Integer rank1 = t1.getRank();
Integer rank2 = t2.getRank();
int rankVal = rank1.compareTo(rank2);
int nameVal = t1.getName().toLowerCase().compareTo(t2.getName().toLowerCase());
//if same rank, then sort by name
if(rankVal == 0){
return nameVal;
}
//else sort by rank
return rankVal ;
}
}
Now, the Sort3Test class has been slightly modified to use a Comparator.
import java.util.Collections;
import java.util.List;
public class Sort3Test {
public static void main(String[] args) {
JavaTechnology jt1 = new JavaTechnology(“JEE”, 1);
JavaTechnology jt2 = new JavaTechnology(“Java”, 1);
JavaTechnology jt3 = new JavaTechnology(“Servlets”, 2);
JavaTechnology jt4 = new JavaTechnology(“JSP”, 2);
JavaTechnology jt5 = new JavaTechnology(“JNDI”, 3);
JavaTechnology jt6 = new JavaTechnology(“EJB”, 4);
JavaTechnology jt7 = new JavaTechnology(“JMS”, 5);
List<JavaTechnology> values = Arrays.asList(jt1, jt2, jt3, jt4, jt5, jt6, jt7);
Collections.sort(values, new JavaTechnologyComparator());
// invokes the compare(…) in JavaTechnologyComparator
// a number of times
System.out.println(values);
}
}
The output will be:
[(Java , 1), (JEE , 1), (JSP , 2), (Servlets , 2), (JNDI , 3), (EJB , 4), (JMS , 5)]
This should now enable you to sort any Java objects.
Now, what if you have your own custom class like a Dog, Cat, etc instead of a library class like String, Integer, etc?
Here is an example of a JavaTechnology custom object that implements a default sorting logic based on the rank (i.e popularity).
public class JavaTechnology implements Comparable<JavaTechnology>{
private String name;
private int rank; // popularity lower value means more popular
public JavaTechnology(String name, int rank){
this.name = name;
this.rank = rank;
}
//default implementation by rank alone
@Override
public int compareTo(JavaTechnology technology) {
int rank1 = this.rank;
int rank2 = technology.rank;
if (rank1 > rank2){
return +1;
}else if (rank1 < rank2){
return -1;
}else{
return 0;
}
}
//required for printing, displaying, etc.
@Override
public String toString() {
return “(” + name + ” , ” + rank + “)”;
}
}
Now, a simple test class
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Sort3Test {
public static void main(String[] args) {
JavaTechnology jt1 = new JavaTechnology(“JEE”, 1);
JavaTechnology jt2 = new JavaTechnology(“Java”, 1);
JavaTechnology jt3 = new JavaTechnology(“Servlets”, 2);
JavaTechnology jt4 = new JavaTechnology(“JSP”, 2);
JavaTechnology jt5 = new JavaTechnology(“JNDI”, 3);
JavaTechnology jt6 = new JavaTechnology(“EJB”, 4);
JavaTechnology jt7 = new JavaTechnology(“JMS”, 5);
List<javatechnology> values = Arrays.asList(jt1, jt2, jt3, jt4, jt5, jt6, jt7);
Collections.sort(values); // invokes the compareTo(…) method in JavaTechnology a number of times
System.out.println(values);
}
}
Output:
[(JEE , 1), (Java , 1), (Servlets , 2), (JSP , 2), (JNDI , 3), (EJB , 4), (JMS , 5)]
What is “application component provider”?
A vendor that provides the Java classes that implement components’ methods, JSP page definitions, and any required deployment descriptors.
What are the components of J2EE application?
A J2EE component is a self-contained functional software unit that is assembled into a J2EE application with its related classes and files and communicates with other components. The J2EE specification defines the following J2EE components:
- Application clients and applets are client components.
- Java Servlet and JavaServer PagesTM (JSPTM) technology components are web components.
- Enterprise JavaBeansTM (EJBTM) components (enterprise beans) are business components.
- Resource adapter components provided by EIS and tool vendors.
How can I set a cookie in JSP?-
response. setHeader(”Set-Cookie”, “cookie string”); To give the response-object to a bean, write a method setResponse (HttpServletResponse response) – to the bean, and in jsp-file:<% bean. setResponse (response); %>