Reputation: 1259
The following is my code in a jsp file :
int s_id = session.getAttribute("s_id") != null ? ((Integer) session.getAttribute("s_id")).intValue() : 0 ;
String Pending = "Pending";
%>
<!-- <jsp:useBean id="link" scope="application" class = "Shop" /> -->
<%
Shop s = new Shop();
int g_orderQuantity=0;
ERROR :
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 30 in the jsp file: /ShopViewOrder.jsp
Shop cannot be resolved to a type
line 30 : <!-- <jsp:useBean id="link" scope="application" class = "Shop" /> -->
An error occurred at line: 32 in the jsp file: /ShopViewOrder.jsp
Shop cannot be resolved to a type
line 32 : Shop s = new Shop();
Some answers as on page : "Import cannot be resolved" with JSP
suggest using page import directive as follows :
<%@ page import="pkgname.Class1"%>
where they sat that you must give a fully qualified name of the class.
I have placed my Shop Class as follows : C:\Users\Documents\ShopSystem-AWSJavaWebProject\src\myPackage\Shop.java
And now I am trying to use the following syntax :
<%@ page import="myPackage.Shop"%>
But this is not working either... It gives an error saying that import myPackage cannot be resolved!
Then as per the solution mentioned in : Eclipse is not importing classes in jsp files
I have ensured that the Preferences->import->Web->Jsp Files->Editor->Content assist->Add_import_instead_of_qualified_name is enabled
now when i use the above line : <%@ page import="myPackage.Shop"%>
I get the following error now :
exception : org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 16 in the generated java file
Only a type can be imported. myPackage.Shop resolves to a package
An error occurred at line: 31 in the jsp file: /ShopViewOrder.jsp
Shop cannot be resolved to a type
line 31: <!-- <jsp:useBean id="link" scope="application" class = "Shop" /> -->
An error occurred at line: 33 in the jsp file: /ShopViewOrder.jsp
Shop cannot be resolved to a type
line 33 : Shop s = new Shop();
Another page : problem in importing java class in jsp
suggested <%@ page language="java" import="yourpackage.subpackage.*,java.util.*" %>
Hence i updated my code as :
<%@ page language="java" import="myPackage.Shop" %>
but the error still remains the same!
I have even tried the following :
<%@ page language="java" import="myPackage.Shop" session="true"%>
as suggested on To import class to JSP in Tomcat6 yet no change in error definition!
On another site it said create a public constructor of the class you want to load. I don't know how that helps nut I've even tried that that too doesn't work!
Please help...
As asked m adding snippets of my entire code :
int s_id = session.getAttribute("s_id") != null ? ((Integer) session.getAttribute("s_id")).intValue() : 0 ;
String Pending = "Pending";
<jsp:useBean id="link" scope="application" class = "myPackage.Shop" />
myPackage.Shop s = new myPackage.Shop();
int g_orderQuantity=0;
Statement stmt6 = con.createStatement();
// a shop can select values from Order table
ResultSet rs3 = stmt6.executeQuery("SELECT * FROM ShopSystem.Order where s_id="+s_id+" AND status='"+Pending+"'");
//code to declare some Vectors
while(rs3.next())
{
status1.add((i),rs3.getString("status"));
status=status1.elementAt(i);
// in the above way am adding ResultSet values to the vector and then copying it to string.
//i maintains the total count of all my orders
i++;
}
for(int j=0; j<i; j++)
{
//checking if the order table is empty
if((g_quantityRequired == 0)||(Cust_address.equals(null))||(email.equals(null))||((contact.equals(null)))||(total_amount == 0))
{ %> <br> There are no pending orders. <br>
<%
%> <form name="Back" method="post" action="ShopMenu3.jsp">
<input type="submit" value="Back">
</form><br><br> <%
}
// Now that the order table is not empty continue to process the order..
int g_available=0;
//check availability of g_id in s_id
boolean b_availability = s.checkAvailability_perShop(s_id, g_id, g_quantityRequired);
//if sufficient quantity is not available then place an order
if(b_availability == false)
{ // example, if required=50, then order is placed for 100 items
// but if required is 150, then order is placed for 150 items
g_orderQuantity = (100>g_quantityRequired)?100 : (g_quantityRequired+100);
//placing order and updating the Grocery_perShop table return the amount available of g_id in s_id
g_available = s.placeOrder_perShop(s_id, g_id, g_orderQuantity);
} %>
New g_avaliable = <%= g_available %>. The value has been updated in the database.
<% //now that sufficient g_id is available with s_id, we execute the order
//updating the Grocery_perShop table
g_available = g_available - g_quantityRequired;
//code to insert updated values in table
sc.close();
%> <form name="Back" method="post" action="ShopMenu3.jsp">
<input type="submit" value="Back">
</form><br><br>
Upvotes: 0
Views: 2203
Reputation: 46881
You have to use fully qualified name for class in <jsp:useBean>
. import
page directive is not considered by <jsp:useBean>
<jsp:useBean id="link" scope="application" class = "mypackage.Shop" />
Directly from Official Oracle Documentation - Creating and Using a JavaBeans Component
The value supplied for the class attribute must be a
fully qualified class name
.Note that beans cannot be in the unnamed package. Thus the format of the value must be
package-name.class-name
.
Upvotes: 0
Reputation: 8217
If the class you are trying to access is under the mypackage
of the application , where you have the jsp file the following statement should work,
<jsp:useBean id="link" scope="application" class = "mypackage.Shop" />
check whether you have file once you are completing the build. check under build->classes as you are using eclipse.
Upvotes: 1