// my 3rd servlet, access to music dbe via jdbc import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class Music extends HttpServlet implements SingleThreadModel { public String getServletInfo() { return "My 3rd Servlet, Lars Appel, Feb 1999"; } static final String dbHref = "jdbc:allbase://my.hp3000.here/musicdbe.grp.acct"; static final String dbUser = "user.acct"; static final String dbPass = "wont,tellyou"; private Connection con; // for JDBC access static private boolean missingDriver = false; // for delayed Exception reporting private int requestCounter; // to show servlet memory on web pages static { try { Class.forName("com.hp.jdbc.allbase.JdbcDriver"); } catch (ClassNotFoundException e) { missingDriver = true; // checked at init() time } } public void init(ServletConfig config) throws ServletException { super.init( config ); if (missingDriver) throw new UnavailableException( this, "missing jdbc:allbase driver" ); else try { con = DriverManager.getConnection(dbHref,dbUser,dbPass); } catch (SQLException e) { throw new UnavailableException( this, e.toString() ); }; } public void doGet( HttpServletRequest req, HttpServletResponse res ) throws IOException { res.setContentType( "text/html" ); PrintWriter toClient = res.getWriter(); String pathInfo = req.getPathInfo(); if (pathInfo == null) showMainPage(toClient); else if (pathInfo.equals("/albums")) showAlbumList(toClient); else if (pathInfo.equals("/composers")) showComposerList(toClient); else showError(toClient); toClient.close(); } private void showMainPage( PrintWriter out ) { out.println( "Music DBE via JDBC\n" + "

My JDBC Servlet Demo

\n" + "\n" + "

(request number " + ++requestCounter + ")

\n" + "
Lars Appel, February 1999
" ); } private void showAlbumList( PrintWriter out ) { out.println( "Albums\n" + "

Albums in DBE

\n" + "\n" + "

(request number " + ++requestCounter + ")

\n" + "
Lars Appel, February 1999
" ); } private void showComposerList( PrintWriter out ) { out.println( "Composers\n" + "

Composers in DBE

\n" + "\n" + "

(request number " + ++requestCounter + ")

\n" + "
Lars Appel, February 1999
" ); } private void showError( PrintWriter out ) { out.println( "Error\n" + "

Something went wrong, hehehe ;-)

" ); } }