You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Суржин Константин Вадимович <Su...@vniizht.ru> on 2015/10/27 12:45:36 UTC

jsp to servlet

Hi.
win XP. 
Apache Tomcat 8.0.15
jdk8
animal.jsp (page code below)
Animal.java(page code below)

line 10 -> animalBean_0 = new org.animal.Animal();
and all right.

line 16 ->  animalBean_1 = (Animal) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "Animal");
but I need a line 1 in animal.isp  and I get differing code ( not new org.animal.Animal();)
and ☹
HTTP Status 500 - /animal.jsp (line: 16, column: 8) The value for the useBean class attribute Animal is invalid.
and ☹
org.apache.jasper.JasperException: java.lang.ClassNotFoundException: org.apache.jsp.animal_jsp

Maybe something is missing or incorrect on this page?? http://wiki.apache.org/tomcat/FAQ/Class_Not_Found


================
animal.jsp
================
1 <%@page import="org.animal.Animal"%>
2 <%@page language="java" contentType="text/html" pageEncoding="UTF-8"%>
3 <!DOCTYPE html>
4 <html> 
5    <head>
6        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7       <title>JSP Page</title>
8    </head>
9    <body>
10        <jsp:useBean id="animalBean_0"  class="org.animal.Animal" scope="session">
11            <%-- intialize bean properties --%>
12            <jsp:setProperty name="animalBean_0" property="name" value="puppy" />
13            <jsp:setProperty name="animalBean_0" property="age" value="12" />
14        </jsp:useBean>
15         Sample Bean: <%= animalBean_0%>
16        <jsp:useBean id="animalBean_1" class="Animal" scope="session">
17           <%-- intialize bean properties --%>
18            <jsp:setProperty name="animalBean_1" property="name" value="floppy" />
19            <jsp:setProperty name="animalBean_1" property="age" value="2" />
20       </jsp:useBean>  
21        Sample Bean: <%= animalBean_1%>
22    </body>
23 </html>

==================
Animal.java
==================
package org.animal;
public class Animal {
    private String name = "no name";
    private Integer age = 0;
    public Animal(){
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Animal [name=" + name + ", age=" + age + "]";
    }
}

Re: jsp to servlet

Posted by Christopher Schultz <ch...@christopherschultz.net>.

On 10/28/15 4:27 AM, Суржин Константин Вадимович wrote:
>> You are asking for a bean with the class name "Animal", and the JSP compiler and/or runtime >can't find it. I'm guessing you meant to use "org.animal.Animal" here?
> 
> <jsp:useBean id="animalBean_1" class="org.animal.Animal" scope="session">
> 
> JSP  compiler produce next servlet code:
> org.animal.Animal animalBean_1 = new org.animal.Animal();
> 
> It's OK without  import directive.
> 
> Or 
> 
> <%@page import="org.animal.Animal"%>
> <jsp:useBean id="animalBean_1" type="Animal" beanName="org.animal.Animal" scope="session">
> 
> JSP  compiler produce next servlet code:
> Animal animalBean_1 = (Animal) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "org.animal.Animal");
> 
> And (ClassLoader) the JSP compiler and/or runtime  can find Animal.class
> 
> But:
> <%@page import="org.animal.Animal"%>
> <jsp:useBean id="animalBean_1" class="Animal" scope="session">
> 
> But in this case JSP  compiler produce of different servlet code
> Animal animalBean_1  = (Animal) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "Animal");
> 
> The method instantiate(...,...) does not have a fully qualified name of Animal and It's does not work.
> 
> And (ClassLoader) the JSP compiler and/or runtime TomCat or GlassFish can't find Animal class in /WEB-INF/classes/org/animal/
> 
> So, directive <%@page import="org.animal.Animal"%> has no effect here and 
> beanName="" or class="" org.animal.Animal" cannot be short.
> Only fully qualified class name acceptable.
> Maybe it's a bug.

No, it's not:

http://www.oracle.com/technetwork/java/syntaxref12-149806.pdf

The spec for <jsp:useBean> is that both "type" and "class" attributes
should be specified as "package.Class" and not "Class" with an import. I
think the package name is required, regardless of any page imports.

> But FAQ writes:
> ======================
> Make sure:
> 
> Your bean is packaged in a class.
> You have fully qualified your class name (e.g.: com.bar.package.MyClass ) OR
> You have imported your class into your jsp (e.g.:  <%@ page import="com.bar.package.MyClass"%> )

You don't need to import the class at all if you are always using
<jsp:useBean>.

> =======================
> 
>> Or do you have a class, Animal, in the default package?
> 
> package org.animal;
> public class Animal {...}

Don't use the default package for anything: it's just a recipe for
confusion.

-chris

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: jsp to servlet

Posted by Суржин Константин Вадимович <Su...@vniizht.ru>.
>You are asking for a bean with the class name "Animal", and the JSP compiler and/or runtime >can't find it. I'm guessing you meant to use "org.animal.Animal" here?

<jsp:useBean id="animalBean_1" class="org.animal.Animal" scope="session">

JSP  compiler produce next servlet code:
org.animal.Animal animalBean_1 = new org.animal.Animal();

It's OK without  import directive.

Or 

<%@page import="org.animal.Animal"%>
<jsp:useBean id="animalBean_1" type="Animal" beanName="org.animal.Animal" scope="session">

JSP  compiler produce next servlet code:
Animal animalBean_1 = (Animal) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "org.animal.Animal");

And (ClassLoader) the JSP compiler and/or runtime  can find Animal.class

But:
<%@page import="org.animal.Animal"%>
<jsp:useBean id="animalBean_1" class="Animal" scope="session">

But in this case JSP  compiler produce of different servlet code
Animal animalBean_1  = (Animal) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "Animal");

The method instantiate(...,...) does not have a fully qualified name of Animal and It's does not work.

And (ClassLoader) the JSP compiler and/or runtime TomCat or GlassFish can't find Animal class in /WEB-INF/classes/org/animal/

So, directive <%@page import="org.animal.Animal"%> has no effect here and 
beanName="" or class="" org.animal.Animal" cannot be short.
Only fully qualified class name acceptable.
Maybe it's a bug.

But FAQ writes:
======================
Make sure:

Your bean is packaged in a class.
You have fully qualified your class name (e.g.: com.bar.package.MyClass ) OR
You have imported your class into your jsp (e.g.:  <%@ page import="com.bar.package.MyClass"%> )
=======================

> Or do you have a class, Animal, in the default package?

package org.animal;
public class Animal {...}

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: jsp to servlet

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Суржин,

On 10/27/15 7:45 AM, Суржин Константин Вадимович wrote:
> Hi.
> win XP. 
> Apache Tomcat 8.0.15
> jdk8
> animal.jsp (page code below)
> Animal.java(page code below)
> 
> line 10 -> animalBean_0 = new org.animal.Animal();
> and all right.
> 
> line 16 ->  animalBean_1 = (Animal) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "Animal");
> but I need a line 1 in animal.isp  and I get differing code ( not new org.animal.Animal();)
> and ☹
> HTTP Status 500 - /animal.jsp (line: 16, column: 8) The value for the useBean class attribute Animal is invalid.
> and ☹
> org.apache.jasper.JasperException: java.lang.ClassNotFoundException: org.apache.jsp.animal_jsp
> 
> Maybe something is missing or incorrect on this page?? http://wiki.apache.org/tomcat/FAQ/Class_Not_Found

You are asking for a bean with the class name "Animal", and the JSP
compiler and/or runtime can't find it. I'm guessing you meant to use
"org.animal.Animal" here?

Or do you have a class, Animal, in the default package?

-chris


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org