File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EL and jsp:usebean..type/class

 
Kuppusamy Venkatasubramanian
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Alankar Yannam
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic