aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes EL and jsp:usebean..type/class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "EL and jsp:usebean..type/class" Watch "EL and jsp:usebean..type/class" New topic
Author

EL and jsp:usebean..type/class

Kuppusamy Venkatasubramanian
Ranch Hand

Joined: Dec 30, 2004
Posts: 91
I have two doubts...
1) In the code snippet shown in page 384 of HF, to get the request method,
it has been shown like pageContext.request.method which when called using a JSP shows like GET or POST. If i want to get a servlet initialization parameter from config, i used like pageContext.config.ininParam but the code threw error saying.. there is no property as config in tomcat 5.5.9. Where can i see the property like request in pageContext.request.method. I couldn't find that under javax.servlet.http.jsp. How can i get the init parameter for a jsp using config? Is that container implemented?


2) In the bullet points in page 414 of HF, the last but one shows that when jsp:useBean is used
along with jsp:setProperty only properties on type can only be set not the
property on class. Listed below are the classes

//Person.java
package com.utils;

public abstract class Person{
private String name;
public abstract void setName(String name);
public abstract String getName();
}

//Employee.java
package com.utils;

public class Employee extends Person{
public Employee(){}
private String name;
private int empID;
private int age;
public void setName(String name)
{this.name=name;}
public void setEmpID(int id)
{this.empID=id;}
public String getName()
{return name;}
public int getEmpID()
{return empID;}
public void setAge(int age)
{this.age=age;}
public int getAge()
{return age;}

//The jsp with form
<form name="reqparamserv" method="Post" action="JSPStandardAct2.jsp">
Name: <input type="text" name="name" />
Employee ID:<input type="text" name="empID" />
<input type="submit" name="go" value="GO" />
</form>

//The jsp that sets the property
<html>
<body>
<jsp:useBean id="person" type="com.utils.Person" class="com.utils.Employee" scope="request" >
<jsp:setProperty name="person" property="name" param="name" />
<jsp:setProperty name="person" property="empID" param="empID" />
</jsp:useBean>
Person's name from servlet is:
<jsp:getProperty name="person" property="name" />
<br>
Person's ID from servlet is:
<jsp:getProperty name="person" property="empID" />
</body>
</html>

This page shows whatever i enter in the form like name and empID. I should be able to set the name only.
Pls. clarify. Thanks.
SAM
[ November 06, 2005: Message edited by: Kuppusamy Venkatasubramanian ]

SCJP1.4, SCBCD 1.3, SCWCD 1.4, SCEA 5, JLPT-N3
My Blog
Alankar Yannam
Greenhorn

Joined: Oct 31, 2005
Posts: 6
Hi Venkatasubramanian,

Here are some clarifications to your doubts.

1. There is no map or bean object available for "servlet init" params. You need to pass init param name ("string" argument) to get init param value, but there is no mechanism available to pass argument on EL!

2. I got following servlet code on tomcat 5.5.7 version inside _jspService() method.

com.utils.Person person = null;
synchronized (request) {
person = (com.utils.Person) _jspx_page_context.getAttribute("person", PageContext.REQUEST_SCOPE);
if (person == null){
person = new com.utils.Employee();
_jspx_page_context.setAttribute("person", person, PageContext.REQUEST_SCOPE);
out.write("\n");
out.write(" ");
org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(_jspx_page_context.findAttribute("person"), "name", request.getParameter("name"), request, "name", false);
out.write("\n");
out.write(" ");
org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(_jspx_page_context.findAttribute("person"), "empID", request.getParameter("empID"), request, "empID", false);
out.write("\n");
out.write(" ");
}
}
out.write("\n");
out.write(" Person's name from servlet is: \n");
out.write(" ");
out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((com.alankar.data.Employee)_jspx_page_context.findAttribute("person")).getName())));
out.write("\n");
out.write(" <br>\n");
out.write(" Person's ID from servlet is: \n");
out.write(" ");
out.write(org.apache.jasper.runtime.JspRuntimeLibrary.toString((((com.utils.Employee)_jspx_page_context.findAttribute("person")).getEmpID())));
out.write("\n");
out.write(" \n");
out.write(" </body>\n");
out.write("</html>\n");


From above code, we can understand that there is no existing object of Person available in the request scope, so container assigned a new object of Employee class. While printing properties, casting back to original object (Employee). I think container using introspection mechanism to identify the object type.

Alankar Yannam
Umesh Adtani
Greenhorn

Joined: Nov 09, 2005
Posts: 5
JSP's config parameters should be available from the implicit object "config" that is available in JSPs.

So you could call config.getInitParam(string) method to get to the initParam you are looking for.
Umesh Adtani
Greenhorn

Joined: Nov 09, 2005
Posts: 5
I just realized you may be looking to access these init parameters using EL.

Well, there is no implicit map available, so you should have to pass those in as attributes or paramters. Basically JSP would need to create a Map of init-param-values from config object and then call it a 'bean'. EL then can use configBean.paramName to get to initParam's value. It is tedious though. I wonder why there is no straightforward way from pageContext to get to initParams.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: EL and jsp:usebean..type/class