Results 1 to 4 of 4
  1. #1
    vagf is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default Code problem in servlet?

    Hi everyone
    To get straight to the point i have created a web application using netbeans 6.9.1 with glassfish 3 and a derby database connected. What i want to do is give a parameter through an html to the servlet and then search for it in the database and print the row which contains the parameter. If the parameter is not found then the response should be an error page. i have completed the code but the error page never shows up (i get a blank page) whereas the search part works correctly. Does anybody see something wrong with my servlet code?( I have tried different methods of writing the if statement but the else part never works).

    Java Code:
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    
    public class ErgasiaServlet extends HttpServlet {
       
        
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
           
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            String id = request.getParameter("param");  //the parameter to be searched in the database
            Connection conn;
    
            try {
               
                Class.forName("org.apache.derby.jdbc.ClientDriver");
                
                String serverName = "localhost";
                String mydatabase = "sample";
                String url = "jdbc:derby://" + serverName + ":1527" +  "/" + mydatabase;
                System.out.println("url "+ url);
                String username = "app";
                String password = "app";
    
                conn = DriverManager.getConnection(url, username, password);
             
                Statement stmt = conn.createStatement();
    
                ResultSet rs = stmt.executeQuery("SELECT * FROM PERSON WHERE USERID='"+id+"'");
                while (rs.next()) {
                    if (id.trim().equals(rs.getString(1))){
                        out.println("<html>");
                        out.println("<head>");
                        out.println("<title>Servlet ErgasiaServlet</title>");
                        out.println("</head>");
                        out.println("<body>");
                        out.println("User ID: "+ rs.getString(1) +"<br> Name: " + rs.getString(2) + "<br> Surname:  "
                        + rs.getString(3));
                        out.println("</body>");
                        out.println("</html>");
                    }else{
                        response.sendError(response.SC_NOT_FOUND,"error page");
                    }
                }
            rs.close();
            }catch (ClassNotFoundException e) {
                // Could not find the database driver
                System.out.println(e.toString());
            }catch (Exception e2) {
                System.out.println(e2.toString());
            }
        } 
        
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            processRequest(request, response);
        } 
    
        
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            processRequest(request, response);
        }
    
       
        public String getServletInfo() {
            return "Short description";
        }
    }
    Last edited by vagf; 04-26-2011 at 07:07 PM. Reason: code tags

  2. #2
    doWhile is online now Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    code tags == readable code, please use them in the future.

    If an empty set is returned, the while loop will never be entered (rs.next() returns false), thus the error response code never run.

  3. #3
    vagf is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Ok i understood what u said about the set. I tried placing the if statement outside of the while (didn't work) and then i replaced the while with do{ ...}while so that i could use the if statement before the change of the cursor but it still didn't work. Is there a way to check the Set without using next? i mean like an empty String or with boolean? I am guessing that when it can't find the id I automatically get sql exeption. So should i try doing the check before the statement?

  4. #4
    vagf is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Thanks a lot for your help! i figured it out. I both put the if statement outside the loop and changed the loop to do...while. Here is the correct code.

    Java Code:
    public class ErgasiaServlet extends HttpServlet {
       
        
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            String id = request.getParameter("param");  //the parameter to be searched in the databse
            Connection conn;
            try {
               
                Class.forName("org.apache.derby.jdbc.ClientDriver");
                
                String serverName = "localhost";
                String mydatabase = "sample";
                String url = "jdbc:derby://" + serverName + ":1527" +  "/" + mydatabase;
                System.out.println("url "+ url);
                String username = "app";
                String password = "app";
    
                conn = DriverManager.getConnection(url, username, password);
             
                Statement stmt = conn.createStatement();
    
                ResultSet rs = stmt.executeQuery("SELECT * FROM PERSON WHERE USERID='"+id+"'");
    
                if (rs.next()){
                    do {
                        out.println("<html>");
                        out.println("<head>");
                        out.println("<title>Servlet ErgasiaServlet</title>");
                        out.println("</head>");
                        out.println("<body>");
                        out.println("User ID: "+ rs.getString(1) +"<br> Name: " +rs.getString(2) + "<br> Surname:  "
                         + rs.getString(3));
                        out.println("</body>");
                        out.println("</html>");
                    }while(rs.next());
                } else {
                        response.sendError(response.SC_NOT_FOUND,"error page");
                }
    
                rs.close();
           
            
                }catch (ClassNotFoundException e) {
                    // Could not find the database driver
                    System.out.println(e.toString());
                }catch (Exception e2) {
                    System.out.println(e2.toString());
                }
        }

Similar Threads

  1. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  2. servlet problem plz help
    By superbg in forum Advanced Java
    Replies: 1
    Last Post: 10-18-2010, 10:43 AM
  3. servlet problem
    By vijayabaskar in forum Java Servlet
    Replies: 0
    Last Post: 04-29-2009, 09:34 AM
  4. Servlet code change
    By joeyxaza in forum Java Servlet
    Replies: 0
    Last Post: 02-20-2009, 05:15 PM
  5. Servlet problem
    By maheshkanda in forum New To Java
    Replies: 5
    Last Post: 02-10-2009, 08:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •