Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sessions on a servlet

 
matias casal
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
matias casal
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using "normal" jsp without tags
 
Charles 'King
Ranch Hand
Posts: 56
Eclipse IDE Postgres Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using that , because i am only starting with jsp
I proved using


but it prints null
 
Shailesh Narkhede
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please put your servlet Jsp code....
 
matias casal
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the servlet it has the doget and dopost after but with anything in them
the jsp
 
Shailesh Narkhede
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
matias casal
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok
thank you.

I am reading the 314 sun book.
I think that it is a good one
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64829
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
matias casal
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64829
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64829
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64829
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The index.jsp appears :S
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64829
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Huh? Are you really saying that you type in the address of a .css file and a .jsp appears instead?
 
matias casal
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64829
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
matias casal
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can it be a problem of the web.xml ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic