• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to get attribute from session in regular java class ?  RSS feed

 
asaf shay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have file index.jsp , that approach to reguar java class and call function . in that function i want to get attribute from session like this :

List<Coupon> couponsList = (List<Coupon>)request.getSession().getAttribute("listOfCouponsThatNotExpired");

because this regular java class i get error message , someone know how to solve this ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
144
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get the list from the session in the servlet, and pass it the to "regular" Java class as an argument.

You should not pass things like the session (and request, and response) to other classes. Under no circumstances "infect" any classes to have to know about servlet specifics (like the session) unless absolutely necessary.

(If you are putting Java code in your JSP (as your post seems to indicate), then you have shot yourself in the foot by writing JSP as if it were over 12 years ago. Please read this JspFaq entry).
 
asaf shay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but the problem is that i"m talking about the first page index.jsp .and he the onlyone from other pages that doesn't go Through the servlet
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
144
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All pages should go through a page controller servlet.

But in any case, how is the class being called if not from a servlet?
 
asaf shay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i call him from my index.jsp .

I'm more interested to call the index.jsp from servlet this will solve the problem .i try couple of things and go back if have problem
thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
144
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regardless of whether you call it from a servlet or Java code in a JSP, the answer is the same: get the list from the session and pass it as an argument to the class. As few classes as possible should have to import servlet stuff.

My point otherwise -- which has little to do with your specific question -- is that having Java code in a JSP is an obsolete technique (over 12 years obsolete -- not a few years, 12 of them! 12!) that you should not be practicing.
 
asaf shay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
meaning if i have website test.com and i want to open my first page .

i need to make shure that if i insert in the address bar www.test.com ,i need to redirect to www.test.com/servletName/index.jsp ??
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
144
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can make the url of the page controller for the first page an entry in the welcome-list in web.xml.
 
asaf shay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:You can make the url of the page controller for the first page an entry in the welcome-list in web.xml.


Maybe I did not understand your answer, but I did not understand how it helps me ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
144
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You asked how to make sure that using the URL with no path goes to the right place. The welcome list in web.xml tells the app what to do when there is no path. By default, it first goes to index.jsp. You change that behavior in web.xml.
 
asaf shay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:You asked how to make sure that using the URL with no path goes to the right place. The welcome list in web.xml tells the app what to do when there is no path. By default, it first goes to index.jsp. You change that behavior in web.xml.


it's not what i"m asking but thanks anyway about the info you give me .

there is two way to open a file : for example index.jsp
first this how i open until now)
http://localhost:8080/MiniCouponsWebsite/index.jsp
meaning not Through the servlet
second : (that Through the servlet )
http://localhost:8080/MiniCouponsWebsite/ServletName/index.jsp

How can I ensure that if someone tries the first way he would be redirect to the second option ? correct me if i Wrong Through session ?

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
144
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your second url is not the correct way. The URL should be to the servlet page controller and not even mention the JSP. Who cares what the JSP is named? The end user sure doesn't.

See this article if this pattern isn't familiar.

To prevent JSPs from being directly navigated to, they are placed somewhere under WEB-INF. This prevents them from being directly served, but allows page controllers to forward to them.
 
asaf shay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Your second url is not the correct way. The URL should be to the servlet page controller and not even mention the JSP. Who cares what the JSP is named? The end user sure doesn't.

See this article if this pattern isn't familiar.

To prevent JSPs from being directly navigated to, they are placed somewhere under WEB-INF. This prevents them from being directly served, but allows page controllers to forward to them.


1)to open index.jsp i agree with you that the end user don't care about the jsp file name but !! ,i build coupons site and one of the option is to let pepole to save to favorite there search result for example to see coupon by specific town , if i want to navigate when he open the link from the favorite i need to let him save querystring and i will navigate him when he return the next time.so it's not always true that the user don't need to know the jsp file , becouse as i explain i want to know.

2)to put file under WEB-INF not prevents anything ,i can still open the file directly
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
144
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) No, you do not need to bookmark JSP files. In fact, you should not. A JSP without its page controller makes no sense. You should create URL mappings that make sense. JSP names should never be part of a URL.

2) Wrong. You cannot open a JSP that is under WEB-INF by using a URL in the browser.
 
asaf shay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:1) No, you do not need to bookmark JSP files. In fact, you should not. A JSP without its page controller makes no sense. You should create URL mappings that make sense. JSP names should never be part of a URL.

2) Wrong. You cannot open a JSP that is under WEB-INF by using a URL in the browser.


1)if you don't let the user the option to save to favorite you limit him .and in my site i don't want to limit .

2)i just upload to youtube how i can,i put file under WEB-INF that call bla.jsp and i open it /


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
144
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
asaf shay wrote:1)if you don't let the user the option to save to favorite you limit him .and in my site i don't want to limit .

Please show me where I said you should not allow bookmarks. I never said that. Anywhere.

I said you do not need the name of the JSP to create bookmarks. You can bookmark any URL.

2)i just upload to youtube how i can,i put file under WEB-INF that call bla.jsp and i open it /

That video does not show anything being accessed from inside WEB-INF.
 
asaf shay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
asaf shay wrote:1)if you don't let the user the option to save to favorite you limit him .and in my site i don't want to limit .

Please show me where I said you should not allow bookmarks. I never said that. Anywhere.

I said you do not need the name of the JSP to create bookmarks. You can bookmark any URL.

2)i just upload to youtube how i can,i put file under WEB-INF that call bla.jsp and i open it /

That video does not show anything being accessed from inside WEB-INF.


1)I told you that if I want a user saves to Favorites Result of seraching by some thing i have to know the query string and from that i know what he searching for .

2)Apparently we have a misunderstanding ,the file bla.jsp in the WEB-INF and I managed to open so i don't know what else to say it's also in the video You can clearly see
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
144
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
asaf shay wrote:1)I told you that if I want a user saves to Favorites Result of seraching by some thing i have to know the query string and from that i know what he searching for

That still does not mean that the name of a JSP needs to be used in the URL. Why do you think you need a JSP name? Any URL that gets the info can be bookmarked.

2)Apparently we have a misunderstanding ,the file bla.jsp in the WEB-INF

I can barely see the text in the video, but I do not see where WEB-INF is any prat of the url or path.

WebContent is not WEB-INF.



 
asaf shay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
asaf shay wrote:1)I told you that if I want a user saves to Favorites Result of seraching by some thing i have to know the query string and from that i know what he searching for

That still does not mean that the name of a JSP needs to be used in the URL. Why do you think you need a JSP name? Any URL that gets the info can be bookmarked.

2)Apparently we have a misunderstanding ,the file bla.jsp in the WEB-INF

I can barely see the text in the video, but I do not see where WEB-INF is any prat of the url or path.

WebContent is not WEB-INF.


about the second part i now understand what you saing and i will check it .

1)i use the query string like i said , is it bad programing ?

last Question so if i want to open index.jsp (this is my starting page) i will redirect like this
http://localhost:8080/MiniCouponsWebsite/ServletName/welcome

is it good to leave the name of the servlet in the address ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
144
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
asaf shay wrote:1)i use the query string like i said , is it bad programing ?

Query strings are fine (see footnote*), and they have nothing at all to do with the JSP name. A path such as the following is fine, and id perfectly bookmark-able.

last Question so if i want to open index.jsp (this is my starting page) i will redirect like this
http://localhost:8080/MiniCouponsWebsite/ServletName/welcome

No redirect is necessary. You should configure the web app to automatically go to the correct path with the welcome list in the web.xml.

is it good to leave the name of the servlet in the address ?

No, it is a bad practice and exposes implementation details in the path. Make the urls meaningful (see footnote*).





* The trend in the industry is towards meaningful URLs -- also called RESTful or "friendly" URLs. For example, rather than the URL I shoed above, a better path would beThe info is part of the path, not a query string. This is not mandatory, but it's considered modern practice.
 
asaf shay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for all the info
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!