Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. package it.uniroma3.servlet.controller;
  2.  
  3. import java.io.IOException;
  4.  
  5. import javax.servlet.RequestDispatcher;
  6. import javax.servlet.ServletContext;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.annotation.WebServlet;
  9. import javax.servlet.http.HttpServlet;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12.  
  13. import it.uniroma3.servlet.model.Studente;
  14.  
  15. @WebServlet("/richiesta")
  16. public class StudenteController extends HttpServlet {
  17.  
  18.     /**
  19.      *
  20.      */
  21.     private static final long serialVersionUID = 1L;
  22.  
  23.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  24.             throws IOException, ServletException {
  25.  
  26.  
  27.         String nome = request.getParameter("nome");
  28.         String cognome = request.getParameter("cognome");
  29.         String matricola = request.getParameter("matricola");
  30.         int mat = 0;
  31.         boolean errori = false;
  32.         String nextPage;
  33.  
  34.  
  35.         if (nome == null || nome.equals("")) {
  36.             request.setAttribute("errNome", "campo obbligatorio");
  37.             errori = true;
  38.         }
  39.  
  40.         if (cognome == null || cognome.equals("")) {
  41.             request.setAttribute("errCognome", "campo obbligatorio");
  42.             errori = true;
  43.         }
  44.  
  45.         if (matricola == null || matricola.equals("")) {
  46.             request.setAttribute("errMatricola", "campo obbligatorio");
  47.             errori = true;
  48.         } else {
  49.        
  50.         try {
  51.             mat = Integer.parseInt(matricola);
  52.             } catch(NumberFormatException e) {
  53.                 request.setAttribute("errMatricola", "Deve essere un numero");
  54.                 errori = true;
  55.             }
  56.         }
  57.  
  58.         // controllo se ci sono errori e decido a quale pagina reindirizzare
  59.  
  60.         if (!errori) {
  61.             Studente studente = new Studente();
  62.  
  63.             nome = nome.toUpperCase();
  64.             cognome = cognome.toUpperCase();
  65.             nome = nome.trim();
  66.             cognome.trim();
  67.            
  68.  
  69.             studente.setNome(nome);
  70.             studente.setCognome(cognome);
  71.             studente.setMatricola(mat);
  72.  
  73.             request.setAttribute("studente", studente);
  74.             nextPage = "/studente.jsp";
  75.  
  76.         } else {
  77.  
  78.             nextPage = "/newStudente.jsp";
  79.         }
  80.  
  81.         // inoltro la richiesta
  82.  
  83.         ServletContext application = this.getServletContext();
  84.         RequestDispatcher rd = application.getRequestDispatcher(nextPage); // qui va nextPage
  85.         rd.forward(request, response);
  86.         return;
  87.  
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement