Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.databases > #741
| Newsgroups | comp.lang.java.databases |
|---|---|
| Date | 2016-10-10 04:57 -0700 |
| References | <b3b00b28.0309121950.15f06f32@posting.google.com> |
| Message-ID | <73feaa74-c8e1-4356-8f4c-803e79bb5db9@googlegroups.com> (permalink) |
| Subject | Re: Tomcat MySQL JSPs and JavaBeans |
| From | ravi@virtuosonetsoft.com |
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
> <?xml version="1.0" encoding="ISO-8859-1"?>
>
> <!DOCTYPE web-app
> PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
> "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
>
> <web-app>
> <servlet>
> <servlet-name>
> Main
> </servlet-name>
> <servlet-class>
> jspbook.ch5.Main
> </servlet-class>
> </servlet>
> <servlet-mapping>
> <servlet-name>
> Main
> </servlet-name>
> <url-pattern>
> /ch5/Main
> </url-pattern>
> </servlet-mapping>
> <taglib>
> <taglib-uri>/simple</taglib-uri>
> <taglib-location>/WEB-INF/tlds/simple.tld</taglib-location>
> </taglib>
> <taglib>
> <taglib-uri>/tableUtils</taglib-uri>
> <taglib-location>/WEB-INF/tlds/utils.tld</taglib-location>
> </taglib>
> <taglib>
> <taglib-uri>/groceries</taglib-uri>
> <taglib-location>/WEB-INF/tlds/groceries.tld</taglib-location>
> </taglib>
> <resource-ref>
> <description>
> 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.
> </description>
> <res-ref-name>
> jdbc/QuotingDB
> </res-ref-name>
> <res-type>
> javax.sql.DataSource
> </res-type>
> <res-auth>
> SERVLET
> </res-auth>
> </resource-ref>
> </web-app>
>
> ------------
>
> Any help or direction will be greatly appreciated.
>
>
> Regards,
>
> Jon Dellaria
Back to comp.lang.java.databases | Previous | Next | Find similar
Re: Tomcat MySQL JSPs and JavaBeans ravi@virtuosonetsoft.com - 2016-10-10 04:57 -0700
csiph-web