Reputation: 923
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.
Upvotes: 0
Views: 395
Reputation: 17839
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
Upvotes: 2
Reputation: 923
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.
Upvotes: 0
Reputation: 308
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.
Upvotes: 1