tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mark Whitby" <>
Subject Servlet problem/Data Source Problem
Date Tue, 21 Feb 2006 13:38:10 GMT

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 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;
            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;
        } catch(NamingException e){
        } catch(SQLException sql){

        RequestDispatcher dispatcher = 
        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?


----- Original Message ----- 
From: "David Smith" <>
To: "Tomcat Users List" <>
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 
> --David

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message