Sondaggio

Creare un form con la domanda: Ti piace la scuola?
e i 4 radio button: Molto, Abbastanza, Poco, Per niente,
associati ai valori: 4, 3, 2, 1.
Alla pressione del tasto Invia il valore selezionato viene inviato alla servlet grafico che accumula nelle 4 variabili molto, abbastanza, poco, per_niente i valori inviati dagli utenti e restituisce l’istogramma aggiornato con le percentuali come nell’esempio.

grafico
Progetto Eclipse: Sondaggio (Eclipse).zip

Esercizio 22

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Imposta</title>
    </head>
    <body>
        <h1>Calcola la tua imposta</h1>
        <%String cifra = request.getParameter("cifra");
            if (cifra == null) {
                cifra = "";
            }
            String aliquota = request.getParameter("aliquota");
            if (aliquota == null) {
                aliquota = "";
            }
        %>         
        <form action="index.jsp" method="post">
            Inserisci la cifra imponibile <input type="text" value="<%=cifra%>" name="cifra"/><br/>
            Inserisci l'aliquota <input type="text" value="<%=aliquota%>" name="aliquota"/>%<br/>
            <input type="submit" value="invia"/> 
            <input type="reset" value="cancella"/>           
        </form>
        <%
            if ((!cifra.isEmpty()) && (!aliquota.isEmpty())) {
                double c = Double.parseDouble(cifra);
                double a = Double.parseDouble(aliquota);
        %><p> La cifra che devi versare allo stato è di: <%= c * a / 100%> €
            <% }
            %>
        </p>
    </body>
</html>

potenze.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>Potenze - Gianfranco Oddenino</title>
 <link rel="stylesheet" type="text/css" href="stile.css">
 </head>
 <body>
 <%@ include file="WEB-INF/jspf/header.jspf"%>
 <%@ include file="WEB-INF/jspf/menu.jspf"%>
 <div id="content">
 <h2>Potenze</h2>
 <form action="potenze.jsp" method="get">
 Inserisci un numero <input type="text" name="n"/><br/>
 Seleziona l'operazione
 <select name="op">
 <option value="2">Quadrato</option>
 <option value="3">Cubo</option>
 <option value="4">Radice quadrata</option>
 </select><br/>
 <input type="reset" value="Cancella" />
 <input type="submit" value="Calcola" />
 </form>
 <% String valore = request.getParameter("n");
 if (valore != null) {
 int n = Integer.parseInt(valore);
 %>
 <p>n = <%=n%><br />
 <% int op = Integer.parseInt(request.getParameter("op"));
 switch (op) {
 case 2: %>n² = <%=n * n %><% break;
 case 3: %>n³ = <%=n * n * n %><% break;
 case 4: %>n½ = <%=Math.sqrt(n)%><% break;
 }
 }
 %>
 </p>
 </div>
 <%@ include file="WEB-INF/jspf/footer.jspf"%>
 </body>
</html>

Istogramma

index.html

<!DOCTYPE html>
<html>
    <head>
        <title>Istogramma</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <h1>Istogramma</h1>
        <p>JSP<br /><img src="istogramma.jsp?v=60&v=120&v=180&v=240&v=300&v=360" /></p>
        <p>Servlet<br /><img src="istogramma?v=60&v=120&v=180&v=240&v=300&v=360" /></p>
    </body>
</html>

istogramma.jsp

<%@page import="javax.imageio.ImageIO"%>
<%@page import="java.io.OutputStream"%>
<%@page import="java.awt.Color"%>
<%@page import="java.awt.Graphics"%>
<%@page import="java.awt.image.BufferedImage"%>
<%@page contentType="image/png"%>
<%  // legge i parametri
    String[] v = request.getParameterValues("v");
    // crea l'immagine
    BufferedImage img = new BufferedImage(600, 200, BufferedImage.TYPE_INT_RGB);
    Graphics g = img.createGraphics();
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, img.getWidth(), img.getHeight());
    for (int i = 0; i < v.length; i++) {
        int val = Integer.parseInt(v[i]);
        g.setColor(Color.getHSBColor(val / 360.0f, 1, 1));
        g.fill3DRect(0, i * 20, val, 16, true);
        g.setColor(Color.BLACK);
        g.drawString(" " + val, val, i * 20 + 13);
    }
    // invia l'immagine in output
    OutputStream os = response.getOutputStream();
    ImageIO.write(img, "png", os);
    os.close();
%>

istogramma (servlet)

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("image/png");
    try (OutputStream os = response.getOutputStream()) {
        // legge i parametri
        String[] v = request.getParameterValues("v");
        // crea l'immagine
        BufferedImage img = new BufferedImage(600, 200, BufferedImage.TYPE_INT_RGB);
        Graphics g = img.createGraphics();
        // cancella lo sfondo
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, img.getWidth(), img.getHeight());
        // disegna l'istogramma
        for (int i = 0; i < v.length; i++) {
            int val = Integer.parseInt(v[i]);
            g.setColor(Color.getHSBColor(val / 360.0f, 1, 1));
            g.fill3DRect(0, i * 20, val, 16, true);
            g.setColor(Color.BLACK);
            g.drawString(" " + val, val, i * 20 + 13);
        }
        // invia l'immagine in output
        ImageIO.write(img, "png", os);
    }
}

Tabelline in JSP

index.jsp

<%-- 
 Document : index
 Created on : 12-gen-2015, 11.24.08
 Author : GfO
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>Tabelline per bambini/e</title>
 </head>
 <body>
 <h1>Tabelline per bambini/e</h1>
 <p>Caro bambino/a, inserisci la base della tabellina che vuoi vedere...</p>
 <form action="tabellina.jsp" method="get">
 Base <input type="text" name="base" />
 <input type="submit" value="OK" />
 </form>
 </body>
</html>

tabellina.jsp

 <%-- 
 Document : tabellina
 Created on : 12-gen-2015, 11.50.00
 Author : GfO
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>Tabelline per bambini/e</title>
 </head>
 <body>
 <h1>Tabellina del <% int b = Integer.parseInt(request.getParameter("base"));%> <%=b%></h1>
 <% for (int i = 1; i <= 10; i++) {%>
 <p><%=b%> x <%=i%> = <%=b * i%></p>
 <% }%>
 </body>
</html>