aspose file tools*
The moose likes JSP and the fly likes connection class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "connection class" Watch "connection class" New topic
Author

connection class

Preeti Roy
Greenhorn

Joined: Aug 08, 2008
Posts: 8
i want to use a connection class from my jsp page to avoid call connection from every jsp page.

I am using apache/tomcat. can i keep the class file in the same folder where my jsp resides.

please help me
Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
No.

In a webapplication your classes will go into a folder inside
WEB-INF/classes. (or in a .jar file inside WEB-INF/lib)

Your .jsp (the sources) will not be there, and the compiled versions certainly will not be there.

Herman
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
I am using apache/tomcat. can i keep the class file in the same folder where my jsp resides.


The usual Tomcat installation includes a class-loader-howto.html file which explains exactly where class files should be placed. Also see the API for details of web application directory structure.

If you are talking about a database connection Tomcat has a facility for maintaining a pool of connections.

Bill
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

"confused confused",
Please see your Private Messages for an important administrative matter.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Preeti Roy
Greenhorn

Joined: Aug 08, 2008
Posts: 8
My problem is not solved. My package is kept in "WEB-INF/classes/connection"
and jsp is kept in same level of folder stucture where WEB-INF is kept.
My error message is "Package connection not found in import"

I am using this syntax to import the package.
<%@page language="java" import="java.sql.*,connection.*" %>

I am using solaris OS with tomcat version 3.2.4.
Can anybody tell me where I am going wrong.

Thanks In Advance
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

"confused confused", you have previously been warned on one or more occasions regarding adjusting your display name to meet JavaRanch standards. This is not optional. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it prior to your next post.

Your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Be aware that accounts with invalid display names are disabled.

bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Preeti Roy
Greenhorn

Joined: Aug 08, 2008
Posts: 8
I am really very sorry. And I have changed my Profile name.
Nishanth Sivaraman
Greenhorn

Joined: Aug 12, 2008
Posts: 7
You can place your class file in folder \WEB-INF\classes\connection in your web application. since the package name is connection.
Nishant Kr
Greenhorn

Joined: Aug 07, 2008
Posts: 8
Hi Preeti,
I think it'll be better if you place all your class files in a package inside the src folder and the jsp files into another in the web folder, and then import all those classes from there into their respective jsp files. Now, you can create a db connection class file and place it in the same package to that of other class files.

Now, in DbConnection.class file,

you can place this code,
public Connection getConn() {
try
{
javax.naming.Context ctx = new javax.naming.InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup ("java:/CONPool");
connection = ds.getConnection();
}
catch(Exception ex) {
strErrorMsg = ex.getMessage();
ex.printStackTrace();
}
return connection;
}

CONPool is the pool name that is coming from your ds.xml file.

Import DbConnection in each of the class files for the JSP pages. Create the connection globally inside the class by calling the method getConn() in a costructor, and use it to call your db procedures. Remember to create a method closing the connection at the end of the class file.
You can close the connection by calling this method at the end of its respective jsp file.

Thank You,
Nishant
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

"Nishant Kr", you have previously been warned on multiple occasions regarding adjusting your display name to meet JavaRanch standards. This is not optional, and this is your final warning. Adjust your display name to comply with the required standards prior to your next post.

Failure to comply will result in the removal of your account.

bear
JavaRanch Sheriff
Preeti Roy
Greenhorn

Joined: Aug 08, 2008
Posts: 8
Thank you All for resonding my problem.
As per the convention I have kept my package in the WEB-INF/classes folder. But still not able to import the PACKAGE. My jsp is saying package not found ti import. Tell me something my i have complied my class jsdk1.4 and in the server my jsdk version is 1.5 . IS there any versiing problem.

I don't want to follow other way apart from package import to access connection class. Can any body post the code where I can include jsp connection file in other file.

Please help me as soon as possible. I am really in trouble.
For Information: OS- solaris 5.10, tomcat -3.2.4, jDk 1.5

thanks in advance
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Originally posted by Preeti Roy:As per the convention I have kept my package in the WEB-INF/classes folder.
Details please.
Preeti Roy
Greenhorn

Joined: Aug 08, 2008
Posts: 8
details are as follows

my test.jsp is hosted in /u03/websites/project1 (Apache/tomcat server) version already mentioned.

<%@page language="java" import="com.conn.*,java.sql.*" %>
<%
try{
connect conn= new connect();
Connection con = conn.getConnection();
java.sql.Statement stmt= con.createStatement();
String sql2="select name from table1 where empID ='123'";
ResultSet rs= stmt.executeQuery(sql2);
rs.next();
String ecode=rs.getString("name");
out.println(ecode);
%>
<%
}catch(Exception e){
out.println(e);
}
%>

Java class file is hosted in
/u03/websites/project1/WEB-INF/classes/com/conn/connect.class.

And I am getting thi error.
Package com.conn not found in import.
import com.conn.*;
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: connection class