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>