home *** CD-ROM | disk | FTP | other *** search
/ ftp.disi.unige.it / 2015-02-11.ftp.disi.unige.it.tar / ftp.disi.unige.it / pub / .person / CataniaB / teach-act / DB3 / servlet-bean-jsp / Main.java < prev    next >
Text File  |  2001-04-02  |  3KB  |  95 lines

  1. import java.io.*;
  2. import javax.servlet.*;
  3. import javax.servlet.http.*;
  4. import java.text.*;
  5. import java.util.*;
  6.  
  7. /**
  8.  * Gestione studenti
  9.  *
  10.  * @author Mirko Manea
  11.  */
  12. public class Main extends HttpServlet {
  13.  
  14.     DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ITALY);
  15.  
  16.     public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  17.     doGet(req, res);
  18.     }
  19.  
  20.     public void doGet(HttpServletRequest req, HttpServletResponse res)
  21.     throws ServletException, IOException {
  22.  
  23.     String jspPage = "/jsp/error.jsp";
  24.  
  25.     DBMS dbms;
  26.  
  27.     try {
  28.         dbms = new DBMS();
  29.     } catch (DBMSException e) {
  30.         String error = "Non è possibile avere una connessione al database:" + e.getMessage();
  31.         throw new ServletException(error);
  32.     }
  33.  
  34.     try {
  35.         String cmd = req.getParameter("cmd");
  36.  
  37.         if (cmd == null) {
  38.             jspPage = "/jsp/menu.jsp";
  39.         } else {
  40.  
  41.             // visualizzazione studente
  42.             if (cmd.equals("vis")) {
  43.                 // estrazione studente
  44.                 StudenteBean studente = dbms.extractStudente(req.getParameter("matricola"));
  45.                 // aggiunta alla request del bean
  46.                 req.setAttribute("studente", studente);
  47.                 // pagina jsp di presentazione
  48.                 jspPage = "/jsp/visStudente.jsp";
  49.             }
  50.  
  51.             // visualizzazione studenti
  52.             if (cmd.equals("vis-tutti")) {
  53.                 // estrazione studenti
  54.                 Vector lista = dbms.extractStudenti();
  55.                 // aggiunta alla request del vettore
  56.                 req.setAttribute("listaStudenti", lista);
  57.                 // pagina jsp di presentazione
  58.                 jspPage = "/jsp/visTuttiStudenti.jsp";
  59.             }
  60.  
  61.                     // richiama jsp per l'inserimento di uno studente
  62.             if (cmd.equals("ins")) {
  63.                 // aggiungi studente
  64.                 jspPage = "/jsp/insStudente.jsp";
  65.             }
  66.  
  67.                     // inserimento studente
  68.             if (cmd.equals("fai-ins")) {
  69.                 StudenteBean studente = new StudenteBean();
  70.                 // poplazione bean
  71.                 studente.setMatricola(req.getParameter("matricola"));
  72.                 studente.setCognome(req.getParameter("cognome"));
  73.                 studente.setNome(req.getParameter("nome"));
  74.                 studente.setFacolta(req.getParameter("facolta"));
  75.                 studente.setDataNascita(df.parse(req.getParameter("data_nascita")));
  76.                 studente.setCreditiOttenuti(Integer.parseInt(req.getParameter("crediti_ottenuti")));
  77.                 // inserimento studente
  78.                 dbms.insStudente(studente);
  79.                 // menu
  80.                 jspPage = "/jsp/menu.jsp";
  81.             }
  82.  
  83.         }
  84.  
  85.     } catch (Exception e) {
  86.         req.setAttribute("javax.servlet.jsp.jspException", e);
  87.         jspPage = "/jsp/error.jsp";
  88.     }
  89.  
  90.     RequestDispatcher rd = getServletContext().getRequestDispatcher(jspPage);
  91.         rd.forward(req, res);
  92.     }
  93.  
  94. }
  95.