• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retrieving data from a servlet then forward to JSP page

 
ellie spring
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mike Zal
Ranch Hand
Posts: 144
Fedora Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ellie spring
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ellie spring
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic