Hello and thank you for reading this question:
I will explain the use case first and the approach I have tried:
I have an ArrayList which holds Courses'data and it is being created in the FrontServlet and it is being stored in the session.
I would like to convert the ArrayList to XML to show it as HTML transforming it with XSL.
Mi doubt is: how could we convert an ArrayList to XML?
I have studied how to annotate classes to transform them: https://www.mkyong.com/java/jaxb-hello-world-example/
I have used an static XML to convert it and show it: Java: Implementing Transform View pattern, to convert XML to HTML with an XSL file
I have read a good post about extending ArrayList<> to be able to transform it: Why my ArrayList is not marshalled with JAXB?
I first tried with:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author YonePC
*/
@XmlRootElement
public class Curso {
@XmlElement
public void setTitulo(String titulo) {
this.titulo = titulo;
}
@XmlElement
public void setAutor(String autor) {
this.autor = autor;
}
@XmlElement
public void setAsignatura(String asignatura) {
this.asignatura = asignatura;
}
@XmlElement
public void setDuracion(String duracion) {
this.duracion = duracion;
}
@XmlElement
public void setVideo(String video) {
this.video = video;
}
String titulo, autor, asignatura, duracion, video;
public Curso(String titulo, String autor, String asignatura, String duracion, String video) {
this.titulo = titulo;
this.autor = autor;
this.asignatura = asignatura;
this.duracion = duracion;
this.video = video;
}
public String getTitulo() {
return titulo;
}
public String getAutor() {
return autor;
}
public String getAsignatura() {
return asignatura;
}
public String getDuracion() {
return duracion;
}
public String getVideo() {
return video;
}
}
But I do not need the Curso class itself, I need to convert the ArrayList which stores them.
In addition, I currently stores the courses being created in the session. How could we get the session's info in a servlet? I mean, we can access them from the servlet where it has being currently created, and from JSP pages, but how do we do access into servlets?
I ask the previous question because in all the examples I found about converting Java to XML, they showed new instances and not how to convert previous stored ones.
The code I have tried:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package frontController;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
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 javax.servlet.http.HttpSession;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.Curso;
/**
*
* @author YonePC
*/
@WebServlet(name = "CourseInfoCommand", urlPatterns = {"/CourseInfoCommand"})
public class CourseInfoCommand extends FrontCommand {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
public String getServletInfo() {
return "Short description";
}// </editor-fold>
@Override
public void process(HttpServletRequest request) {
try {
File file = new File("C:\\Users\\YonePC\\Videos\\ASAPLICACIONCURSOSPRACTICA1\\src\\java\\frontController\\Cursos.xml");
ArrayList courses = (ArrayList) session.getAttribute("cursos");
JAXBContext jaxbContext = JAXBContext.newInstance(ArrayListCourses.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.marshal(courses, file);
TransformerFactory factory = TransformerFactory.newInstance();
StreamSource xsl = new StreamSource(new File(""));
Transformer newTransformer = factory.newTransformer(xsl);
StreamSource xml = new StreamSource(new File(""));
PrintWriter writer = response.getWriter();
Result result = new StreamResult(writer);
newTransformer.transform(xml, result);
writer.println(writer.toString());
forward("/CourseInfo.jsp");
} catch (ServletException ex) {
Logger.getLogger(CourseInfoCommand.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(CourseInfoCommand.class.getName()).log(Level.SEVERE, null, ex);
} catch (TransformerConfigurationException ex) {
Logger.getLogger(CourseInfoCommand.class.getName()).log(Level.SEVERE, null, ex);
} catch (TransformerException ex) {
Logger.getLogger(CourseInfoCommand.class.getName()).log(Level.SEVERE, null, ex);
} catch (JAXBException ex) {
Logger.getLogger(CourseInfoCommand.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Thank you for your help.