aspose file tools*
The moose likes JSP and the fly likes please validate my jsp page 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 "please validate my jsp page" Watch "please validate my jsp page" New topic
Author

please validate my jsp page

Preeti Roy
Greenhorn

Joined: Aug 08, 2008
Posts: 8
I am using this jsp page to connect database. Please tell me whether the declaration is ok nr not.


<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" %>
<%!
Connection con;
Statement stmt1;
Statement stmt2;

ResultSet rs;
ResultSet rs;

String driver;
String db;
String name="";
%>
<%
response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
%>
<%
String employee_id;
String password;


employee_id=request.getParameter("txtEmpId");
password=request.getParameter("txtPass");

driver="oracle.jdbc.driver.OracleDriver";
db="jdbcracle:thin:@111.111.111.111:1521:CONN";

try
{
Class.forName(driver);

con_=DriverManager.getConnection(db,"userID","Password");
stmt=con.createStatement();
String sql1 = "select name from employee_mster"

rs=stmt.executeQuery(sql1);
while(rs.next())
{
name=rs.getString("name");
}

rs.close();
stmt.close();
}

}catch(Exception e)
{
out.print(e);
}

finally
{
con.close();
}

%>
[ August 22, 2008: Message edited by: Bear Bibeault ]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

It is not OK. You should never have DB code inside a JSP. You should avoid at all costs having any Java code in your JSP. Your JSP should be about display only.

See This JavaRanch Article for more info.


Steve
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

Moreover, using JSP scriptlets declarations in this manner creates thread contention that will prevent your code from ever working correctly. All those instance variables are shared by everyone who is viewing the JSP.

Let's all move away from 1998 and start writing JSPs like grownups.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

Also, please be sure to use UBB code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the .
Preeti Roy
Greenhorn

Joined: Aug 08, 2008
Posts: 8
I agree with your comments. And I am trying to incorporate JSP in right manner. Since I am working on old application which is being hosted in a Apache/tomcat server with OS solaris. There are lots of application hosted there. I cannot change the whole set up rigt now for each application.

I wanted to write a connection class inside a Package and i want to access the same from every JSP.
I have created a connection package called "MyConn" and kept under 'WEB-INF/classes'

I have set the classpath for this : here is my code

set CLASSPATH= 'My classfile Path'
export CLASSPATH

And one copy of class file I kept under'/user/apache/tomcat/lib'

But still it is giving me this error
"sun.tools.javac.Main has been deprecated. Package MyConn not found in import"
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

Please read through the JSP FAQ. It has entries that deal with this situation.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: please validate my jsp page