Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

read cookies when load the start JSP  RSS feed

 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! I'm studying the "cookie" topic. Here is the code:
index.jsp



servlet CookieTest which is started after action="servlet_a"



The question - Is it the only and the good way to read and put cookie in index.jsp like I did?

 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way of setting cookies is- Call a Javascript function on click of the button. Set cookies in the Javascript function.
But first thing is "avoid writing Java code in JSP".
From your post I believe you have just started learning JSP/Servlet. It is a very good time to follow the standard which will be helpful in long run.

Never ever write a single line of Java code in JSP.
 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapas Chand wrote:Another way of setting cookies is- Call a Javascript function on click of the button. Set cookies in the Javascript function.
But first thing is "avoid writing Java code in JSP".
From your post I believe you have just started learning JSP/Servlet. It is a very good time to follow the standard which will be helpful in long run.

Never ever write a single line of Java code in JSP.

Well, that is I'm afraid of and do not understand. Do you mean this -> <%= statement %> an evil code? Why shall not I use it?
And one more question. A lot of times during learning JSP/Servlet (yep, I'm the very beginner in it) I saw the answer: "...use JavaScript..." But I expected to use the whole power of JAVA and I'm confused. Does JAVA have the power in WEB developing?
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergey Lotvin wrote:Well, that is I'm afraid of and do not understand. Do you mean this -> <%= statement %> an evil code? Why shall not I use it?

Scriplets are evil because they make the code difficult to
1. Read
2. Maintain
3. Reuse

I have experienced the problem in past. If JSP is small you will not face any problem, but once it gets more than some 200 lines and you keep inserting Java code, it will become hell of an effort to modify something in that.
Read this nice article how to write web application.


Sergey Lotvin wrote:And one more question. A lot of times during learning JSP/Servlet (yep, I'm the very beginner in it) I saw the answer: "...use JavaScript..." But I expected to use the whole power of JAVA and I'm confused. Does JAVA have the power in WEB developing?

I do not think I am qualified enough to comment on this, I think I should leave it to the experienced moderators.

Anyway my personal opinion is - I always use things which will be easily done and which can be maintained easily in future and which can be understood by my fellow developers who happen to work on the code which I have written.
So if I feel Javascript will make life easier I use it.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java scriptlets have been obsolete for over 15 years. 15 years! And with good reasons, many of which have already been listed.

And even if you are going to use scriptlets, which you should not, the following is nonsense:. The proper, but now obsolete way, to display data would have been "<%= cookielist[1].getValue() %>"

But you need to find yourself a modern tutorial and learn modern JSP.
 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSP - obsolete, ok. But what is the modern substitute? what do people use when create big WEB applications?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No one said JSP is obsolete.

Java scriptlets in a JSP are obsolete. They are part of JSP 1.x. JSP 2 was introduced in 2001 with the EL (Expression Language) and JSTL (JSP Standard tag Library). That's what you should be using.
 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gotcha! JSTL - I'll try it. Thanks!
 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW! I found out that this smell-code:


doesn't work when first start the page. When there no any cookie. I changed it to:


Also won't work. Why?
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergey Lotvin wrote:Also won't work. Why?

This is exactly why people are suggesting you to avoid scriplet in JSP.
It will only make things so complicated that it will be difficult to debug.

Now your first step should be remove all Java code from this JSP and move them to a servlet.
Dispatch the request from that servlet to this JSP after setting all necessary data in request.
Display the data in JSP using EL.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!