Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey friends,

I'm trying to do a online library system using jsp, servlet, java and mysql. In my system im tring to enter a book id through a text box and search the book then display the name of the box in next text box.my problem is how to display a db column value in a text box. values are passing to jsp->servlet->service->dao and vice versa. but did not display the name in the 2nd text box.
here my code of my system.

HTML
<div id="page">
            <form name="input" action="book" method="post">
                Book Id   :       <input type="text" name="txtBid" class="resizedTextbox"><br><br>
                                   
                <input type="submit" value="Search Book" onclick= <a href="main/Servelet"</a>      
                <input type="hidden" name="operation" value="searchBookbtn"><br><br>	
                Book Name : <input type="text" name="txtName" class="resizedTextbox"><br><br>
                                   
                <input type="submit" value="Delete Book" onclick= <a href="main/Servelet"</a>
                <input type="hidden" name="operation" value="deleteBookbtn">	
                <input type="submit" value="Cancel/Clear" onclick= <a href="main/Servelet"</a>
                <p><%=request.getAttribute("deleteBookMsg") %></p>
            </form>
                <br><br><br><br><br><br><br><br><br><br><br>
        </div>


servlet

Java
private void searchBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        System.out.println("Inside searchBook");
        book b =new book();
        bookService bs = new bookServiceImpl();
        
        String b_id=request.getParameter("txtBid");
        String b_name=request.getParameter("txtName");
        
        b.setB_id(b_id);
        b.setB_title(b_name);
        
        System.out.println(b_id);
        if(bs.searchbook(b)){
            String message = "Click Delete button to delete the book detail";
            request.setAttribute("deleteBookMsg", message);
            request.setAttribute("txtName",b); 
        }else{
           String message = "Book does not Exist";
 	   request.setAttribute("deleteBookMsg", message);
        }
        RequestDispatcher rd = getServletContext().getRequestDispatcher("/deleteBook.jsp");
        rd.forward(request, response);
    }


service layer

Java
public boolean searchbook(book b) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        System.out.println("Inside bs.searchbook");
        boolean book_result=true;
        bookDAO bd = new bookDAOImpl();
        System.out.println("===Inside bs.searchbook");
        book bk =bd.search(b);
        if(bk==null){
             System.out.println("....");
             System.out.println(book_result);
             book_result=false;     
        }return book_result; 
     }


dao

Java
public book search(book b) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        System.out.println("Inside book search");
        book b_result = null;
        Connection conn= null;
        PreparedStatement ptmt= null;
        ResultSet rset = null;
        try{ 
            conn= getConnection();             
            String queryString = "SELECT * FROM book WHERE b_id=?";
            ptmt = conn.prepareStatement(queryString);
            ptmt.setString(1, b.getB_id());
            System.out.println(b.getB_id());
            rset = ptmt.executeQuery();
            
            System.out.println(rset.first()); 
            if (rset.first()){
                System.out.println(rset.first());
                b_result = new book();
                b_result.setB_title(rset.getString(String.valueOf("b_title")));
                b_result.setB_suplier(rset.getString(String.valueOf("b_supplier")));
                b_result.setB_publisher(rset.getString(String.valueOf("b_publisher")));
                return b_result;
            }    
        } catch (SQLException ex) {
              ex.printStackTrace();
        }  
        finally {
                try {
                    ptmt.close();
                    conn.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
        }System.out.println("======");
        System.out.println(b_result);
        return b_result;	
    }
Posted

you need to reference the txtName request attribute in the jsp when your servlet returns to that jsp e.g.
XML
<input type="text" name="txtName" value="<%=request.getAttribute('txtName')%>" class="resizedTextbox">

Just make sure you handle the case where the value in that attribute is null when the jsp is first loaded i.e. before the servlet is invoked. A better solution than using these java scriptlets is to use a taglib library. Google "JSLT core tags" for examples and tutorials.
 
Share this answer
 
v3
Comments
mali_angel 29-Aug-13 23:44pm    
I put That code into jsp page but did not work. the value of book name did not pass to the jsp. it always null. Theres a problem of passing b_title value fron DAO to service/business layer.. Can u tell where i went wrong?
grahamrenney 30-Aug-13 2:25am    
Do you see any console output (your System.out.println(...) statements) when executing the servlet?
mali_angel 2-Sep-13 0:08am    
This is i get when i run this files with a true value.

Inside book dopost
Inside searchBook
b001
Inside bs.searchbook
Inside book search
Welcome
Connected
b001
true
true
Java Beginners
**true
I've created a simplied version of your project and zipped the eclipse project to this URL: http://www.filedropper.com/librarysearch[^]. Use this as a base and add your search etc. There are many problems in your code e.g. the onclick handler cannot have tags. I would suggest you use struts or spring mvc as using jsps and servlets is not the best solution
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900