1

このような JSP ファイルを作成しました。

<jsp:useBean id="ucz" class="pl.lekcja.beany.beany.Uczen" scope="request">
    <jsp:setProperty name="ucz" property="*"/> 
</jsp:useBean>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Podaj dane ucznia:</h1>

        <form method="POST" action="Ocen">
            <table>
                <tr>
                    <td>Imie:</td>
                    <td><input type="text" name="imie" /></td>
                </tr>
                <tr>
                    <td>Nazwisko:</td>
                    <td><input type="text" name="nazwisko" /></td>
                </tr>
                <tr>
                    <td>Punkty:</td>
                    <td><input type="text" name="punkty" /></td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" value="Oceń" /></td>
                </tr>
            </table>
        </form>
    </body>
</html>

Bean クラスの場合:

import java.io.Serializable;

public class Uczen implements Serializable {
    private String imie, nazwisko;
    private int punkty;

    public Uczen() {

    }

    public Uczen(String imie, String nazwisko, int punkty) {
        this.imie = imie;
        this.nazwisko = nazwisko;
        this.punkty = punkty;
    }

    public String getImie() {
        return imie;
    }

    public void setImie(String imie) {
        this.imie = imie;
    }

    public String getNazwisko() {
        return nazwisko;
    }

    public void setNazwisko(String nazwisko) {
        this.nazwisko = nazwisko;
    }

    public int getPunkty() {
        return punkty;
    }

    public void setPunkty(int punkty) {
        this.punkty = punkty;
    }
}

そしてサーブレット:

public class Ocen extends HttpServlet {

    private static final int PROG_PUNKTOWY = 50;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Uczen uczen = (Uczen)request.getAttribute("ucz");
        System.out.println(uczen); // <---- here prints null, always, there's no "uczen" object in attributes
        String czyZdal = "nie ";

        if (uczen.getPunkty() >= PROG_PUNKTOWY) {
            czyZdal = " ";
        }

        request.setAttribute("czyZdal", czyZdal);
        request.getRequestDispatcher("/WEB-INF/wynik.jsp").forward(request, response);
    }
}

そして、サーブレットのコードに書いたように、作成された Bean クラスではなく、常に null を出力するポイントがあります。Bean が作成されていないか、属性に追加されていません。

processRequest() は doGet() と doPost() の両方から呼び出されます

このコードのどこが間違っていますか?

4

1 に答える 1