File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes can't able to run <jsp:useBean> in jsp page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "can Watch "can New topic
Author

can't able to run <jsp:useBean> in jsp page

V K Gupta
Ranch Hand

Joined: Aug 07, 2008
Posts: 55
I am trying to create a simple application in which,

1) index.html -> index page takes a name and passes it to servlet

2) control.java (servlet) -> takes the parameter from index.html , and creates a javabean and set that value into the bean and than finally set the bean as a request attribute, and forward the request to result.jsp

3) person.java (java bean) -> person.java is an java bean having property "name" and getter setter method

4) result.jsp -> this jsp files tries to read the bean value using <jsp:useBean> and <jsp:getPropert> tags.

My application runs perfectly upto servlet and servlet also passes the control to jsp. But in result.jsp a error comes given below.

Note : control.class and Person.class file are stored inside web-inf/classes


An error occurred at line: 13 in the jsp file: /result.jsp
Person cannot be resolved to a type
10: Person is <% //Person p = (Person) request.getAttribute("person");
11: //out.println(p.getName()); %>
12:
13: <jsp:useBean id="person" class="Person" scope="request" />
14: <jsp:getProperty name="person" property="name"/>
15: </body>
16: </html>

-----------------------------------------------------------------------------------------------------------------------------------------

code of all the 5 files is given below

--------------------------------------------------------------------
index.html
------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Index</title>
</head>

<body>

<form id="form1" name="form1" method="post" action="control2.do">


<label>Name
<input type="text" name="name" />
</label>



<label>
<input type="submit" name="Submit" value="Submit" />
</label>


</form>
</body>
</html>

----------------------------------------------------------------------
web.xml
-----------------------------------------------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>



<servlet>
<servlet-name>jsp3</servlet-name>
<servlet-class>Control</servlet-class>
</servlet>


<servlet-mapping>
<servlet-name>jsp3</servlet-name>
<url-pattern>/control2.do</url-pattern>
</servlet-mapping>

</web-app>

---------------------------------------------------------------------------------------
person.java
-------------------------------------------------------------------------------------
public class Person {
String name;

public void setName(String name) {
name = this.name;
}

public String getName() {
return name;
}
}

------------------------------------------------------------
control.java (servlet)
-------------------------------------------------------------
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;


public class Control extends HttpServlet {
public void doPost (HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException {

Person p = new Person();
p.setName(request.getParameter("name"));
request.setAttribute("person",p);

RequestDispatcher view = request.getRequestDispatcher("result.jsp");
view.forward(request,response);
}
}

-------------------------------------------------------------------------------
result.jsp
------------------------------------------------------------------------------------
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Result JSP</title>
</head>

<body>
Person is <% //Person p = (Person) request.getAttribute("person");
//out.println(p.getName()); %>

<jsp:useBean id="person" class="Person" scope="request" />
<jsp:getProperty name="person" property="name"/>
</body>
</html>
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575



Here you need to give fully qualified Class Name for class attribute example, class="com.Person"

try and let me know.....
V K Gupta
Ranch Hand

Joined: Aug 07, 2008
Posts: 55
but i am not using any package statement in Person class,
than why should i use com.Person

Please explain
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

V K Gupta wrote:but i am not using any package statement in Person class


if you use a class with useBean, then you need to put the class in a package. useBean expect a class with package.

For Your Information: always a class with out package consider bad practice.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61612
    
  67

Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can't able to run <jsp:useBean> in jsp page