0
<%@ page language="java" import="DBConnect"%> <% try { System.out.println("\n--------------------"); System.out.println("\n loading .."); DBConnect.connectToDb(); %> <h3>Connection ok</h3> <% } catch(Exception e) { e.printStackTrace(); } %> 

gives me following error how to load java class in jsp

org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 12 in the generated java file The import DBConnect cannot be resolved An error occurred at line: 16 in the jsp file: /applicationservices/fileshare/vm/TestJdbc.jsp DBConnect cannot be resolved 13: System.out.println("\n--------------------"); 14: System.out.println("\n loading .."); 15: 16: DBConnect.connectToDb(); 17: 18: %> 19: 
0

5 Answers 5

1

You need to create a package for the application which would be something like

com.yourappname.data 

then create your DBConenct class in that package or refactor it in there so that when you need to import the class you do so by using

import com.yourappname.data.DBConnect 

The you can import and use the class in your jsp

<%@page import="com.yourappname.data.DBConnect"%> 

On a side note, you shouldn't be doing any database acces within the jsp you should instead be doing all of your data access within a servlet.

Sign up to request clarification or add additional context in comments.

Comments

0

Add the package prefix to DBConnect:

<%@ page language="java" import="full.package.path.DBConnect"%> 

Where full.package.path is the actual package of DBConnect.

5 Comments

@ tibtof: there is no package i directly put my DBConnect.class file in class dir
@ybc126 Don't do that, the default package (aka "directly in the class dir") is inherently broken (as shown here). Always use a package to avoid this other namespace problems. Things in the default package cannot be reference reliably from other locations.
@Philipp Reichart: now I put my class file db directory now error is Only a type can be imported. db.DBConnect resolves to a package An error occurred at line: 17 in the jsp DBConnect cannot be resolved
Can you please update the code in your question with the latest changes?
@tibtof: there are no changes.
0

class should be public and methods also should be declared as public

Comments

0

You page has no error. Please clean and build your project again and retest it.Putting your class in some package is good practice but without using this is no problem for test project.

Comments

0

Make sure you see

DBConnect.class 

in your

WEB-INF/classes 

directory. If it's not there, the class isn't on your classpath if you are running in tomcat or similar.

1 Comment

@ stevedbrown: my DBConnect.class file is in WEB-INF/classes directory

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.