X-Received: by 10.157.40.151 with SMTP id s23mr8739241ota.124.1476100671232; Mon, 10 Oct 2016 04:57:51 -0700 (PDT) X-Received: by 10.157.51.3 with SMTP id f3mr2786354otc.4.1476100671191; Mon, 10 Oct 2016 04:57:51 -0700 (PDT) Path: csiph.com!weretis.net!feeder6.news.weretis.net!news.glorb.com!l13no2049321itl.0!news-out.google.com!w143ni5602itb.0!nntp.google.com!l13no2049315itl.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.databases Date: Mon, 10 Oct 2016 04:57:50 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=112.196.30.219; posting-account=Z3IOEgoAAABoXtrgPyPOPbt3Rg9VTLHN NNTP-Posting-Host: 112.196.30.219 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <73feaa74-c8e1-4356-8f4c-803e79bb5db9@googlegroups.com> Subject: Re: Tomcat MySQL JSPs and JavaBeans From: ravi@virtuosonetsoft.com Injection-Date: Mon, 10 Oct 2016 11:57:51 +0000 Content-Type: text/plain; charset=UTF-8 Xref: csiph.com comp.lang.java.databases:741 On Saturday, September 13, 2003 at 9:20:20 AM UTC+5:30, Jon Dellaria wrote: > I have been working with JSP's and MySql using Tomcat. Until now it > has been working fine. I think I am having a problem with defining my > JDBC resource within Tomcat. I am pretty sure that my code is correct > (but I am not certain), and the problem is with defining the resource > in Tomcat. The error I am getting is: > HTTP Status 500 - > > -------------------------------------------------------------------------------- > > type Exception report > > message > > description The server encountered an internal error () that prevented > it from fulfilling this request. > > exception > > java.lang.NullPointerException > at jspbook.ch5.Main.authenticate(Main.java:126) > at jspbook.ch5.Main.doPost(Main.java:49) > at javax.servlet.http.HttpServlet.service(HttpServlet.java:760) > at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) > .... > > Here is my Main.java: > > package jspbook.ch5; > > import javax.servlet.*; > import javax.servlet.http.*; > import java.io.*; > import java.sql.*; > import javax.naming.*; > import javax.sql.*; > > import jspbook.ch5.CustomerBean; > > public class Main extends HttpServlet { > > // Connection dbCon; > DataSource ds; > HttpSession session; > > /* Initialize servlet. Use JNDI to look up a DataSource */ > public void init() { > > try { > Context initCtx = new InitialContext(); > Context envCtx = (Context) initCtx.lookup("java:comp/env"); > ds = (DataSource) envCtx.lookup("jdbc/QuotingDB"); > // dbCon = ds.getConnection(); > System.out.println("executed lookup for jdbc. JON"); > } > catch (javax.naming.NamingException e) { > System.out.println("A problem occurred while retrieving a > DataSource object"); > System.out.println(e.toString()); > } > > } > > public void doPost (HttpServletRequest _req, HttpServletResponse > _res) > throws ServletException, IOException { > > /* Refresh session attributes */ > session = _req.getSession(); > session.removeAttribute("loginError"); > session.removeAttribute("submitError"); > > String action = _req.getParameter("action"); > > /* Authenticate user if request comes from login page */ > if (action.equals("login")) { > String uid = _req.getParameter("UID"); > String pwd = _req.getParameter("PWD"); > if (authenticate(uid, pwd)) { > session.setAttribute("validUser", "y"); > session.setAttribute("loginError", "n"); > session.setAttribute("uid", uid); > gotoPage("/WEB-INF/jsp/ch5/census.jsp", _req, _res); > } > /* If the user login fails, then return them to the login page > to retry */ > else { > loginError(_req, _res); > } > } > > /* Record the survey data if the request comes from the survey > form */ > else if (action.equals("submit")) { > /* Make sure the user has logged in before recording the data */ > String validUser = (String) session.getAttribute("validUser"); > if (validUser.equals("y")) { > if (recordSurvey(_req)) { > /* Reset validUser flag and forward to ThankYou page */ > session.removeAttribute("validUser"); > gotoPage("/WEB-INF/jsp/ch5/thankyou.jsp", _req, _res); > } > else { > session.setAttribute("submitError", "y"); > gotoPage("/ch5/login.jsp", _req, _res); > } > } > /* If the user did not login, then send them to the login page > */ > else { > loginError(_req, _res); > } > } > > } > > /* Send request to a different page */ > private void gotoPage(String _page, HttpServletRequest _req, > HttpServletResponse _res) > throws IOException, ServletException { > > RequestDispatcher dispatcher = _req.getRequestDispatcher(_page); > if (dispatcher != null) > dispatcher.forward(_req, _res); > > } > > /* Set error attributes in session and return to Login page */ > private void loginError(HttpServletRequest _req, HttpServletResponse > _res) > throws IOException, ServletException { > > session.setAttribute("validUser", "n"); > session.setAttribute("loginError", "y"); > gotoPage("/ch5/login.jsp", _req, _res); > > } > > /* Check if the user is valid */ > private boolean authenticate(String _uid, String _pwd) { > > Connection dbCon = null; > ResultSet rs = null; > try { > System.out.println("try"); > dbCon = ds.getConnection(); > System.out.println("dbCon = ds.getConnection()"); > Statement s = dbCon.createStatement(); > System.out.println("Statement s = dbCon.createStatement()"); > rs = s.executeQuery("select * from user where id = '" > + _uid + "' and pwd = '" + _pwd + "'"); > System.out.println("rs = s.executeQuery"); > return (rs.next()); > } > catch (java.sql.SQLException e) { > System.out.println("A problem occurred while accessing the > database."); > System.out.println(e.toString()); > } > finally { > try { > dbCon.close(); > } > catch (SQLException e) { > System.out.println("A problem occurred while closing the > database."); > System.out.println(e.toString()); > } > } > > return false; > > } > > /* Using the CustomerBean, record the data */ > public boolean recordSurvey(HttpServletRequest _req) { > > Connection dbCon = null; > try { > dbCon = ds.getConnection(); > CustomerBean cBean = new CustomerBean(); > cBean.populateFromParms(_req); > return cBean.submit(dbCon); > } > catch (java.sql.SQLException e) { > System.out.println("A problem occurred while accessing the > database."); > System.out.println(e.toString()); > } > finally { > try { > dbCon.close(); > } > catch (SQLException e) { > System.out.println("A problem occurred while closing the > database."); > System.out.println(e.toString()); > } > } > > return false; > } > > public void destroy() {} > > } > > > -------- > > Here is my web.xml > > > PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" > "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> > > > > > Main > > > jspbook.ch5.Main > > > > > Main > > > /ch5/Main > > > > /simple > /WEB-INF/tlds/simple.tld > > > /tableUtils > /WEB-INF/tlds/utils.tld > > > /groceries > /WEB-INF/tlds/groceries.tld > > > > Resource reference to a factory for java.sql.Connection > instances that may be used for talking to a particular > database that is configured in the server.xml file. > > > jdbc/QuotingDB > > > javax.sql.DataSource > > > SERVLET > > > > > ------------ > > Any help or direction will be greatly appreciated. > > > Regards, > > Jon Dellaria