Hello everyone,
I haven't wrote on this blog for a long, long time.
I actually was busy with finishing the computer science faculty.
Now I'm at master studies in computer science.
One of the first challanges was Java Web Technologies(using J2EE).
JSP, JavaBeans, Servlets.
A Web application can be wrote as a MVC.
Control-Servlet
JSP-View
JavaBeans-Model.
The comunication between JSP and Servlets is made using JavaBeans:
a) A JavaBeans is a public class that:
The required conventions are:
*The class must have a public default constructor. This allows easy instantiation within editing and activation frameworks.
*The class properties must be accessible using get, set, and other methods (so-called accessor methods and mutator methods), following a standard naming convention. This allows easy automated inspection and updating of bean state within frameworks, many of which include custom editors for various types of properties.
*The class should be serializable. This allows applications and frameworks to reliably save, store, and restore the bean's state in a fashion that is independent of the VM and platform.
//example:
/**
* Class
PersonBean.
*/
public class PersonBean implements java.io.Serializable {
private String name;
private boolean deceased;
/** No-arg constructor (takes no arguments). */
public PersonBean() {
}
/**
* Property
name (note capitalization) readable/writable.
*/
public String getName() {
return this.name;
}
/**
* Setter for property
name.
* @param name
*/
public void setName(final String name) {
this.name = name;
}
/**
* Getter for property "deceased"
* Different syntax for a boolean field (is vs. get)
*/
public boolean isDeceased() {
return this.deceased;
}
/**
* Setter for property
deceased.
* @param deceased
*/
public void setDeceased(final boolean deceased) {
this.deceased = deceased;
}
}
b)a JSP page using a JavaBean:
<% // Use of PersonBean in a JSP. %>
Name:
Deceased?
c)The Servlet-I will only list the part necessary to take the bean and use it:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session=request.getSession();
PersonBean pers=(PersonBean)session.getAttribute("person");
out.print(pres.getName());
//we also can set and send a bean
session.setAttribute("person2", pers);
getServletContext().getRequestDispatcher("Result.jsp").forward(request, response);
}
Paul.