0

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

Bundy
  • 717
  • 3
  • 12
  • 23

2 Answers2

2

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

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

Alan J Liu
  • 324
  • 1
  • 7
  • what should the package declaration be? – Bundy Oct 17 '12 at 11:27
  • On the top of your class (User.java) you will see something like package xxx.yyy.zzz.User; that should transform into: <%@page import="xxx.yyy.zzz.User" %>, if you don't see the package declaration, then it should be <%@page import="User" %> – Alan J Liu Oct 17 '12 at 11:53