• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but i am not using any package statement in Person class,
than why should i use com.Person

Please explain
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic