Ho capito più o meno il concetto ma non lo saprei fare da solo. Inserisco il codice della mia pagina, Zappa potresti modificarlo tu? Io non so come fare. Grazie.
Questo è il file html:
Codice PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Da form a xml</title>
</head>
<body>
<h1>Inserimento utente</h1>
Per trasformare i dati utente in un file xml riempire i campi sottostanti
Al termine clicca su invia
</p>
<form action="FormXml2" method="post">
<table cellpadding="5" border="0">
<tr>
<td align="right">Nome:</td>
<td><input type="text" name="nome"/></td>
</tr>
<tr>
<td align="right">Cognome:</td>
<td><input type="text" name="cognome"/></td>
</tr>
<tr>
<td align="right">Età:</td>
<td><input type="text" name="eta"/></td>
</tr>
<tr>
<td align="right">Ruolo:</td>
<td><input type="text" name="ruolo"/></td>
</tr>
<tr>
<td align="right">Percorso:</td>
<td><input type="text" name="percorso" size="80"/></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Invia"/>
</tr>
</table>
</form>
</body>
</html>
Questa è la servlet
Codice PHP:
package controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
import model.ConvertToXml;
import model.Impiegato;
/**
* Servlet implementation class for Servlet: FormXml
*
*/
public class FormXml2 extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public FormXml2() {
super();
}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String nome = request.getParameter("nome");
String cognome = request.getParameter("cognome");
String eta = request.getParameter("eta");
String ruolo = request.getParameter("ruolo");
String percorso = request.getParameter("percorso");
Impiegato i = new Impiegato(nome, cognome, eta, ruolo);
ConvertToXml convert = new ConvertToXml();
convert.convertToXml(percorso, i);
model.Risposta risposta = new model.Risposta();
risposta.setRisp("ok");
//String ok = new String("ok");
request.setAttribute("risultato",risposta);
//HttpSession session = request.getSession();
//session.setAttribute("risultato", "ok");
RequestDispatcher disp = getServletContext().getRequestDispatcher("/risposta2.jsp");
disp.forward(request, response);
}
}
Infine la jsp
Codice PHP:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Risultato creazione file XML</title>
</head>
<body>
<%@ page import="model.Risposta" %>
<%Risposta risultato = (Risposta) request.getAttribute("risultato");
if ("ok".equals(risultato.getRisp())){ %>
<h2>Inserimento avvenuto con successo tramite oggetto request</h2>
<%} %>
</body>
</html>
Grazie mille!