1 Replies - 514 Views - Last Post: 16 April 2015 - 04:30 PM

#1 percydw   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-April 15

Acessing an Instance of an Object from a Serlvet in Java Web App

Posted 16 April 2015 - 04:11 PM

Hi there, I just subscribed to your forum because I am working on a project and have found it difficult to get help. I posted my question to Stackoverflow but thought I should check out other places for help aswell. With that in mind, I have now cross-posted this question to a few different places. Thankyou for reading.


I have a service AnnJAXB.java that performs a GET request and puts the response in an Ann class (which contains Anime and Info classes as attributes). I modified the service to return Ann so I can potentially use the return value. I annotated the Ann, Anime and Info classes to say @Stateless.

I have a servlet ControllerServlet that I want to use to (initially) print attributes of Ann to a JSP webpage frontend. I took code that was initially in the AnnJAXB service class and put it in the servlet. This code is basically the for loop:

for (Anime anime : ann.getAnn()) {


It seems that none of the contents of the for loop are displayed (neither the html nor the Ann/Anime attributes).

My service/method class AnnJAXB:

package main;

import entities.*;
import javax.xml.bind.*;
import java.io.*;
import java.net.URL;
import java.net.MalformedURLException;
import javax.ejb.Stateless;

@Stateless
public class AnnJAXB {

    public Ann Unmarshalling(String searchString) throws JAXBException, MalformedURLException {


            JAXBContext jc = JAXBContext.newInstance(Ann.class);
            Unmarshaller ums = jc.createUnmarshaller();
            URL url = new URL( "http://cdn.animenewsnetwork.com/encyclopedia/api.xml?title=~"+searchString );


            Ann ann = (Ann) ums.unmarshal(url);

            return ann;
    }
}

My serlvet class ControllerServlet. Note I have not added anything to config files or anything like that. I just right-clicked and made a servlet class and started modifying it.

package main;

import entities.Anime;
import entities.Ann;
import entities.Info;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 *
 * @author J
 */
@WebServlet(name = "ControllerServlet", urlPatterns = {"/ControllerServlet"})
public class ControllerServlet extends HttpServlet {

//    @EJB
//    private AnnJAXB annJAXB = new AnnJAXB();

    @EJB
    private Ann ann;

    /**
     * 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");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ControllerServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ControllerServlet at " + request.getContextPath() + "</h1>");

            AnnJAXB an = new AnnJAXB();
            try{
            an.Unmarshalling("evangelion");
            }catch(Exception e){}

            for (Anime anime : ann.getAnn()) {

                out.println("<h1>Loop1</h1>");

                out.println("<h1>ID: " + anime.getId() + "</h1>");
                out.println("<h1>Name: " + anime.getName() + "</h1>");

                for (Info temp : anime.getAnime()) {
                    if (temp.getSrc() != null) {
                        out.println("<h1>Info: " + temp.getSrc() + "</h1>");
                    }
                }
            }

            out.println("<h1>End</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }

(other default methods here)

What happens when it runs:

The IDE asks me for a execution url and I leave it as /ControllerServlet?. A webpage opens and prints the words:

Servlet ControllerServlet at /prototype9

End

It should show Ann attributes inbetween those words.

Here is the Ann class for reference:

package entities;

import javax.xml.bind.annotation.*;
import java.util.*;
import javax.ejb.Stateless;

@Stateless
@XmlRootElement(name = "ann")
public class Ann {

    private List<Anime> Ann = new ArrayList<Anime>();

    @XmlElement(name="anime")
    public List<Anime> getAnn() {
        return Ann;
    }

    public void setAnn(List<Anime> Ann) {
        this.Ann = Ann;
    }

    public Ann() {
        super();
    }
}

Thankyou so much for reading.

Is This A Good Question/Topic? 0
  • +

Replies To: Acessing an Instance of an Object from a Serlvet in Java Web App

#2 percydw   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-April 15

Re: Acessing an Instance of an Object from a Serlvet in Java Web App

Posted 16 April 2015 - 04:30 PM

FIXED - see top of thread

FIXED -
sorry i cant see an edit button anywhere.


I changed the loop to say:

 for (Anime anime : annj.Unmarshalling("evangelion").getAnn()) {


and removed

 annj.Unmarshalling("evangelion");

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1