0

Im new to java an oop programming and I'm having trouble figuring out how to process this method in a .jsp file. Sorry about the limited info so Im going to elaborate cause I'm still stuck. I have a LoginServlet that gets input from a html file.

LoginServlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String username = request.getParameter("username");
    String password = request.getParameter("password");

    loginInfo.setUsername(username);
    loginInfo.setPassword(password);

    request.setAttribute("login", loginInfo);
    ServletContext context = getServletContext();
    RequestDispatcher dispatch = context.getRequestDispatcher("/Accounts.jsp");
    dispatch.forward(request, response);
}

Then I have JavaBean.java with accessors/mutators for password and username and the JavaBean also has this method

JavaBean - get/set methods for password & username and this method getAccounts()

public Account[] getAccounts() {
    return new Account[] {
            new Account(3001, "Checking", 29.96f , 2912.96f),
            new Account(4001, "Savings", 500.00f, 10030.50f),
            new Account(6001, "IRA", 1000.25f, 43456.83f)
            };

}

Ok, now finally I have the Account.java that only the JavaBean.java has access to so I need to create an instance using getAccounts() then access the getter and setters within Account.java. Last I'm going to include the Accounts.jsp code

Accounts.jsp

<jsp:useBean id="login" class="edu.pcc.cis234j.assign04b.LoginBean" scope="request">
 <jsp:setProperty property="*" name="login"/>  
</jsp:useBean> 
<h1>Welcome, <jsp:getProperty property="userName" name="login"/> How's your day going?</h1>
<% Account[] a = login.getAccounts(); %>

So how would I process this so I can display the new account info. without having access to Account.java and having this method within JavaBean.java returning an array of Account.java instances.

4
  • where is the hard-coded array? What compiler error did you get? Commented Apr 16, 2014 at 5:43
  • more info please. Like if you have imported the class to the JSP, what error you see, if possible a SSCCE. Commented Apr 16, 2014 at 5:48
  • 2
    In jsp page you will have to add reference / import Account[] class file to access it's data and further processing. Commented Apr 16, 2014 at 5:49
  • @Nishant I have made the question complete now, sorry about that limited question. Commented Apr 16, 2014 at 15:29

3 Answers 3

2

Suppose you have this:

package p1;
public class Account{
  public Account(int x, String y, float f, float z){....}
}

package p2;
import p1;
public class Login{
 public Account[] getAccounts() {
  return new Account[] {
        new Account(3001, "Checking", 29.96f , 2912.96f),
        new Account(4001, "Savings", 500.00f, 10030.50f),
        new Account(6001, "IRA", 1000.25f, 43456.83f)
       };          
  } 
}

Then using scriplets you can access your jsp like this:

<%@page import="p1"%>
<%@page import="p2"%>
<%
  Login login = new Login();
  Account[] ac = login.getAccounts();
%>

PS: Make sure your imports are correct

Sign up to request clarification or add additional context in comments.

Comments

1
package test;

public class Account {

    String ac_id;
    String ac_bal;

    public Account(String ac_id, String ac_bal) {
        this.ac_id = ac_id;
        this.ac_bal = ac_bal;
    }

    public Account() {
    }

    public Account[] getAccounts() {
      return new Account[] {
            new Account("3001", "Checking"),
            new Account("4001", "Savings"),
            new Account("6001", "IRA")
          };          
    }
}

Above is java code ...

<%
        Account a = new Account();
        Account[] arr = a.getAccounts();
%>

This is jsp code.

It works fine without error.

Hopes this works.

1 Comment

@MavRoSCy Hey thanks for your help, I completed the question more and tried your example but Account cannot be resolved to a type, I have to access it through the JavaBean.
0

So this is what was missing and I did to complete this. First I imported the class make the compiler happy about the type Account.

<%@ page import="edu.pcc.cis234j.assign04b.LoginBean" %>
<%@ page import="edu.pcc.cis234j.assign04b.Account" %>

Then I was able to access the Account methods this way:

<% Account[] a = login.getAccounts(); %>


<% for(Account ac : a) { %>  
    <%= ac.getBalance() %>  
    <%= ac.getLastDeposit() %>
    <%= ac.getName() %>
    <%= ac.getId() %>
 <% } %> 

Now I just need to format these result into a table or something, thanks for your guys help though. I usually fix my own problems with the push of asking questions. It just seems to get my mind rolling for some reason.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.