Skip to main content

JSTL Tutorials Using NetBeans : Part-4

Create a new JSP file. Follow exactly the same way as we created the Jstl_Hello_World.jsp but name it Jstl_Core_Tags.jsp. Then we need to modify our index.jsp to contain the link to our newly created JSP. So please add below codes to our index.jsp just right after our first link to open the Hello World JSTL tags.
 
JSTL if tag and EL operators
 
What we do here is basically to send some parameters so that they are accessible from the target page.
 
On the target page, we can retrieve these parameters via ${param.parameter_name} and we are going to use the logical JSTL tags to compare some values. Add below codes to Jstl_Core_Tags.jsp
 
A test shows that parameter valid is ${param.valid}
 
This tag is used to determine whether parameter valid value is true or false. If the value is true then it prints out the sentence. On the other hand, if it is false, it should not print anthing out. You may like to try to modify the valid value to be false in the index.jsp. The sentence should not be printed out if the value is false. Until now, you should know that if you would like to access any parameters, you may use the keyword “param”. It is similar to the request.getParameter.
 
A test shows that parameter name contains value of ${param.name} via != operator
 
The above tag is to check whether parameter name is null or not. If it is not null then it prints out the sentence and it prints nothing if the value is null.
 
A test shows that parameter name contains value of ${param.name} via ne operator
 
Above codes are working similarly with the previous codes. We just want to test JSP notation “ne” for not equal function as what “!=” does.
 
A test shows that parameter name contains value of ${param.name} via empty operator
 
Above codes are another feature of JSP notation to check a value whether it is empty or not. It is basically pretty much the same as the second and third tag do. The difference lies on the value null and empty. What is the difference between empty and null? Well, empty is not necessarily be null but null always be empty. Null means that the variable is simply not existed while empty means that the variable is existed and initialized but it contains nothing. Please be careful when dealing with null values as it may cause you the famous NullPointerException.
 
A test shows that parameter mark contains value of ${param.mark} which lies in range of 0 to 10
 
Above codes are used to check whether parameter with the name of “mark” is within the range of 0 to 10. If yes then it prints the sentence.
Again, press Shift+F11 to build the project and F6 to run. The index.jsp will now looks like this
 
Image
 
Try to click on the second link (JSTL Core Tags) and it should look like below screenshot.
 
Image
 
You may be interested to try out some different parameters values for this JSTL tag. Remember, you will learn a lot by practice. Let’s move forward to more advance JSTL core tags.
 
Create another JSP page. This time, we can name it with Jstl_Core_Tags_2.jsp. We also need to create another link in index.jsp to open up this page as well. Add below codes to create the additional new link in index.jsp.
 
Play around with JSTL forEach, forTokens, choose, when, and otherwise
 
Now, we could see the new JSTL tag in index.jsp here. is normally used to embed a parameter values to the URL link. While, tag is basically used for creating a link of URL and stores the link in variable named “url”. In fact, this combination of and tags will do the same as the link that we hard coded before. The final link should be in form of Jstl_Core_Tags_2.jsp;jsessionid=…?param_1_name=param_1_value&param_2_name
=param_2_value. This is how we are creating a dynamic link using JSTL tag. After this, we can use to display it.
 
In this case, we have 2 parameters named test_for_each and test_for_tokens. Next, we need to write JSTL forEach and JSTL forTokens tags in Jstl_Core_Tags_2.jsp to see how they work.
 
 
Above codes will take values of the parameter “test_for_each” and stores it in variable called “part_each”. Within the tag forEach, we print the variable “part_each” using . It loops through default separator ‘,’ (comma) until it reaches the end. is extremely useful when you are sending some Collection object to the JSP and you need to iterate the content of the Collections to your JSP.
 
(small)
(medium)
(large)
 
Above codes are the sample of more advanced forEach loop. We first declare a variable “i” and we also define the beginning, ending and stepping value for the variable “i”. The above tag means that it should loop from 1 to 20 in increment of 2. To make it more interesting, we could add a condition to check whether the current value of variable “i” is in the range of small, medium or large by using JSTL tag , and . This one (, and ) is equivalent to switch case default syntax in conventional programming language.
 
 
This tag works in same way as forEach tag. But it has additional delims attribute to tokenize values with certain delimiters. What does it mean? It means that it will separate or tokenize the values within its items attribute with the delimiter. So if you have the String of “1,2,3” and the delimiter is “,”, the result would be three items i.e. 1, 2 and 3. How about if we want to separate the values for multiple delimiters? You can initialize it using multiple delimiters as delims=”;|()” to tokenize the values.
Let’s see how it works. Press Shift+F11 to build the project and F6 to run.
 
Image
 
And this is how the result looks like.
 
Image
 
Next, we are going to explain on and . These 2 tags are simple. simply redirects us to another page and is similar to that will extend imported page to current page.

Comments

Popular posts from this blog

Asynchronous Vs. Synchronous Communications

Synchronous (One thread):   1 thread -> |<---A---->||<----B---------->||<------C----->| Synchronous (multi-threaded):   thread A -> |<---A---->| \ thread B ------------> ->|<----B---------->| \ thread C ----------------------------------> ->|<------C----->|

WebSphere MQ Interview Questions

What is MQ and what does it do? Ans. MQ stands for MESSAGE QUEUEING. WebSphere MQ allows application programs to use message queuing to participate in message-driven processing. Application programs can communicate across different platforms by using the appropriate message queuing software products. What is Message driven process? Ans . When messages arrive on a queue, they can automatically start an application using triggering. If necessary, the applications can be stopped when the message (or messages) have been processed. What are advantages of the MQ? Ans. 1. Integration. 2. Asynchrony 3. Assured Delivery 4. Scalability. How does it support the Integration? Ans. Because the MQ is independent of the Operating System you use i.e. it may be Windows, Solaris,AIX.It is independent of the protocol (i.e. TCP/IP, LU6.2, SNA, NetBIOS, UDP).It is not required that both the sender and receiver should be running on the same platform What is Asynchrony? Ans. With messag...

Advantages & Disadvantages of Synchronous / Asynchronous Communications?

  Asynchronous Communication Advantages: Requests need not be targeted to specific server. Service need not be available when request is made. No blocking, so resources could be freed.  Could use connectionless protocol Disadvantages: Response times are unpredictable. Error handling usually more complex.  Usually requires connection-oriented protocol.  Harder to design apps Synchronous Communication Advantages: Easy to program Outcome is known immediately  Error recovery easier (usually)  Better real-time response (usually) Disadvantages: Service must be up and ready. Requestor blocks, held resources are “tied up”.  Usually requires connection-oriented protocol