aspose file tools*
The moose likes Servlets and the fly likes Sessions on a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Sessions on a servlet" Watch "Sessions on a servlet" New topic
Author

Sessions on a servlet

matias casal
Ranch Hand

Joined: Oct 18, 2009
Posts: 61
Hello
I have a doubt with servlets and jsp
I want to pass a session atributte put in a servlet, into a jsp page.

Is it possible?

How should I do that?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

matias casal wrote:I want to pass a session atributte put in a servlet, into a jsp page.
Is it possible?

JSPs by default participate in a session. So, yes it's possible.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
matias casal
Ranch Hand

Joined: Oct 18, 2009
Posts: 61
How should I make that?

I am setting the session attribute in the servlet in this way


How should I call the session in the jsp ?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

matias casal wrote:How should I call the session in the jsp ?

It depends on how you plan to retrieve it in the JSP... Are you using custom tags, JSTL or ....
matias casal
Ranch Hand

Joined: Oct 18, 2009
Posts: 61
I am using "normal" jsp without tags
Charles 'King
Ranch Hand

Joined: Jul 05, 2009
Posts: 56

matias casal wrote:I am using "normal" jsp without tags


You should avoid using jsp code in the view...Here's your answer:

<%= session.getAttribute("who") %>
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

matias casal wrote:I am using "normal" jsp without tags

What do you mean by "normal" ? You should consider using EL, JSTL, Custom actions, Standard actions in JSP. You may be using scriptlets etc.. ? then that's a very old way of doing things in JSP & a very bad practice too.
matias casal
Ranch Hand

Joined: Oct 18, 2009
Posts: 61
I am using that , because i am only starting with jsp
I proved using


but it prints null
Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 365
Please put your servlet Jsp code....


Thanks,
Shailesh
matias casal
Ranch Hand

Joined: Oct 18, 2009
Posts: 61
the servlet it has the doget and dopost after but with anything in them
the jsp
Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 365
I hope from doget & dopost you are calling processRequest method,
then you are calling servlet from application and after you are forwarding to Jsp...
I hope in this sequence only this is happening .....
If it is then it should work.....
Please confirm....
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Shailesh Narkhede wrote:I hope from doget & dopost you are calling processRequest method,
then you are calling servlet from application and after you are forwarding to Jsp...
I hope in this sequence only this is happening .....
If it is then it should work.....
Please confirm....



No he is not doing this way, actually he is thinking that when he will write this code


then that servlet will be called and attribute will be set in the session.
He is going in wrong direction.


SCJP 6
matias casal
Ranch Hand

Joined: Oct 18, 2009
Posts: 61
I am calling the processrequest method in doget + dopost.
I am calling the servlet in the web.xml in this way

Finally, when you said forwarding to Jsp what are trying to tell me?
THank you
matias casal
Ranch Hand

Joined: Oct 18, 2009
Posts: 61
Punit Singh wrote:
Shailesh Narkhede wrote:I hope from doget & dopost you are calling processRequest method,
then you are calling servlet from application and after you are forwarding to Jsp...
I hope in this sequence only this is happening .....
If it is then it should work.....
Please confirm....



No he is not doing this way, actually he is thinking that when he will write this code


then that servlet will be called and attribute will be set in the session.
He is going in wrong direction.


Whats the correct direction?
Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 365
page import directive is for impoting class only, like we are importing package in simple java file,
by using page import doGet or doPost is not going to call...]

what you need to do is ,
call servlet from brower directly by url-pattern in web.xml,
and then in servlet sfter setting attribute to session forward to jsp by,



hope this will help.....

matias casal
Ranch Hand

Joined: Oct 18, 2009
Posts: 61
It works,
thank you
however i would like to learn the best way to take the session attribute from a servlet in a jsp.

Should I use the strut framework?
Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 365
first clear basic concept of servlet & JSP then you can better use struts framework,
For that you can use head first servlet & JSP that is very nice book...
matias casal
Ranch Hand

Joined: Oct 18, 2009
Posts: 61
Ok
thank you.

I am reading the 314 sun book.
I think that it is a good one
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
matias casal wrote:Ok
thank you.

I am reading the 314 sun book.
I think that it is a good one


Always try to follow a book that gives sample codes or examples to clear your fundamentals running the code.
Else try to make samples yourself.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

Struts is most certainly not necessary. If your application is configured correctly, session scoped variables are easily accessible by simply naming them in an EL expression:

This is no longer 2001; scriptlets should no longer be used in JSP pages.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
matias casal
Ranch Hand

Joined: Oct 18, 2009
Posts: 61
Bear Bibeault wrote:Struts is most certainly not necessary. If your application is configured correctly, session scoped variables are easily accessible by simply naming them in an EL expression:

This is no longer 2001; scriptlets should no longer be used in JSP pages.


Now I am confused, what are you trying to tell me?

I have a doubt now

I am using this in a jsp page


but the css and js of the jsp are not load, what should I use to load the jsp with the css and javascript ?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

matias casal wrote:Now I am confused, what are you trying to tell me?

That scriptlets should no longer be used in JSP pages.

matias casal wrote:but the css and js of the jsp are not load, what should I use to load the jsp with the css and javascript ?
matias casal
Ranch Hand

Joined: Oct 18, 2009
Posts: 61
but if i need something like this?



And in other.jsp i have in the head


I did something like that , and mysyle.css and xxx.js are not loaded.

How should i do the same loading the css and js?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

matias casal wrote:but if i need something like this?

This is the type of decision that should be made in the controller before the JSP is loaded.

I did something like that , and mysyle.css and xxx.js are not loaded.

Most likely because the URLs are invalid.
matias casal
Ranch Hand

Joined: Oct 18, 2009
Posts: 61
I think that the urls are ok because when i run index.jsp without being called from the servlet with the

the css and js are load.

But when i run the index.jsp with this:

the css and js are not load
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

matias casal wrote:I think that the urls are ok because when i run index.jsp without being called from the servlet ...

You can keep thinking that and continue to have issues, or you can fix the URLs and get it to work.

Page-relative URLs do not work in the servlet environment -- see the JSP FAQ for information on formatting proper server-relative URLs for your resources.
matias casal
Ranch Hand

Joined: Oct 18, 2009
Posts: 61
I am using something like this

When I view source code in the mozilla firefox
it appears
<link href="/proyect1/css/index.css" rel="stylesheet" type="text/css" />
which is the real path of my css.

But the index.jsp continues without the syle

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

What happens when you hit the URL "http://localhost:8080/proyect1/css/index.css" in the browser address bar?

(Assumes localhost:8080 hits your server)
matias casal
Ranch Hand

Joined: Oct 18, 2009
Posts: 61
The index.jsp appears :S
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

Huh? Are you really saying that you type in the address of a .css file and a .jsp appears instead?
matias casal
Ranch Hand

Joined: Oct 18, 2009
Posts: 61
Nnono,
in the url appears
http://localhost:8080/proyecto/css/index.css

but it is shown as web page the index.html
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

I have no idea what you are trying to say. First, you say that that when you type in the address of the CSS file, it shows a JSP. And then you say that it shows an HTML?

Please explain clearly and specifically what appear in the browser when you type in the address of the CSS file.
tushar panda
Ranch Hand

Joined: Feb 16, 2009
Posts: 133
matias casal wrote:How should I call the session in the jsp ?


by using custom tags,jstl or by scriptlets(not preferred)
using EL : simply put ${variablename} - you get the session variable

using scriplets(again not preferred but you can do it)

<% String var1 = session.getAttribute("variablename");%>

matias casal wrote: I am using this in a jsp page ; request.sendRedirect("other.jsp");

its not "request.sendRedirect("other.jsp");" it is "response.sendRedirect("other.jsp");"

you can use this in your head instead :<meta http-equiv="refresh" content="3;url=yourpage.jsp">

cheers
TUSHAR

Do Right , Fear No Man
matias casal
Ranch Hand

Joined: Oct 18, 2009
Posts: 61
Sorry I am using


My problem is that the css styles and js are not loaded in the jsp.
They are called in this way.


Thank you
matias casal
Ranch Hand

Joined: Oct 18, 2009
Posts: 61
can it be a problem of the web.xml ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sessions on a servlet
 
Similar Threads
which book HFSJ or Sun guide?
difference
Do you have elegant way of nullpointer exceptions
Using JSTL example in K&B Book
Servlet and JSP