David,

Thanks very much for the tips. I haven't yet got it in a package as I am just using this for testing at the moment though I had managed to find out about number 2 and 3 for myself. Thanks for the hint though.

Now I have another problem with the following code:

import java.io.*;
import java.net.*;
import javax.sql.*;
import javax.naming.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

/**
*
* @author Mark
* @version
*/
public class SearchServlet extends HttpServlet {

/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
public void processRequest(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

       String address = "/privacy.jsp";

       DataSource ds = null;
       InitialContext ctx = null;
       Connection con = null;
       Statement stmt = null;
       int blob = -1;
       try{
           ctx = new InitialContext();
           ds = (DataSource) ctx.lookup("java:comp/env/jdbc/TestDB");
           con = ds.getConnection();
           if (con != null){
               address = "/termsandconditions.jsp";
           }
           else {
               address = "/support.jsp";
           }
           stmt = con.createStatement();
           ResultSet rst =
                   stmt.executeQuery("select id, chest from testdata");
           blob = rst.getInt("chest");
           address = "/testy.jsp?errorcode=" + blob;
           stmt.close();
           con.close();
       } catch(NamingException e){
           System.out.println(e.toString());
       } catch(SQLException sql){
           System.out.println(sql.toString());
       }

RequestDispatcher dispatcher = request.getRequestDispatcher(address);
       dispatcher.forward(request, response);
   }

Now what I've done is tested it using the if statement above and it detects the changes there and the servlet redirects me to termsandconditions.jsp. I've done this for the connection, statement and result set and none return as being null.

However, when I remove the if statement the servlet never gets to the "address = "/testy.jsp?errorcode=" + blob;" line and just redirects me to the privacy.jsp page. If the if statement is still in, it redirects me to the address within the != null part of that.

Any ideas why it isn't working and if the database connection is even working at all? Is there a better way to test it?

Mark

----- Original Message ----- From: "David Smith" <[EMAIL PROTECTED]>
To: "Tomcat Users List" <users@tomcat.apache.org>
Sent: Tuesday, February 21, 2006 12:42 PM
Subject: Re: Fw: Servlet problem


I get the impression you are a beginner at this. Reading the servlet spec would go a long way in understanding how the servlet container works. For the immediate problem:

1) Try to get SearchServlet in a package. ie: com.mycompany.myproject.SearchServlet This isn't strictly required for servlet classes, but excellent practice none the less. You will need packaged classes for any supporting classes you develop.

2) Map your servlet to a unique url of it's own and not to the url of existing JSPs. When you map your servlet to a url matching a jsp, the servlet is executed instead of the jsp per the mapping mechanism.

3) The action attribute of your form tag should be to the url you mapped your servlet to, not it's literal location at WEB-INF/classes/SearchServlet. Resources inside of WEB-INF are not directly accessible from the user and you'll get an error if you attempt it.

After the servlet does it's thing, it can forward the request to one of your jsps as necessary. The servlet spec can be found at http://www.jcp.org/en/jsr/detail?id=154

--David


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to