1

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.

Udo Held
  • 12,314
  • 11
  • 67
  • 93
Denys
  • 4,287
  • 8
  • 50
  • 80

3 Answers3

4

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • how should i than call this servlet from the jsp? – Denys Dec 04 '11 at 12:25
  • @Denis: You don't call this servlet from the JSP. You instead call the servlet which in turn displays results in JSP. See also the "servlet" link in my answer which contains detailed Hello World examples. – BalusC Dec 04 '11 at 12:36
  • @BalusC: I only took the chance of giving that feedback because your English is so amazingly good, you clearly care about getting it right. :-) – T.J. Crowder Dec 04 '11 at 12:38
  • in which method of servlet would you recommend creating my db connection? – Denys Dec 04 '11 at 13:00
  • @Denis: Actually, that's a whole subject at its own. At least, the proper JDBC idiom is that you create, use and close the connection in the shortest possible scope. I've answered this several times before. See also http://stackoverflow.com/a/8345640/157882 and the "See also" links in this answer and so on. – BalusC Dec 04 '11 at 13:08
0

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.

Sarin Jacob Sunny
  • 2,138
  • 3
  • 29
  • 61
0

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.

Udo Held
  • 12,314
  • 11
  • 67
  • 93
  • Alright, you mean .java class? How should i than call it from my jsp pages? – Denys Dec 04 '11 at 12:23
  • After importing the class with the above import statement you can just use YourClass objName = new YourClass(); afterwards call the methods you require. – Udo Held Dec 04 '11 at 12:28
  • @DenisStetsenko: Just like you use any other class in a JSP. You `import` it and use it. In general, a JSP application should mostly consist of compiled Java classes, with the JSP code just being the minimum necessary to populate the view. Refer to your container documentation to find out where to put the `.class` and/or `.jar` files so it finds them. – T.J. Crowder Dec 04 '11 at 12:29