Results 1 to 8 of 8
  1. #1
    cgk_js is offline Member
    Join Date
    Oct 2012
    Posts
    21
    Rep Power
    0

    Default WebApplication development using beans not useful

    Hi,

    I tried with the web application development using the java beans, jsp , jdbc and it seems it does not seems to be useful.

    reason: i need to create the java page and need to load the property and use it for the page...

    Please find my codes attached for my simple application...

    simple application name: log entry
    brief: it updates the daily comments, and tracks the status completed or not, you can update and insert.. it..


    Can u pls advice me the new, robust technology i can follow futher...
    Last edited by cgk_js; 11-20-2012 at 05:05 AM. Reason: i could not able to upload the file for the attachement.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,814
    Rep Power
    19

    Default Re: WebApplication development using beans not useful

    Post the code.
    People don't tend to open attachments.

    If the code is too long to post then it's probably too long to deal with here and you need to narrow down what your problem is.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    cgk_js is offline Member
    Join Date
    Oct 2012
    Posts
    21
    Rep Power
    0

    Default Re: WebApplication development using beans not useful

    here is my code..
    1. my code calls the loginEntry.jsp, then if in need to search the data using the date, then i need to click search it calls index.jsp, otherwise if i need to insert record in need to click on input, it will call the logInput.jsp, if save it calls the logEntrySave.jsp

    LogEntry.jsp: entry page
    index.jsp: to display the records for the inputed date
    LogInput.jsp: to input the records
    LogEntrySave.jsp: to update or insert records
    JdbcConnection.java: java beans

    loginEntry.jsp
    -------------

    Java Code:
    <html>
        <head>
            <title>Log entry Search</title>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        </head>
        <script type="text/javascript">
            function clear_form()
            {
               document.form.date.value=""
            }
                
            function input_form()
            {
               window.location="LogInput.jsp";
            }
    
            function search_form()
            {
                var dateval = document.form.date.value;
                if (dateval != "")
                {
                    window.location="index.jsp?date="+ dateval;
                }         
            }
    
        </script>
        <body>
        <!-- <form name="form" action="index.jsp" method ="POST"> -->
        
        <form name="form">
            <br>
            <br>
             Date:<input type="text" name="date"/>(mm/dd/yyyy)
            <br>
            <br>
            <!-- <input type="submit" name="search" value="Search"/> -->
            <input type="button" name="search" value="Search" onClick="search_form()" />
            <input type="button" name="clear" value="Clear" onClick="clear_form()" />
            <input type="button" name="input" value="Input" onClick="input_form()" />
        </form>
        </body>
        
    </html>

    index.jsp (display records.)
    ----------

    Java Code:
    <%-- 
        Document   : index
        Created on : Nov 15, 2012, 3:49:55 PM
        Author     : gopc
    --%>
    
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Log Entry Display</title>
        <link rel="stylesheet" type="text/css" href="http://localhost:8080//WebApplication1/CSS/version1.css">
        </head>
        <body onLoad="onloadfun()">
    
        <script type="text/javascript">
        
        function onloadfun()
        {
            var id_val = document.index.id.value;
            if (id_val <= 0 )
            {
                RedirectPage();
            }    
        }
    
        function RedirectPage()
        {
            //alert("hi");
            window.location="LogEntry.jsp"
        }
    
        function RedirectSavePage(id)
        {
            //alert(2);
            var id_val = document.index.id.value;
            var date_val = document.index.date.value;
            //var desc_val = document.index.desc.value;
            var comm_val = document.index.comm.value;
            var status_val = document.index.status.checked;
            if (document.index.status.checked == true)
            {
                status_val = 1;
            }
            else
            {
                status_val = 0;
            }
            //alert(status_val);
            window.location="LogEntrySave.jsp?id=" + id_val + "&date="+ date_val + "&comment=" + comm_val + "&completed=" + status_val;
        }
    
        </script>
    
             <%@ page import="beans.JdbcConnection" %>
             <jsp:useBean id="db" scope="session" class="beans.JdbcConnection"/> 
             <jsp:setProperty name="db" property="*"/> 
    
           <%
                JdbcConnection con = new JdbcConnection();
                String dateval = request.getParameter("date");
                System.out.println("dateval" + dateval);
                boolean return_val = con.getValues(dateval);
                System.out.println("ret_val:" + return_val);
                System.out.println("id in index:" + con.getdate());
                if (con.getdate() == null || con.getdate().isEmpty() || con.getdate().compareToIgnoreCase("") == 0)
                {
                    System.out.println("inside null");
                    System.out.println("inside redirect page");
                }
           %>
           <form name="index">
            <table>
                <tr>
                    <td>Id:</td> 
                    <td>
                        <input type="textbox" name="id" disabled="disabled" value="<%=con.getid()%>"></input>  
                    </td>
                </tr>
                <tr>
                    <td>Date:</td> 
                    <td>
                        <input type="textbox" name="date" value="<%=con.getdate()%>">(mm/dd/yyyy)</input>
                    </td>
                </tr>
    <!--
                <tr>
                    <td>Description:</td> 
                    <td>
                            <input type="text" name="desc" type="text" value="<%=con.getdescription()%>"></input>
                    </td>
    -->
                </tr>
                <tr>
                    <td>comment:</td> 
                    <td>
                            <textarea name="comm" rows="10" cols="50"><%=con.getcomment()%></textarea>
                    </td>
                </tr>
                <tr>
                    <td>completed:</td> 
                    <td>
                            <input type="checkbox" name="status" <%=con.getcompleted()%>></input>
                    </td>
                </tr>
            </table>
    
            <br>
            <br>
            <input type="button" name="back" value="Back" onClick="RedirectPage()" />
            <input type="button" name="save" value="Save" onClick="RedirectSavePage(<%=dateval%>)" />
        </form>
        </body>
    </html>
    logInput.jsp (input records)
    ----------------------------

    Java Code:
    <%-- 
        Document   : LogInput
        Created on : Nov 15, 2012, 3:49:55 PM
        Author     : gopc
    --%>
    
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Update log entries</title>
        <link rel="stylesheet" type="text/css" href="http://localhost:8080//WebApplication1/CSS/version1.css">
        </head>
        <body>
    
        <script type="text/javascript">
        function RedirectPage()
        {
            //alert("hi");
            window.location="LogEntry.jsp"
        }
    
    
        function doValidate()
        {
            //check the validation of the data..
            if (document.index.id.value < 0 )
            {
               return false;
            }
            else if ( (document.index.date.value == "" || document.index.date.value == "null" ) 
                        || ( document.index.comm.value == "" || document.index.comm.value == "null")  )
            {
                return false;
            }
            return true;
        }
    
        function RedirectSavePage()
        {
            
            var id_val = document.index.id.value;
            var date_val;
            var comm_val;
            if (document.index.date.value == "null" )
            {
                date_val  = "";
            }
            else
            {
                date_val = document.index.date.value ;
            }
    
            if (document.index.comm.value == "null")
            {
                comm_val = "";
            }
            else
            {
                comm_val = document.index.comm.value;
            }
    
    
            var status_val = document.index.status.checked;
            if (document.index.status.checked == true)
            {
                status_val = 1;
            }
            else
            {
                status_val = 0;
            }
    
            var validate = doValidate();
            if (!validate)
            {
                alert ("date or comments is invalid")
            }
            else
            {
                 window.location="LogEntrySave.jsp?id=" + id_val + "&date="+ date_val + "&comment=" + comm_val + "&completed=" + status_val;
            }
            
        }
    
        </script>
    
             <%@ page import="beans.JdbcConnection" %>
             <jsp:useBean id="db" scope="session" class="beans.JdbcConnection"/> 
             <jsp:setProperty name="db" property="*"/> 
    
           <%
                JdbcConnection con = new JdbcConnection();
                con.getNextId();
                System.out.println("id:" + con.getid());
           %>
           <form name="index">
            <table>
                <tr>
                    <td>id:</td> 
                    <td>
                        <input name=id   disabled="disabled" type="textbox" value="<%=con.getid()%>"></input>  
                    </td>
                </tr>
                <tr>
                    <td>date:</td> 
                    <td>
                        <input name=date type="textbox" value="<%=con.getdate()%>">(mm/dd/yyyy)</input>
                    </td>
                </tr>
                <tr>
                    <td>comment:</td> 
                    <td>
                            <textarea name=comm rows="10" cols="50"><%=con.getcomment()%></textarea>
                    </td>
                </tr>
                <tr>
                    <td>completed:</td> 
                    <td>
                            <input type="checkbox" name=status <%=con.getcompleted()%>"></input>
                    </td>
                </tr>
            </table>
    
            <br>
            <br>
            <input type="button" name="save" value="Save" onClick="RedirectSavePage()" />
            <input type="button" name="back" value="Back" onClick="RedirectPage()" />
        </form>
        </body>
    </html>
    logEntrySave.jsp
    ------------------


    Java Code:
    <%-- 
        Document   : LogEntrySave
        Created on : Nov 15, 2012, 3:49:55 PM
        Author     : gopc
    --%>
    
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Update log entries</title>
        <link rel="stylesheet" type="text/css" href="http://localhost:8080//WebApplication1/CSS/version1.css">
        </head>
        <body>
    
        <script type="text/javascript">
        function RedirectPage()
        {
            //alert("hi");
            window.location="LogEntry.jsp"
        }
    
        </script>
    
             <%@ page import="beans.JdbcConnection" %>
             <jsp:useBean id="db" scope="session" class="beans.JdbcConnection"/> 
             <jsp:setProperty name="db" property="*"/> 
    
           <%
                JdbcConnection con = new JdbcConnection();
                int id_val = Integer.parseInt(request.getParameter("id"));
                System.out.println("id_val:" + id_val);
                String date_val = request.getParameter("date");
                System.out.println("date_val:" + date_val);
                String desc_val = request.getParameter("description");
                String comment_val = request.getParameter("comment");
                int status_val = Integer.parseInt(request.getParameter("completed"));
                con.putValues(id_val,date_val,desc_val,comment_val,status_val);
                System.out.println("id:" + con.getcompleted());
           %>
           <form name="index">
            <table>
                <tr>
                    <td>id:</td> 
                    <td>
                        <input type="textbox" disabled=disabled value="<%=con.getid()%>"></input>  
                    </td>
                </tr>
                <tr>
                    <td>date:</td> 
                    <td>
                        <input type="textbox" value="<%=con.getdate()%>">(mm/dd/yyyy)</input>
                    </td>
                </tr>
    <!--
                <tr>
                    <td>description:</td> 
                    <td>
                            <input type="text" type="text" value="<%=con.getdescription()%>"></input>
                    </td>
                </tr>
    -->
                <tr>
                    <td>comment:</td> 
                    <td>
                            <textarea rows="10" cols="50"><%=con.getcomment()%></textarea>
                    </td>
                </tr>
                <tr>
                    <td>completed:</td> 
                    <td>
                            <input type="checkbox" <%=con.getcompleted()%>></input>
                    </td>
                </tr>
            </table>
    
            <br>
            <br>
            <input type="button" name="back" value="Back" onClick="RedirectPage()" />
        </form>
        </body>
    </html>
    JdbcConnection.java
    ----------------------

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package beans;
    import java.sql.*;
    import java.text.SimpleDateFormat;
    
    /**
     *
     * @author gopc
     */
    public class JdbcConnection {
    
        /**
         * @param args the command line arguments
         */
       // public static void main(String[] args) {
            private int id;
            private String date;
            private String description;
            private String comment;
            private String completed;
        
                    
        public void setid(int _id)
        {
            id = _id;
        }
        
        public int getid()
        {
            return id;
        }
        
        public void setdate(String _date)
        {
                    date = _date;
        }
        
        public String getdate()
        {
           if(date ==  null )
           {
              date = "";
           }
            return date;
        }
        
        public void setdescription(String _description)
                
        {
            description = _description;
        }
        
        public String getdescription()
        {
            return description;
        }
        
        public void setcomment(String _comment)
                
        {
            comment = _comment;
        }
        
        public String getcomment()
        {
           if(comment ==  null )
           {
              comment = "";
           }
            return comment;
        }
        
        public void setcompleted(String _completed)
                
        {
            completed = _completed;
        }
        
        public String getcompleted()
        {
            return completed;
        }
        
        public boolean putValues(int val, String dateval, String description, String comment, int completed) 
        {
            try 
            {
                System.out.println("putValues 1");
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                //Connection con = DriverManager.getConnection("jdbc:odbc:msaccessconn","","");
                Connection con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C://Users//gopc//Documents//odbc_sql.accdb", "", "");
                Statement st = con.createStatement();
                System.out.println("putValues 2 date:" + date);
                
                String QueryStr = "select * from log_entry where id ="+val;
                boolean update = executeQuery(QueryStr);
                
                //if the id is not there, then it is insert..
                if (!update)
                {
                    
                    QueryStr = "insert into log_entry (id, datecol,comment, completed) values("
                            + val + ", format('" + dateval+ "','mm/dd/yyyy')" + ",'" + comment + "'," + completed + ")";
    
                    System.out.println("QueryStr:" + QueryStr);
                    boolean insert_status = st.execute(QueryStr);
    
                    System.out.println("inserted status :" + insert_status);
                    System.out.println("putValues 3");
                    
                }
                else
                {
                    //check the id is there. if present then it is update...
                    QueryStr = "update log_entry set DateCol ='" + date + "',description ='" + description + "',comment ='" + comment + "',completed = " + completed
                            + " where  id=" + val;
                    System.out.println("QueryStr:" + QueryStr);
                    int rows = st.executeUpdate(QueryStr);
    
                    System.out.println("no of rows updated:" + rows);
                    System.out.println("putValues 4");
                }
                
    
                
                st.close() ;
                con.close() ;
                //call the getvalues to load the values
                try 
                {
                    boolean return_val = getValues(val);
                } 
                catch (Exception e) 
                {
                    e.printStackTrace();
                }
    
    
                return true;
    
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        public boolean resultSets(ResultSet rs)
        {
            try {
                    ResultSetMetaData rsMetaData = rs.getMetaData();
                    while (rs.next())
                    {
                        System.out.println("resultSets 1");
                        int count = 1;
                        while (count <= rsMetaData.getColumnCount())
                        {
                            if (count == 1)
                            {
                                setid(Integer.parseInt(rs.getString(count)));
                            }
                            else if (count == 2)
                            {
                                SimpleDateFormat dt = new SimpleDateFormat("MM/dd/yyyy");
                                setdate( dt.format(rs.getDate(count)));
                            }
                            else if (count == 3)
                            {
                                setdescription(rs.getString(count));
                            }
                            else if (count == 4)
                            {
                                setcomment(rs.getString(count));
                            }
                            else if (count == 5)
                            {
                                if ( rs.getString(count).compareToIgnoreCase("1") == 0) 
                                {
                                    setcompleted("checked");
                                }
                            }
                            count++;
                        }
                        return true;
                    }
                    //no records
                    return false;
            }
            catch(Exception e)
            {
                e.printStackTrace();
                return false;
            }
        }
        
        public boolean executeQuery(String qry)
        {
                    try{
                        
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    //Connection con = DriverManager.getConnection("jdbc:odbc:msaccessconn","","");
                    Connection con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C://Users//gopc//Documents//odbc_sql.accdb","","");
                    Statement st  =  con.createStatement();
                    System.out.println("executeQuery 2 date:" + qry);
                    ResultSet rs =  st.executeQuery(qry);
            
                    System.out.println("executeQuery 2");
                    //To print the rows
                    return resultSets(rs);
    
               }
               catch(Exception e)
               {
                   e.printStackTrace();
                   return false;
               }
        }
        
        public boolean getValues(int val) {
            try 
            {
                System.out.println("getvalues 1");
                String QueryStr = "select * from log_entry where id ="+val;
                System.out.println("getvalues 2");
                return executeQuery(QueryStr);
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
                return false;
            }
        }
        
         public boolean getNextId() {
            try 
            {
                System.out.println("getNextId 1");
                String QueryStr = "select max(id)+ 1,'' as datecol ,'' as comment,0 as completed from log_entry ";
                System.out.println("getNextId 2");
                return executeQuery(QueryStr);
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
                return false;
            }
        }
            
        public boolean getValues(String date) {
            try 
            {
                System.out.println("getvalues 1");
                String QueryStr = "select * from log_entry where  format(DateCol,'mm/dd/yyyy')='" + date + "'";
                System.out.println("getvalues 2");
                return executeQuery(QueryStr);
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
                return false;
            }
        }
        
        public boolean populatevalues() throws SQLException {
    
            try 
            {
                System.out.println("populatevalues 1");
                String QueryStr = "select * from log_entry";
                System.out.println("populatevalues 2");
              
                return executeQuery(QueryStr);
               
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
                return false;
            }
        }
        
        public static void main(String args[])
        {
            try {
            //JdbcConnection con = new JdbcConnection();
            //boolean values =  con.populatevalues();
            //boolean values =  con.getValues("11/07/2012");
            //boolean values1 =  con.putValues(1,"10/22/2012","test2","hi test",1);
            //boolean values1 =  con.putValues(3,"11/19/2012","","hi this is for test",1);
            //System.out.println("return values:" + values);
            //System.out.println("id:" + con.getid());
            //System.out.println("date:" + con.getdate());
            //System.out.println("description:" + con.getdescription());
            //System.out.println("comment:" + con.getcomment());
            //System.out.println("completed:" + con.getcompleted());
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    Last edited by cgk_js; 11-21-2012 at 10:20 AM. Reason: wrapping with <code>

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,814
    Rep Power
    19

    Default Re: WebApplication development using beans not useful

    You need to use [code] tags [/code] when posting code.
    That lot is unreadable without them.

    I also think that falls under the category of "too much code".

    What does it do?
    What should it do?
    What do you see happening when you run it that you should not?

    What do your logs show as happening on the server?

    You can't expect us to run this to see the problem.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    cgk_js is offline Member
    Join Date
    Oct 2012
    Posts
    21
    Rep Power
    0

    Default Re: WebApplication development using beans not useful

    i had wrapped the code separtely

    thanks for the comment..

    please find the answers..

    What does it do?
    >> it is basic simple usage, where i need to log my dailly activities. and retrive it and mark it as completed.

    What should it do?
    What do you see happening when you run it that you should not?
    >> problem is that every time i need to create a new page to do some action and load the java beans..
    >> another thing is that the bean values are set from the function called, i cannot call the another function based on the button action.
    for that i need to call the another jsp to write it...
    why it is too tedious with java beans..

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,814
    Rep Power
    19

    Default Re: WebApplication development using beans not useful

    OK, step 1 remove all the Java code from your JSP page.
    You need a servlet or two to do that work, which should call the JDBC stuff and then construct a Java object that you put in the request. You then forward to the JSP to display the data.

    Next, split out your JDBC code into a DAO and a model.
    At the moment you are doing both in one thing and, I suspect, getting all in a muddle.
    The DAO (Data Access Object) holds the JDBC code, opening and closing connections, handling result sets etc. The model is what you pass into the DAO and get back from the DAO. This is all the servlet and JSP should see.

    Once you have a better structure in place then we can see if you still have problems with your flow.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    cgk_js is offline Member
    Join Date
    Oct 2012
    Posts
    21
    Rep Power
    0

    Default Re: WebApplication development using beans not useful

    thanks for the comments, but currently i have using simple beans to do... i am planning to get used of spring framework to develop this applicaiton ..

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,814
    Rep Power
    19

    Default Re: WebApplication development using beans not useful

    And Spring will still not have you putting code into the JSP.
    Since you are asking why this stuff doesn't seem to function, I am pointing out that it helps if you use it the way it should be used.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 06-08-2012, 11:48 PM
  2. Replies: 4
    Last Post: 05-30-2011, 06:47 PM
  3. jsp webapplication
    By prakash_moturu in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-02-2009, 01:31 PM
  4. How to make Beans Lazily-instantiating beans
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-30-2008, 10:10 AM
  5. How to make Beans Lazily-instantiating beans
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 09-26-2007, 08:41 PM

Posting Permissions

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