Hello Everyone! A new thread i have started and this is how the problem goes...

I want to display record of a user something like profile details based on the user who has logged in to member's page! So when the user has logged in and clicks on the view profile details link it should take to the profile.jsp and display the details of the user who has logged in to the member's page.

That is how it is!

All Help shall be highly appreciated.

hy anand! thanks for the reply but im using the DAO (Data Access Object) pattern for display of record! MVC is not part of my requirement! cud u suggest me an idea of how to use it! I have a DAO.java file and i knw sessions in the servlet has to be implemented to show the record!

Cud u help me with the code?

Thankin u in advance

sagar_danceking

If you say that MVC is not part of your requirement , would you be able to explain me principles of DAO?

Yes Peter i cn! All Code should be in a DAO File such as Database Connection and the SQL Statements and the records should be called in the JSP whilst nt having the Database Code in the JSP! Servlets should nt also contain any database code as previously mentioned as it is a violation of DAO.

So how does it differ from MVC model?

The MVC Model (Model View Controller) pattern acts more like being able to handle several requests from the user and dispatch them to the respective servlet! An Example would be a Relay Servlet being able to Dispatch the request to a servlet based on a hidden field from the Java Server Page!.

Eg:

//Login.jsp
<form name="LoginForm" action="Relay" method="POST">

<input type="text" name="Username">

<input type="password" name="password">

<input type="hidden" name="ActionID" value="LoginUser">

<input type="submit" name="submit" value="Login">
//Relay Servlet

if(request.getParameter("ActionID").equals("LoginUser"))
{
     RequestDispatcher rd = request.getRequestDispatcher("LoginUser");
     rd.forward , response; // Forwards to the Login User Servlet.
}

DAO is simply acting as a layer where all code for the database is in a java file and has to be called in servlets! Methods are called in servlet so that is how my system should act! Cud u assist me with a logic at least? I would really appreciate your thanks.

THANK U !

I MUST SAY! IT IS GREAT TO BE PART OF THIS COMMUNITY AS I AM GETTING TO LEARN ALOT!

MY THANKS AGAIN FOR THE ASSISTANCE IN THE PREVIOUS THREAD!

You have already written the code at your previous thread. Click the link that goes to the profile.jsp, call the method that gets the data and display them.
Pass as argument to the method the username. When you login, put the username in the session and get it at that page.

When the user logs in:

session.setAttribute("USER_NAME", user_name_value);

When the user logs out:

session.setAttribute("USER_NAME", null);

Whenever you need the username take it:

String userName = (String)session.getAttribute("USER_NAME");

If the userName is null then no one is logged in. You need to put that in an if statement and redirect to the login page if the username taken from the session is null

And since you have written all that code in the previous thread this should be very easy, because you already go to a page, run a DAO method and display.

And what you have described is the MVC. And it is not like: "MVC is not in my requirements" because it is not something someone asks you not to do. It's a "way" of solving things which you can follow. And that link provided has a very good example of how you should do things.
But that is only an example. If you want to learn JSP, you need to study from a book or a tutorial. Not rely solely on examples and scrip-lets of code taken from here

Hy friend! i used the same method as per in the previous thread! Calling the array list from the DAO.java in to the JSP Everything! The only issue encountered is that it refuses to accept the Username as a parameter in the method! Since in the previous thread the counter was of integer variable , in this the Username is a string and it is what we are passing as a paramter!

String Username = (String)session.getAttribute("Username");

session.setAttribute("Username", Username);

OshwalDAO dao = DAO.getDAOInterface();

ArrayList GetLoggedInUser = dao.GetLoggedInUser();

if(Username!=null)
{

BorrowerRecordViews Borrower = (BorrowerRecordViews)GetLoggedInUser.get(Username);
//Here is the Method GetLoggedInUser equal to the method of dao.GetLoggedInUser(). The Parameter as u cn see is the Username and is refusing to accept the Username because it can only accept the Integer variable as a parameter.

}

Any ideas on how to solve it? All Help Appreciated!

Thanking Once Again!

sagar_danceking

Then pass as parameter the integer that it needs. I don't know your code to tell you what to pass and how. I told you the methodology.
Call the method that returns the data that you need and display them.
Now you have to think and make the neccassary changes.

Also: I assume that when you get the username from the session you had put it there when you logged in. It doesn't automicaly gets there

If the method takes as argument an int then put in the session the int that you need and take that when you call the method. If you don't have access to that int change the method to take as parameter the username instead of the int.

Hy Friend i am understanding the logic behind it! Jiust like previous thread in this one i have already passed the username as a parameter in the DAO.java! Here it is:

//DAO.java
//This is the Method Called:

        public ArrayList<BorrowerRecordViews> GetLoggedInUser(String Username)
                throws ClassNotFoundException , SQLException
        {
            try
            {
                ArrayList<BorrowerRecordViews> LoggedInUser = new ArrayList<BorrowerRecordViews>();
                Statement SQLStatement = getDatabaseConnection();
                String SQLQuery = "SELECT * FROM Borrower WHERE Username = '" + Username +"'";
                ResultSet LoggedInResult = SQLStatement.executeQuery(SQLQuery);

                while(LoggedInResult.next())
                {
                    LoggedInUser.add(populateBorrowerObject(LoggedInResult));
                }
                DestroySQLConnection();
                return LoggedInUser;
            }

            catch (ClassNotFoundException cnfe)
            {
                System.out.println(cnfe);
                throw cnfe;
            }

            catch (SQLException SQLE)
            {
                System.out.println(SQLE);
                throw SQLE;
            }
        }

Borrow.jsp
Here it is all ok!

The Code For JSP:

String Username = (String)session.getAttribute("Username");

session.setAttribute("Username", Username);

OshwalDAO dao = DAO.getDAOInterface();

ArrayList GetLoggedInUser = dao.GetLoggedInUser(Username);

BorrowerRecordViews LoggedInUser = (BorrowerRecordViews)GetLoggedInUser;

<tr><td><b>Your ID</b></td><td><input type="text" name="BorrowerID" value="<%=LoggedInUser.getStudentID()%>"></tr>

There is no Problem in the Code! No Errors shown! BUT! When i run the program it says that it cannot cast the Arraylist to the BorrowerRecordViews.java

Here is the Code For BorrowerRecordViews.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DataAccessObject;

/**
 *
 * @author Sagar Joshi
 */

//An Interface that contains the incomplete methods that cannot be instantiated. Since it is an interface the methods are implemented by the BookViews Class.

public interface BorrowerRecordViews {

    public int getStudentID();

    public void setStudentID(int StudentID);

    public String getBorrowerFirstName();

    public void setBorrowerFirstName(String FirstName);

    public String getBorrowerMiddleName();

    public void setBorrowerMiddleName(String MiddleName);

    public String getBorrowerLastName();

    public void setBorrowerLastName(String LastName);

    public String getBorrowerAge();

    public void setBorrowerAge(String Age);

    public String getBorrowerNumber();

    public void setBorrowerNumber(String MobileNumber);

    public String getBorrowerEMail();

    public void setBorrowerEMail(String EMail);

    public String getBorrowerUsername();

    public void setBorrowerUsername(String Username);

    public String getBorrowerCourse();

    public void getBorrowerCourse(String Course);

    public String getBorrowerRole();

    public void setBorrowerRole(String Role);
    

}

It gives me cast exception!

All Help Appreciated friend! Thanks :-)

You wrote:

ArrayList GetLoggedInUser = dao....
BorrowerRecordViews LoggedInUser = (BorrowerRecordViews)GetLoggedInUser;

GetLoggedInUser is a list. Why do you cast it to BorrowerRecordViews ?
Also you wrote the code for the GetLoggedInUser method, right? Then I don't see what is the problem. You should know how to call it and what to do with the results, since you wrote the implementation: You put BorrowerRecordViews objects in the list that you return That is the solution to your problem

If you are having problems then I would suggest going back to your notes and study how to call methods, and how to use generics and lists.

Hy Friend! Sorry for late reply! Have been busy trying to figure out how to Come up with a solution! In the previous post i had used the same way to project the records of a borrower in the same way as i have shown here with only the difference of the Paramter being passed.! In the previous post i had!

int Counter = 0;

ArrayList GetBorrowersRecord = dao.getBorrowerRecord();

BorrowerRecordViews Borrower = (BorrowerRecordViews)GetBorrowerRecord.get(Counter);

It is the Same Way! But here we are passing the Username as a parameter! So why would the cast class exception be occured?

It would have also occured in the previous post as well!

Or is it because the parameter being passed is session?

Thanking u very much indeed for your help!

Thanks!
:-)
cHEERz

Anyone to assist please! I am so lost and cannot be able to figure out!

Helloz i have finally done it! It is working! I used the solution provided abt the MVC Architecture by @peter_budo! Thank u everyone! Im marking this thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.