0

This is a very stupid question, probably with a very simple answer. I have been googling around for the past week attempting to understand this, however, and have come up empty.

I have a simple servlet that allows a user to access an article posted by another user. The servlet uses the request url to determine what article to get, and follows the pattern:

http://www.example.com/Servlet/Article#

By using this number in the url, it fetches the article, puts it into a corresponding article object, which is a request-scoped managed bean, and attempts to pass the request to the a JSF, article.xhtml, which is responsible for rendering the article page.

Despite my best efforts, I appear unable to pass the Article bean created in my servlet to the JSF page. The method by which I pass the article is as follows:

//import and package declaration
public class ArticleServlet extends HttpServlet {
    //various methods
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        //Retrieve article #
        Article article = //create/fetch article using #
        request.setAttribute("article", article);
        request.getRequestDispatcher("/dev/article.xhtml").forward(request, response);
    }
}

The Article class itself is setup similar to the following:

@ManagedBean
@RequestScoped
public class Article {

private String title;
//other instance variables

public Article() 
{
    title = null;
    //init other variables to default values
}

public Article(Integer articleNum)
{
    //Retrieve article and init using the number, including the title variable
}

    //Getter and Setter methods

    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }

    //More getters and setters
}

Finally, I have the JSF page itself, which is modified from a mockup the web designer has made for me. I've attempted two ways to access the bean from within the page:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
    //header stuff, stylesheet links, etc.
</h:head>
<h:body>
    //template text, scripts, etc.
    <h2>#{article.title}</h2>
</h:body>

The other method I have attempted is replacing the outputText tag with #{article.title}. The first method, as I have it posted, just fills in the space in the h2 tag with nothing, and the second method literally prints the characters #{article.title} to the page.

Am I attempting to use JSF for something it isn't meant for? Am I not passing the bean through to the page properly? All I know is that this should be a fairly simple task, yet I haven't been able to figure it out regardless of the amount of searching I've done.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Aargonian
  • 212
  • 2
  • 12
  • 1
    Why do you do this in a servlet? And why do you ask if you use jsf what it was not meant for? It is more that you use a servlet in a combination with jsf that is not optimal – Kukeltje Jul 11 '15 at 07:16
  • Looks like you merely want an URL rewriting solution. For that, you should use a filter, not a servlet. Look at existing URL rewriting solutions for JSF like PrettyFaces / Rewrite. Knowing that, [this whole question doesn't make any sense](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). In the future, try to ask how to solve a certain functional requirement instead of how to get a wrong solution to work. – BalusC Jul 11 '15 at 08:18
  • @BalusC: to me it looks OP also wants to retrieve something. `f:viewAction` comes to mind. Read http://stackoverflow.com/questions/6377798/what-can-fmetadata-fviewparam-and-fviewaction-be-used-for and see if it helps you solving it a jsf way (assuming you want to use jsf as mvc) – Kukeltje Jul 11 '15 at 08:36
  • @Kukeltje You would be correct. – Aargonian Jul 11 '15 at 18:36
  • There is some initialization that I wish to do on the story using the Article number provided. I either need a way of using the Article's constructor method from within the JSF itself (and grabbing the article # from the url), or I need a way to pass it to the JSF from the servlet, which is the method I chose to travel. Being relatively new to JSF, I am unknowledgable of a way to not use the default constructor on the bean when the JSF is loaded. In order for the Article class to load the proper article, it needs to have its constructor with the Article# called. Am I doing this the wrong way? – Aargonian Jul 11 '15 at 18:39
  • Did you check the link in my comment? – Kukeltje Jul 11 '15 at 21:35
  • Is this acceptable as dupe? http://stackoverflow.com/questions/10724428/how-do-i-process-get-query-string-url-parameters-in-backing-bean-on-page-load/ Or perhaps this? http://stackoverflow.com/questions/8459903/creating-master-detail-pages-for-entities-how-to-link-them-and-which-bean-scope – BalusC Jul 11 '15 at 21:55
  • You had a point with me really treating it like the XY problem. I was looking for a way to make my current solution (instantiating the bean from a servlet first) work, rather than solving my real problem. My real problem, or at least as best I can define it, was that I needed to somehow let the bean know what URL (or article#) had been requested /before/ the page was rendered, so it could have the correct information. No arg constructors wouldn't allow me to do this, so I needed some way to make it work. – Aargonian Jul 12 '15 at 22:01

0 Answers0