Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package it.uniroma3.servlet.controller;
- import java.io.IOException;
- import javax.servlet.RequestDispatcher;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import it.uniroma3.servlet.model.Studente;
- @WebServlet("/richiesta")
- public class StudenteController extends HttpServlet {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws IOException, ServletException {
- String nome = request.getParameter("nome");
- String cognome = request.getParameter("cognome");
- String matricola = request.getParameter("matricola");
- int mat = 0;
- boolean errori = false;
- String nextPage;
- if (nome == null || nome.equals("")) {
- request.setAttribute("errNome", "campo obbligatorio");
- errori = true;
- }
- if (cognome == null || cognome.equals("")) {
- request.setAttribute("errCognome", "campo obbligatorio");
- errori = true;
- }
- if (matricola == null || matricola.equals("")) {
- request.setAttribute("errMatricola", "campo obbligatorio");
- errori = true;
- } else {
- try {
- mat = Integer.parseInt(matricola);
- } catch(NumberFormatException e) {
- request.setAttribute("errMatricola", "Deve essere un numero");
- errori = true;
- }
- }
- // controllo se ci sono errori e decido a quale pagina reindirizzare
- if (!errori) {
- Studente studente = new Studente();
- nome = nome.toUpperCase();
- cognome = cognome.toUpperCase();
- nome = nome.trim();
- cognome.trim();
- studente.setNome(nome);
- studente.setCognome(cognome);
- studente.setMatricola(mat);
- request.setAttribute("studente", studente);
- nextPage = "/studente.jsp";
- } else {
- nextPage = "/newStudente.jsp";
- }
- // inoltro la richiesta
- ServletContext application = this.getServletContext();
- RequestDispatcher rd = application.getRequestDispatcher(nextPage); // qui va nextPage
- rd.forward(request, response);
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement