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 >
Wrap
Text File
|
2001-04-02
|
3KB
|
95 lines
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.text.*;
import java.util.*;
/**
* Gestione studenti
*
* @author Mirko Manea
*/
public class Main extends HttpServlet {
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ITALY);
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doGet(req, res);
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String jspPage = "/jsp/error.jsp";
DBMS dbms;
try {
dbms = new DBMS();
} catch (DBMSException e) {
String error = "Non è possibile avere una connessione al database:" + e.getMessage();
throw new ServletException(error);
}
try {
String cmd = req.getParameter("cmd");
if (cmd == null) {
jspPage = "/jsp/menu.jsp";
} else {
// visualizzazione studente
if (cmd.equals("vis")) {
// estrazione studente
StudenteBean studente = dbms.extractStudente(req.getParameter("matricola"));
// aggiunta alla request del bean
req.setAttribute("studente", studente);
// pagina jsp di presentazione
jspPage = "/jsp/visStudente.jsp";
}
// visualizzazione studenti
if (cmd.equals("vis-tutti")) {
// estrazione studenti
Vector lista = dbms.extractStudenti();
// aggiunta alla request del vettore
req.setAttribute("listaStudenti", lista);
// pagina jsp di presentazione
jspPage = "/jsp/visTuttiStudenti.jsp";
}
// richiama jsp per l'inserimento di uno studente
if (cmd.equals("ins")) {
// aggiungi studente
jspPage = "/jsp/insStudente.jsp";
}
// inserimento studente
if (cmd.equals("fai-ins")) {
StudenteBean studente = new StudenteBean();
// poplazione bean
studente.setMatricola(req.getParameter("matricola"));
studente.setCognome(req.getParameter("cognome"));
studente.setNome(req.getParameter("nome"));
studente.setFacolta(req.getParameter("facolta"));
studente.setDataNascita(df.parse(req.getParameter("data_nascita")));
studente.setCreditiOttenuti(Integer.parseInt(req.getParameter("crediti_ottenuti")));
// inserimento studente
dbms.insStudente(studente);
// menu
jspPage = "/jsp/menu.jsp";
}
}
} catch (Exception e) {
req.setAttribute("javax.servlet.jsp.jspException", e);
jspPage = "/jsp/error.jsp";
}
RequestDispatcher rd = getServletContext().getRequestDispatcher(jspPage);
rd.forward(req, res);
}
}