0

I am using Eclipse Java EE IDE for Web Developers - Version: Neon.1a Release (4.6.1) - Build id: 20161007-1200 with Tomcat apache-tomcat-8.0.38 I am trying to implement some jsf example project but I am facing problem that the Servlet objects are not being identified. I configured server Tomcat v.8.0 and in Windows -> preference -> server -> runtime environment I have this tomcat version too. What else should I do to work with the servlets?

Code

package at.irian.jsfatwork.gui.page;

import java.io.IOException;
import java.io.PrintWriter;

import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;


/*
 * customer profile
 */
@ManagedBean
@SessionScoped
public class Customer {
    private String firstName;
    private String lastName;
    private Boolean useCreditCard = Boolean.FALSE;
    private String creditCardType;
    private String creditCardNumber;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Boolean getUseCreditCard() {
        return useCreditCard;
    }

    public void setUseCreditCard(Boolean useCreditCard) {
        this.useCreditCard = useCreditCard;
    }

    public String getCreditCardType() {
        return creditCardType;
    }

    public void setCreditCardType(String creditCardType) {
        this.creditCardType = creditCardType;
    }

    public String getCreditCardNumber() {
        return creditCardNumber;
    }

    public void setCreditCardNumber(String creditCardNumber) {
        this.creditCardNumber = creditCardNumber;
    }

    public String save() {
        return "/showCustomer.xhtml";
    }

    public void useCreditCardChanged(ValueChangeEvent ev) {
        Boolean useCreditCard = (Boolean) ev.getNewValue();
        if (useCreditCard != null) {
            this.useCreditCard = useCreditCard;
        }
        FacesContext.getCurrentInstance().renderResponse();
    }

    public String export() {
        FacesContext fc = FacesContext.getCurrentInstance();
        try {
            // Get writer from servlet context, this won't work with portlets! 
          //I can not imported the HttpServletResponse here?
            HttpServletResponse response = (HttpServletResponse)fc.getExternalContext().getResponse();
            // Set content type to plain text
            response.setContentType("text/plain");
            // Get writer and print data
            PrintWriter writer = response.getWriter();
            writer.print("First Name: ");
            writer.println(firstName);
            writer.print("Last Name: ");
            writer.println(lastName);
            if (useCreditCard) {
                writer.print("Credit Card Type: ");
                writer.println(creditCardType);
                writer.print("Credit Card Number: ");
                writer.println(creditCardNumber);
            }
            // Skip further lifecycle processing
            fc.responseComplete();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
B_PRIEUR
  • 160
  • 1
  • 1
  • 18
water
  • 405
  • 1
  • 5
  • 13
  • What you mean by Servlet objects are not being identified – Thusitha Thilina Dayaratne Oct 14 '16 at 12:59
  • @ThusithaThilinaDayaratne: are not being imported. I am getting error and I should create the `HttpServletResponse` class. Usually it should work with `import javax.servlet.http.HttpServletResponse;` but it does not even appear to import it and the `servlet-api.jar` is in the lib of the apache-tomcat-8.0.38\lib – water Oct 14 '16 at 13:17
  • Why do you need Servlet library for JSF managed bean? What your `export()` method doing is not the way JSF is conceived to work. You better take a look at [JSF tutorial](http://docs.oracle.com/javaee/6/tutorial/doc/gjaam.html) and come back if the example there does not work for you. – ujulu Oct 14 '16 at 13:40
  • @ujulu: I am actualy reading a book JSF 2.2 from Michael Kurz and this was a bean from his example!? – water Oct 14 '16 at 13:58
  • well, I haven't read the book. But when I see the rating on Amazon: one user gives 5 (a friend of him?) and the other gives 1; you better judge it. For me, JSF managed beans != Servlet. – ujulu Oct 14 '16 at 14:04

0 Answers0