File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Retrieving data from a servlet then forward to JSP page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Retrieving data from a servlet then forward to JSP page" Watch "Retrieving data from a servlet then forward to JSP page" New topic
Author

Retrieving data from a servlet then forward to JSP page

ellie spring
Greenhorn

Joined: Jun 15, 2011
Posts: 24
hi im new to jsp and i am
having problem with my simple web application.

I want to display data to jsp page using the forward() method of RequestDispatcher in my servlet.
The servlet handles and retrieves data from a textfile then set attributes for the request object.

but the problem is when i display them in my jsp page im getting null values..
what i did is just set attributes for the request object and get that attributes from my servlet..

i hope anyone can help me..thanks in advance


"Nothing is impossible, impossible takes a little longer".
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

Can you post some code, because there are quite a few things that can go wrong.

I have a feeling that you are directly accessing the JSP page instead of using the Servlet's mapped url.


OCJP6, OCWCD5
ellie spring
Greenhorn

Joined: Jun 15, 2011
Posts: 24

yes im accessing directly the JSP page..

Im following the MVC pattern where my servlet will be the controller and the view will be the jsp page.
What i want is when I access the JSP page it will automatically call the servlet where it handles data retrieval from the data source like database,
then the servlet will forward the retrieved data to jsp page for display.

here is my servlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

DatabaseManager dm = new DatabaseManager();
User user = dm.getUser();

request.setAttribute("user", user);
RequestDispatcher view = request.getRequestDispatcher("items.jsp");
view.forward(request, response);

}

here is the code in my jsp

<%

User s = (User)request.getAttribute("user");
out.println(s);
%>

im getting null values in my jsp page.I think when the page loads it does not call the servlet because it does not send a form or something.
Is there a better way or good way that i may be able to display a data from database in my jsp page when i first accessed it.

thanks for the reply..

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61662
    
  67

ellie spring wrote:
yes im accessing directly the JSP page..
Well then the servlet is never executing, so of course the scoped variable isn't set. Access the servlet, not the JSP!

And please UseCodeTags when posting code to the forums.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
ellie spring
Greenhorn

Joined: Jun 15, 2011
Posts: 24

Thanks for the reply.

Is there a better way to display data retrieve from a data source into JSP page when the page is first accessed like
clicking the link to that specific jsp page.

What i want is when i click the link to my jsp page. The page will display information retrieve from the data source automatically.

Sorry for not using codes tags.I am new here.



Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61662
    
  67

ellie spring wrote:Is there a better way to display data retrieve from a data source into JSP page when the page is first accessed like
clicking the link to that specific jsp page.

Never link to a JSP page. Always link to the servlet controller for the JSP page. In a properly constructed web application, a JSP will never be directly accessed without first calling its controller.

Please read this article for information on properly structuring web applications.

If you are new to JSP, you might also find this article helpful.
ellie spring
Greenhorn

Joined: Jun 15, 2011
Posts: 24

Thank you for the information. I really appriciate it.

Well i am new to jsp and web application so im starting from scratch.
I guessed i need a lot more reading and studying.

Thanks..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Retrieving data from a servlet then forward to JSP page