Hey everybody!

My task is to implement a simple membership database for a sports club. The index.jsp is my 'welcome file' within I offer input fields, first and last name, a selection for sport and check boxes for payment.

The index.jsp looks like this:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Members</title>
        <link rel="stylesheet" type="text/css" href="./css/style.css">
    </head>
    <body>
    <div id="page">
        <h2>Members</h2>
        <form name="data" action="Controller?target=insert" method="POST">
        <p>first name: <input name="firstname" type="text" size="30" maxlength="30"></p>
        <p>last name: <input name="lastname" type="text" size="30" maxlength="30"></p>
        <p>sport: <select name="sports" type="text" size="1">
            <option>tennis</option>
            <option>soccer</option>
            <option>squash</option>
            <option>badminton</option>
            </select>
        </p>
        <p>payer: <input type="radio" name="payment" value="yes"> yes -
                  <input type="radio" name="payment" value="no"> no</p>
        <!-- alternative parameter handling: hidden fields -->
        <!-- <input name="target" type="hidden" value="insert"> -->
        <input type="submit" value="store">    
        </form>
        <br />
        <br />
        <a href="Controller?target=view">view</a>
    </div>    
    </body>
</html>

Within the Controller.java each member should be stored in a java.util.List. I wasn't able to do that, so I tried to store the members in an ArrayList.

-----Control.java-----

package sport;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import sport.domain.*;
import java.util.ArrayList;

public class Controller extends HttpServlet{
ArrayList list = new ArrayList();
      
protected void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        ServletContext application=getServletContext();
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        String target=null;
        try {
            target=request.getParameter("target");
        }catch (Exception ex1) { }
        
        if (target==null) {
        
            RequestDispatcher dispatcher=application.getRequestDispatcher("/index.jsp");
            dispatcher.forward(request,response);
        }else if (target.equals("insert")) {
            try {
                String fname=request.getParameter("firstname");
                String lname=request.getParameter("lastname");
                String sport=request.getParameter("sports");
                String payer=request.getParameter("payment");
                 
                if (!fname.equals("") && !lname.equals("") && !sport.equals("") && !payer.equals("")) {
                      
                   list.add(new Member (fname, lname, sport, payer));
                                     
                    request.setAttribute("allmembers", list);
                    RequestDispatcher dispatcher=application.getRequestDispatcher("/view.jsp");
                    dispatcher.forward(request,response);
                }else {
                    RequestDispatcher dispatcher=application.getRequestDispatcher("/index.jsp");
                    dispatcher.forward(request,response);
                }
            }catch (Exception ex1) { System.out.println(ex1); }
        }else if (target.equals("view")) {
            request.setAttribute("allmembers", list);
            RequestDispatcher dispatcher=application.getRequestDispatcher("/view.jsp");
            dispatcher.forward(request,response);
        }
    } 
}

To store each Member I'll use the Member bean:
-----Member.java-----

package sport.domain;

import java.io.Serializable;

public class Member implements Serializable {
    
    private String fname;
    private String lname;
    private String payer;
    private String sport;

    public Member(String fname, String lname, String payer, String sport) {
        this.fname = fname;
        this.lname = lname;
        this.payer = payer;
        this.sport = sport;
    }

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getLname() {
        return lname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    public String getPayer() {
        return payer;
    }

    public void setPayer(String payer) {
        this.payer = payer;
    }

    public String getSport() {
        return sport;
    }

    public void setSport(String sport) {
        this.sport = sport;
    }
     
}

And here are my problems:
The results should be offered by an Bean (java.util.List) of member-objects to the view.jsp.
Do you know how I can access these results within the view.jsp???
I'm sure the solution is pretty simple, but unfortunately not for me at the moment!

Would be great if someone could help me!

Greetings
Sirrana

This article has been dead for over six months. Start a new discussion instead.