Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP/Servlet - clarification needed  RSS feed

 
Jim Daddy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am writing a java webapp using JSP/Servlets and am looking for a little help regarding implementing something in a best practice approach. Here are the steps in my program:

1) User logs in to index.jsp
2) User goes to display.jsp
3) display.jsp opens XML file and displays it.

I initially thought that from within "display.jsp" I could call a bean (jsp:useBean) and have it return Objects from the XML file. The issue with this is that my XML file is included within my web application, so I do not want to hardcode the location to the XML file (as this could change depending on where it is deployed). I would be able to get past this by passing the realpath to the Bean, like:
<% myBean.setPath( getServletContext().getRealPath("") ); %>

This however seems like its not the way I should be doing it. So I decided to look up how other people have done this, and most examples seem to point to submitting to a servlet, and having it pass the path to the Bean, then forwarding to a jsp page. This however confuses me, since the way I am writing this I only have a "JSP" page which should display this, no forms etc.

Can anyone please help me determine how I should implement this?

Thanks a lot!
 
Jim Daddy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again, after a bit of searching here is a solution I have come across. Is this solution a good or bad way to do it?

display.jsp


DisplayServet


Any feedback on this is appreciated

Thanks.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Today, 'best practice' means no scriptlets and no real logic (other than markup) in the JSPs. If you look up "Model, View, Controller" or 'MVC', you should be able to find quite a bit on the subject.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66187
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Jim Daddy",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66187
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might also find this article helpful.
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The issue with this is that my XML file is included within my web application, so I do not want to hardcode the location to the XML file (as this could change depending on where it is deployed).


Check out the following elements that are part of the web.xml configuration file for the application. You may store the location to the XML in the web.xml file. And, then read these values from the code.

<context-param>
<init-param>
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!