Denys
Denys

Reputation: 4557

Using a function from one jsp in another jsp

I have a jsp file were the db connection is created and queries can be executed. I want to call these functions (of connecting to a database and executing query) from another jsp simply to add new record to a database and to stay on a same page.

I read here about an "import" tag but i don't have any packages. Also i'm not allowed to use JSTL.

Little help?

Thanks for considering my question.

Upvotes: 1

Views: 4258

Answers (3)

Sarin Jacob Sunny
Sarin Jacob Sunny

Reputation: 2138

Mainly we used to import two types of pages into a jsp page,

  • Java pages
  • JSP pages

If you want to import a java page into your jsp page, syntax is

<%@ page import="package_name.java_page" %>

If you want to import a jsp page, syntax is

<%@ include file='jsp_page_name.jsp'%>

while adding a jsp page, if it is in an another folder, you just need to specify the paths also.

Upvotes: 0

BalusC
BalusC

Reputation: 1109735

You can use <%@include %> directive to statically include JSP fragments.

<%@include file="db.jsp" %>

Writing JSPs this way is however considered poor practice. Java code belongs in a Java class, not in a JSP file. You could for example use a preprocessing servlet class wherein you do the job in doGet() method and have a reuseable DAO class wherein you hide all the JDBC boilerplate code away.

Upvotes: 4

Udo Held
Udo Held

Reputation: 12548

You shouldn't program in your jsp. Put that connection logic out into a class and use that in your jsps.

Afterwards you can import your class with:

<%@ page import="package.yourclass" %> 

In general you should consider using frameworks like Spring MVC or Struts. Using JSF could be another option.

Upvotes: 0

Related Questions