Bundy
Bundy

Reputation: 725

import a created class in jsp

ive got a servlet with a class file contained within file path webapps/ass2/WEB-INF/classes/User.class, the class represents a user bean required for my application. In jsp, ive go the imports organised as `<%@page import="mypck.User" %>. im being thrown an error by apache tomcat when i try to load the page,

An error occurred at line: 12 in the jsp file: /fourm.jsp
User cannot be resolved to a type
9: 
10: <head>
11: <%
12: User user = (User)session.getAttribute("userBean");
13: 
14: String username = user.getName();
15: 

i guess i havent imported the class correctly then? my question is: how do i import this class to the jsp file

Upvotes: 0

Views: 158

Answers (2)

BalusC
BalusC

Reputation: 1109715

a class file contained within file path webapps/ass2/WEB-INF/classes/User.class

...

<%@page import="mypck.User" %>

This doesn't match. The User.class has got to be placed in the mypck folder representing the package. Fix it accordingly: webapps/ass2/WEB-INF/classes/mypck/User.class.


Unrelated to the concrete problem, using scriptlets is discouraged since a decade. I recommend to take a JSP pause and invest some time in learning taglibs and EL. With EL, you can just show the username like follows:

<p>Welcome, ${userBean.name}</p>

See also:

Upvotes: 2

Alan J Liu
Alan J Liu

Reputation: 324

Your import is correct syntax-wise, but is the package declaration correct?

Upvotes: 0

Related Questions