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

JSP questions  RSS feed

 
cle tan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1st let's say i want to perform a insert sql query into an audit table if the field is not the same

field is taken from mysql

but i dunno why i can't compare it with a string? i can get them to display the fields from the database though

<% if (mysqlvar!="test"){
//perform sql query
}

%>

another qn is using session to ensure the user doesn't bypass a page, by entering that url, which can be seen while registering to this forum

let's say the flow is step1.jsp->step2.jsp->step3.jsp

i do not want the user to bypass step 2 by typing step3.jsp in the url

so i used a session prev_url in step 2 and 3.jsp to keep track of the prev_url

is it a correct way to do this?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cle tan wrote:
but i dunno why i can't compare it with a string?
<% if (mysqlvar!="test"){
//perform sql query
}

%>

use equals to compare strings.

cle tan wrote:
so i used a session prev_url in step 2 and 3.jsp to keep track of the prev_url
is it a correct way to do this?

why do you want to keep previous url string? why not the user id? by the way: use Filter
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66205
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cle tan wrote:1st let's say ....

First, let's move any Java code out of the JSP. Using Java code in a JSP ia a poor practice that has been discredited for over 10 years now. Please read this JspFaq entry.

but i dunno why i can't compare it with a string? i can get them to display the fields from the database though

Once this code is in a Java class instead of a JSP, it'll be easier to diagnose.

Also, Please use real words when posting to the forums. Abbreviations such as "dunno" in place of "don't know" only serve to make your posts more difficult to read and less likely to generate useful responses. Please click this link ⇒ UseRealWords for more information.

another qn is using session to ensure the user doesn't bypass a page, by entering that url, which can be seen while registering to this forum

"question" not "qn".

is it a correct way to do this?

Not sure because you haven't explained what you are doing. But I would not keep track of URLs. Rather, keep track of the data.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and Welcome to JavaRanch cle tan ! :)
 
cle tan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
cle tan wrote:1st let's say ....

First, let's move any Java code out of the JSP. Using Java code in a JSP ia a poor practice that has been discredited for over 10 years now. Please read this JspFaq entry.


but i dunno why i can't compare it with a string? i can get them to display the fields from the database though

Once this code is in a Java class instead of a JSP, it'll be easier to diagnose.

Also, Please use real words when posting to the forums. Abbreviations such as "dunno" in place of "don't know" only serve to make your posts more difficult to read and less likely to generate useful responses. Please click this link ⇒ UseRealWords for more information.

another qn is using session to ensure the user doesn't bypass a page, by entering that url, which can be seen while registering to this forum

"question" not "qn".

is it a correct way to do this?

Not sure because you haven't explained what you are doing. But I would not keep track of URLs. Rather, keep track of the data.


then what should be in the jsp?
i keep functions like executing sql query and those functions in java
then i use usebean to use the functions

why do you want to keep previous url string? why not the user id? by the way: use Filter
let's say you are registering a person but needs to take a few steps

step1.jsp-> step2.jsp-> step3.jsp

normally you will write a seperate servlet, then use a filter to prevent bypass of step2.jsp?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66205
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cle tan wrote:then what should be in the jsp?

Template text, JSP actions, JSTL and EL. There should be no Java code in a JSP.

If you are not using JSTL and EL, your JSP knowledge is 10 years out of date!
 
cle tan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:and Welcome to JavaRanch cle tan ! :)

hi all
i'm just starting out with programming jsp,so i am quite new to this

Seetharaman Venkatasamy wrote:
cle tan wrote:
but i dunno why i can't compare it with a string?
<% if (mysqlvar!="test"){
//perform sql query
}

%>

use equals to compare strings.


i also tried using this but doesn't work

if(mysqlvar=="test"){}
else{perform sql query

}
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66205
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cle tan wrote:i'm just starting out with programming jsp,so i am quite new to this

So you are saying that because you are new that you should be learning the wrong way to do things? Why not learn the right way right from the start?

i also tried using this but doesn't work
if(mysqlvar=="test"){}

Are you new to Java as well? Because that is not the way to compare Java strings.
 
cle tan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
cle tan wrote:i'm just starting out with programming jsp,so i am quite new to this

So you are saying that because you are new that you should be learning the wrong way to do things? Why not learn the right way right from the start?

i also tried using this but doesn't work
if(mysqlvar=="test"){}

Are you new to Java as well? Because that is not the way to compare Java strings.


i am not very sure what is the right way, but logic like this is ok in the jsp?

yeah, i am new to java, came from C background, i know how to write a class, create a new object in main java application, and know how to get some variables in jsp using request.getAttribute, session.getAttribute
java code logic looks like c, so i assume a c comparison would work

i found that if i compare with a session parameter i got from the prev page with a string it works

but if i compare with a parameter i got from the database it does not work


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66205
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cle tan wrote:
i am not very sure what is the right way, but logic like this is ok in the jsp?

Conditional logic for display purposes is fine, but you need to be using the JSTL tags, not Java code to do this. You need to update your JSP knowledge with a modern reference. I recommend the Head First Servlets and JSP book.

yeah, i am new to java, came from C background, i know how to write a class, create a new object in main java application, and know how to get some variables in jsp using request.getAttribute, session.getAttribute
java code logic looks like c, so i assume a c comparison would work

Your assumption is very very wrong.

I highly recommend learning a lot more about Java before trying to use it with Servlets and JSP. If you don't know Java well -- and it's clear that you do not -- you will have a very hard time getting servlets and JSPs to work.
 
cle tan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


wonder why even if value of name and new name is the same, sql query is still performed

is it because i cannot compare a value which i extracted from database using recordset, and a string?

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66205
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cle tan wrote:
is it because i cannot compare a value which i extracted from database using recordset, and a string?

OF course you can compare them. You just need to learn how to do it in Java. The == operator is not the way to do it. Again, you need to learn Java before trying to write JSPs and Servlets.
 
cle tan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so it should be like this?



saw it from
http://leepoint.net/notes-java/data/expressions/22compareobjects.html
 
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!