How to reference a DatabaseHandler servlet class from a jsp file in Tomcat

Trying to create a database connection in the jsp file in webapps\project folder by referencing the DatabaseHandler class in webapps\project\WEB-INF\classes.
Here is the code in the jsp to make the connection:
DatabaseHandler db = new DatabaseHandler() ;
String sql = "SELECT password FROM cms_users WHERE user =" + userName ;
java.sql.ResultSet rs = db . selectQuery( sql ) ;
Here is the DatabaseHandler class:
import java.sql.*;
public class DatabaseHandler
     // instance variables - replace the example below with your own
private Connection connection;
     * Constructor for objects of class DatabaseHandler
     public DatabaseHandler()
String url = "jdbc:odbc:javadatabase";
// Load the driver to allow connection to the database
try
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
connection = DriverManager.getConnection( url );
catch ( ClassNotFoundException cnfex )
System.err.println( "Failed to load JDBC/ODBC driver." );
cnfex.printStackTrace();
System.exit( 1 ); // terminate program
catch ( SQLException sqlex )
System.err.println( "Unable to connect" );
sqlex.printStackTrace();
public ResultSet selectQuery( String query )
Statement statement;
ResultSet resultSet = null ;
try
statement = connection.createStatement();
resultSet = statement.executeQuery( query );
catch ( SQLException sqlex )
sqlex.printStackTrace();
System . exit( 1 ) ;
return resultSet ;
Here is the error i am getting when i try to run the jsp script:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 2 in the jsp file: /ValidateLogon.jsp
Generated servlet error:
[javac] Compiling 1 source file
C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\cms\ValidateLogon_jsp.java:47: cannot find symbol
symbol : class DatabaseHandler
location: class org.apache.jsp.ValidateLogon_jsp
DatabaseHandler db = new DatabaseHandler() ;
^

Just like in the class file you need to import any classes that you want to access in the JSP.
http://java.sun.com/products/jsp/tags/11/syntaxref11.fm7.html

Similar Messages

  • How can i pass the same parameter from 1 jsp page to many jsp page?

    hello.....
    how can i actually pass a parameter from 1 jsp file to many other jsp files? pls help......
    thanx

    Hi
    Save the information which u want to pass to various JSP's inside Session Object.
    There is an Object called HttpSession and u can store information inside which other jsp pages within the same domain and same browser instance can use it.
    commands which are for ur interests are
    <Session Object>.setAttribute("<object name>", <value>);
    Example
    HttpSession objsession =request.getSession(true); String strName="NEED_HELP":
    objSession.setAttribute("Name", strName);
    And then u can retreive that information in your n pages.
    Read any JSP/Servlet Books and u will know about them
    Bye.

  • How do you call a java class from the main method in another class?

    Hi all,
    How do you call a java class from the main() method in another class? Assuming the two class are in the same package.
    Thanks
    SI
    Edited by: okun on May 16, 2010 8:40 PM
    Edited by: okun on May 16, 2010 8:41 PM
    Edited by: okun on May 16, 2010 8:47 PM

    georgemc wrote:
    To answer your impending question, either the method you're calling has to be static, or you need an instance of that other class to invoke it against. Prefer the latterAnd to your impending question after that: no, don't use the Singleton pattern.

  • Urgent: how to import or transport abap class from dev to q

    How to import/transport custom abap class from dev to q.
    Any suggestions points will be awarded.
    Thanks in advance.
    MK

    hi M K,
    try SE24, your class, and go to menu 'Go to' -> object directory entry,
    click pencil icon and give your package name, will raise request.
    SE10 to release and STMS from prod to import.
    hope this helps.

  • How to reference the WindowListener in "class implements WindowListener"

    i need to remove a WindowListener but i dont know how to
    reference Listeners implented by classes.
    Thanks for any help in advance!
    public class MyClass implements WindowListener{
    public MyClass(){
    frame.addWindowListener(this);
    public static void RemoveIt(){
    frame.removeWindowListener( ??? )
    JFrame frame = new JFrame();
    }

    Looks like im just going to have to implement this
    all much differently -
    and maybe in a less lazy manner, haha.Hi! Sorry for my late reply.
    Let me help you a little;
    ===============================================================
    1.) You can create a class that will be shared by each of your frame.
    ===============================================================
    The shared class will contain the following:
    1.) The number of your alive frame.
    2.) The information of your alive frame. ie. name, if it is the main, etc.
    Then your frame should look at that shared class so that it will know when to
    invoke System.exit(int);
    ===============================================================
    2.) You can use java.util.prefs.Preferences
    ===============================================================
    Use preference to put and get information about your frame.
    ===============================================================
    3.) You can extends my class below.
    ===============================================================
    NOTE: This example may or may not help the OP. I'm sorry.
    * @(#)AbstractExitableJFrame.java     05/06/25
    * 2005 Tunay na pag-aari ni Ronillo Ang.
    package com.yahoo.ronilloang.cswingx;
    import java.awt.GraphicsConfiguration;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowFocusListener;
    import java.awt.event.WindowListener;
    import java.awt.event.WindowStateListener;
    import javax.swing.JFrame;
    * Use this at your own risks.
    * @version     1.4.2
    * @author     Ronillo Ang
    public abstract class AbstractExitableJFrame extends JFrame implements WindowFocusListener, WindowListener, WindowStateListener{
         private static int frameCount = 0; // how many are we?
          * @see          javax.swing.JFrame constructor.
         public AbstractExitableJFrame(){
              super();
              ++frameCount;
          * @see          javax.swing.JFrame constructor.
         public AbstractExitableJFrame(GraphicsConfiguration gc){
              super(gc);
              ++frameCount;
          * @see          javax.swing.JFrame constructor.
         public AbstractExitableJFrame(String title){
              super(title);
              ++frameCount;
          * @see          javax.swing.JFrame constructor.
         public AbstractExitableJFrame(String title, GraphicsConfiguration gc){
              super(title, gc);
              ++frameCount;
          * Called by the constructors to init the <code>AbstractExitableJFrame</code> properly.
         protected final void frameInit(){
              super.frameInit();
              addWindowFocusListener(this);
              addWindowListener(this);
              addWindowStateListener(this);
         public final void windowGainedFocus(WindowEvent we){
              exitableJFrameGainedFocus(we);
         public final void windowLostFocus(WindowEvent we){
              exitableJFrameLostFocus(we);
         public final void windowActivated(WindowEvent we){
              exitableJFrameActivated(we);
         public final void windowClosed(WindowEvent we){
              exitableJFrameClosed(we);
              --frameCount;
              if(frameCount <= 0 || isMain())
                   System.exit(0);
         public final void windowClosing(WindowEvent we){
              exitableJFrameClosing(we);
         public final void windowDeactivated(WindowEvent we){
              exitableJFrameDeactivated(we);
         public final void windowDeiconified(WindowEvent we){
              exitableJFrameDeiconified(we);
         public final void windowIconified(WindowEvent we){
              exitableJFrameIconified(we);
         public final void windowOpened(WindowEvent we){
              exitableJFrameOpened(we);
         public final void windowStateChanged(WindowEvent we){
              exitableJFrameStateChanged(we);
          * Invoked when {@link #windowGainedFocus(WindowEvent)} is invoked.
         public abstract void exitableJFrameGainedFocus(WindowEvent we);
          * Invoked when {@link #windowLostFocus(WindowEvent)} is invoked.
         public abstract void exitableJFrameLostFocus(WindowEvent we);
          * Invoked when {@link #windowActivated(WindowEvent)} is invoked.
         public abstract void exitableJFrameActivated(WindowEvent we);
          * Invoked when {@link #windowClosed(WindowEvent)} is invoked.
         public abstract void exitableJFrameClosed(WindowEvent we);
          * Invoked when {@link #windowClosing(WindowEvent)} is invoked.
         public abstract void exitableJFrameClosing(WindowEvent we);
          * Invoked when {@link #windowDeactivated(WindowEvent)} is invoked.
         public abstract void exitableJFrameDeactivated(WindowEvent we);
          * Invoked when {@link #windowDeiconified(WindowEvent)} is invoked.
         public abstract void exitableJFrameDeiconified(WindowEvent we);
          * Invoked when {@link #windowIconified(WindowEvent)} is invoked.
         public abstract void exitableJFrameIconified(WindowEvent we);
          * Invoked when {@link #windowOpened(WindowEvent)} is invoked.
         public abstract void exitableJFrameOpened(WindowEvent we);
          * Invoked when {@link #windowStateChanged(WindowEvent)} is invoked.
         public abstract void exitableJFrameStateChanged(WindowEvent we);
          * Tests if this frame is the main frame.
         public abstract boolean isMain();
    Take care and God bless you all. Thank you. -Ronillo

  • Referencing utility classes from a war file

    How do you reference a utility class from a war file? I tried adding the class to the deployment plan descriptor, however I still get a ClassNotFoundException.
    Thanks,
    Will

    To use the utility classes from a war you have to place them under the web-inf/classes directory.
    webApplication(WAR directory)--->WEB-INF--> classes (put your utility classes in this directory)

  • How can i get the all values from the Property file to Hashtable?

    how can i get the all values from the Property file to Hashtable?
    ok,consider my property file name is pro.PROPERTIES
    and it contain
    8326=sun developer
    4306=sun java developer
    3943=java developer
    how can i get the all keys & values from the pro.PROPERTIES to hashtable
    plz help guys..............

    The Properties class is already a subclass of Hashtable. So if you have a Properties object, you already have a Hashtable. So all you need to do is the first part of that:Properties props = new Properties();
    InputStream is = new FileInputStream("tivoli.properties");
    props.load(is);

  • How to read a tab seperated data from a text file using utl_file

    Hi,
    How to read a tab seperated data from a text file using utl_file...
    I know if we use UTL_FILE.get_line we can read the whole line...but i need to read the tab separated value separately.....
    Thanks in advance...
    Naveen

    Naveen Nishad wrote:
    How to read a tab seperated data from a text file using utl_file...
    I know if we use UTL_FILE.get_line we can read the whole line...but i need to read the tab separated value separately.....If it's a text file then UTL_FILE will only allow you to read it a line at a time. It is then up to you to split that string up (search for split string on this forum for methods) into it's individual components.
    If the text file contains a standard structure on each line, i.e. it is a fixed delimited structure, then you could use external tables to read the data instead.

  • Accessing my own class members from the JSP file

    Hi,
    I have created a class by name simple.java.and the package structure is like sridhar.source.useful.In this package i placed my file simple.java.
    Now i import this file with the import statement in JSP like
    <%@ import = "sridhar.source.useful.*" %>
    Now from the JSP file i want to execute some of the methods in simple.java.
    I tried this one by importing the simple.java in my JSP file,but i am getting some errors:
    variables that i have declared in Java files are not defined.
    The JSP is not importing the class simple.java
    I am executing my JSP in weblogic6.1(webserver is IPlanet)
    shall i need to set any classpath for this in the weblogic shell script??
    pls clarify me if any configuration is to be made or not??
    regards
    Naidu

    For JSP with IPlanet/weblogic and imported custom classes:
    Put your class / package in the correct folder. $path is any path you want....
    Ex: package foo, class foorbar must be
    $path/foo/foobar.class
    All classes must be .class, not .jar.
    Weblogic/IPlanet must be configured to have the CLASSPATH
    $path/
    (do not include foo/foobar!)
    The JSP has
    <%@ import "foo.*" %>
    Your class may now be used just like the other imported classes. Say you have an object Foobar with a constructor, you can just say
    Foobar myFoo = new Foobar();
    I do this with my classes, so if you have any questions, just ask.

  • How to get result from another JSP file?

    I have to write a jsp (my.jsp) to get information from another jsp file (other.jsp).
    The other.jsp return integer value (0 / 1) so that user can tell if certain service is available or not. And in my.jsp I need to collect such result and other information from a text file to make up of a XML string.
    How can I call other.jsp to get the result? Thanks a lot.

    Hi, I think I didn't describe the problem clearly
    enough. In fact, there is a JSP file, and if our
    database is currently connected, the JSP will return
    value 1, otherwise, it will return 0. My java program
    need to get that result, and then form an XML string,
    and send the string back to the client. I'm just
    wonder how can I write such a program to read result
    from JSP file. Thanks a lot.Why is this function implemented as a JSP file? It should be implemented as a bean. It would be simple to get the information you require from that bean.

  • How to store ,retreive and edit  data from an xml file

    I am new to XML. I am creating a phone book in JSP with backend as an XML file.How can I store retrieve and edit datas from a jsp file.
    Please provide me with examples.I dont want to use bean.
    Please provide examples

    You will have to do some leg-work yourself. You will probably want to use DOM to parse and manipulate your XML data. Xerces (xml.apache.org) is a good open source parser. There are extensive examples on their site.
    - Saish

  • Is it possible to load classes from a jar file

    Using ClassLoader is it possible to load the classes from a jar file?

    URL[] u = new URL[1] ;
    u[0] = new URL( "file://" + jarLocation + jarFileName + "/" );
    URLClassLoader jLoader = new URLClassLoader( u );
    Object clsName = jLoader.loadClass( clsList.elementAt(i).toString() ).newInstance();
    I get this error message.
    java.lang.ClassNotFoundException: ExceptionTestCase
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:297)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:253)
    // "file://" + fileLocation + fileName + "/" This works fine from a browser.
    Is there anything I am missing? Thanks for the reply.

  • How do i remove the exif metadata from my jpg file produced with a digital camera photo?

    How do i remove the exif metadata from my jpg file produced with a digital camera photo?

    The excellent and free utility Irfanview has the option to remove the EXIF data.
    Simply open and the file and do a Save, or Save As with the original name.
    Surprisingly, the default is to not keep the original EXIF data.

  • How can I find the servlet class name from inside a ServletFilter?

    Ive implemented a servlet filter, but need to discover the class name of any servlet class that gets executed.
    Ive dug through the spec and cant seem to find any path to do this.
    Seems the methods needed to do this have been deprecated. (for security reasons?)
    Is there any way to write a ServletFilter to grab this info?
    If not, is there any other way to capture every servlet execution in the container, time its execution, and log the class name along with the execution time?
    (***WITHOUT*** requiring a classpath over ride of any container provider classes)
    Any help is much appreciated. Been banging my head against this for some time now :(

    request.getServletPath() returns the part of the URL which refers to the servlet. It isn't the classname of the servlet but it should be a reasonable surrogate. If you log that, then you could write some code which reads your web.xml and uses the servlet-mapping elements to convert it to servlet class names later.

  • How can i make a servlet (class) temporarily unavailable, except for ADMIN

    Hello All!
    I am rather new to the programming field and have already completed a web-project in Java only with Servlets. (no jsp). I have a login procedure, means I have a user management for users and admins. If an admin is logged in, he/she should be able to deactivate the servlet with a mouse-click as long he activates it again with the same. if the servlet is deactivated, other users/admins trying to access the servlet should get a "servlet is currently unavailable" message. but the admin, who is logged in, still should be able to work with the servlet! how can i realise that?
    in other words, by clicking that particular button, the servlet should be made "non-multi-thread" and other way round by activating it.
    any ideas??? how can the standard "servlet currently unavailable" page be displayed? I have tomcat 4 on a linux machine! i'd be really grateful if someone oculd help me.
    another question i have is, how to define a "pseudo" link address to the servlet. now, the servlet can only be accessed by typing like this: http://servername.xy.com:8080/ProjectName/servlet/ServletName (because I have activated the servlet mapping in the server.xml/web.xml with /servlet/*)
    but I want a link like this: http://servername.xy.com:8080/shortname
    How can I do this in an easy way?? I have treid to create a web.xml in der WEB-INF folder of the servlet with following content, but it is not working:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE web-app PUBLIC
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
    <web-app>
    <servlet>
    <servlet-name>
    shortname
    </servlet-name>
    <servlet-class>
    ClassName
    </servlet-class>
    </servlet>
    </web-app>
    Thanks a lot in advance for your kind help,
    lisa

    Ok,
    You'll need to find a tutorial on servlet filters. Its not that hard a concept.
    http://java.sun.com/j2ee/1.4/docs/tutorial/doc/Servlets8.html#wp64572
    Filters need to be configured in the web.xml.
    Basically they are a buffer between a request and your servlet.
    Any requests for the servlet, go through the filter first. It lets you do some processing before/after the servlet gets called. Its a good way of putting in some generic code that need to be run for many servlets - security checks are often implemented in this fashion.
    This should give you an idea of the sort of thing you need. I haven't really written one before, so I copied this out of the tutorial and did some basic framework for it....
    public final class TestFilter implements Filter {
       private FilterConfig filterConfig = null;
       public void init(FilterConfig filterConfig) throws ServletException {
          this.filterConfig = filterConfig;
       public void destroy() {
          this.filterConfig = null;
       public void doFilter(ServletRequest request,  ServletResponse response, FilterChain chain) throws IOException, ServletException {
            // if you want to get the session will need to cast request to an HttpServletRequest
            Session session = request.getSession();
            String requestURL = request.getRequestURL();
            // check if this URL is for a servlet that is disabled for this user
           // somehow you have to keep track of this ... maybe in the servletContext ?
        User user = (User) session.getAttribute("user")     
          boolean disabled = checkDisabled(requestURL, user);
          // if its not disabled, go ahead
          if (!disabled)
            chain.doFilter(request, response);
        else{
            // send the servlet off somewhere else - requestDispatcher maybe?
    }

Maybe you are looking for

  • How do i get my Hotmail Contacts transferred to my iCloud Contacts? on my iPhone 5?

    How do i get my Hotmail Contacts transferred to my iCloud Contacts? on my iPhone 5?

  • Error while opening Mobile Sales

    Hi  all I have developed one application in MAS. Before Generating i tick make dll both UI and Businsslogic then generate.  After that i open Mobile sales application "Starting Mobilesalesfailed". Context - UI_CORE_APPL_LAUNCH_FAILED_STA. Starting Mo

  • Can i see the name and the nickname at the same time

    Can I see the name and the nickname at the same time when Iphone 4 is ringing? Sometimes i don't remember somebody's name, so i want to see the name and nickname when phone is ringing.

  • Setting PYTHONPATH in 10.7.2

    Hey, I just decided to start working on learning a programming language and have decided to start with Python.  I have the most current version but am running into issues when trying to import modules that I have downloaded from the internet.  I beli

  • CWGraph Flashing/Flickering when updating

    We are utilizing CWGraph (3.0) in VB6-SP4 as a single trace chart. Quite often the chart flickers/flashes when updating, but there is no particular pattern to it. If a chart is currently flickering & a command button on the same form is selected to c