Wednesday 18 September 2013

JSP Expressions:

JSP Expressions:


Basically whatever you put in JSP Expressions the code is evaluated and you get the value as a output so let's  say that I have a variable called int i=10 when I put variable i in JSP Expressions tags it is going to print 10 as a output but not variable i. So basically expressions whatver you put expressions in the expression tags and displays output value.

Explanation about the Expressions:

What are Expressions?
Ans: Whatever goes inside the "<%={java code here}>". Onething you have to note in jsp Expressions is whenever you put some in jsp expressions you do not put a semi colon( ;) at the end so that is not allowed.
Lets say if you want to print the value of variable i you can not say like this . <%=i;%>//invalid because of semicolon,
<%=i%>//ok
Code inside jsp expressions is evaluated, and output is displayed
Whatever you put inside expressions should evaluate to a value.
Basically sometimes you will need to print a certain texts on to the page, on to your web page, lets say the user logs in and after successfully login in on the login page or main page, we are going to display "Welcome username" , so while going to display this dynamic username you can not do it in static html because whatever you put in static html page will remain same for each and every user and you can not really change that value there so inorder to do that you need to insert dynamic content, whatever you enter the username we need to display that username in the main page so you can do that using scriptlets since you said that scriptlets whatever you code you put in scriptlets the expression is evaluated as a output  is displayed .

Example: <%=localVar %>
So eventhough we saw that how we can completely perform all the business logic processing and my java code within jsps it is not really recommended to do bulk of business logic of in your jsps because as we talk many times main role of jsp is it should be for presentation logic and not business logic. The role of the business logic is played by the servlets and its supporting classes and not by jsps . So jsp is only should use for presentation logic so inorder to present the jsp a webpage the user , inorder to present the dynamic web page to the user whatever dynamic certain amount of logic or certain amount of business logic we need to write in your jsp you can put that otherwise all the hard code business logic process should be done in servlets and other supporting java classes and we are going to simply call those methods from jsps .


JSP Directives

It is one of the important jsp element we have. JSP Directives basically commands to the JSP Engine. We will have complete details in future class.


Example for jsp elements:

<html><head><title>JSP Elements Example</title>
</head>
<body>
<%! int userCnt=0;%>  //declaration element

//scriptles
<%
    String name="Rajendra";
     userCnt++;
 %> //end of the scriptlet

<table>
 <tr><td>
  Welcome <%=name%> //here within <%=..%> is Expression element
     Your are user number<%=userCnt%>//Expression element
  </td></tr>
 </table>
</body>
</html>

/* output*/
Welcome Rajendra you are user number2

Explanation: here im the second person who has accessed web page it is going to display welcome Rajendra and you are user  number 2, lets say that some other guys comes in access this web page, is going to display: Welcome Rajendra you are user number 3. So for each request that comes it into this web page for everyone who login simple this web page and it is going to increase the userCont by one. By this time you would have triggered  at which place in this dynamic web page and it is use jsp elements and what type of jsp elements .

So we know that the variable userCnt is shared across multiple requests , in the same web page somebody else has to remember whats the previous count and it has to increase that count. So we know that using declaration elements we can share the variable across multiple requests . So im going to declare the userCnt variable  in the jsp declarations .
Next I have defined the value of Rajendra the variable called name, and this goes into the service() method, so for every new request this will be a new name, here simply I have defined Rajendra, but in real time, we need to fetch, whatever the user enter his username, that has to be displayed. And after each request increasing the userCnt by one.
And ultimately Iam goint to display the username and the userCnt. We know that we have to display value on to the screen we have to use JSP Expressions so I used jsp Expressions to display the name and the userCnt.


I hope you understood the jsp elements concepts.

No comments:

Post a Comment