[an error occurred while processing this directive]

JSP Simple Examples

                          

  1. Creating a String
    In jsp we create a string as we does in a java. In jsp we can declare it inside the declaration directive or a scriptlet directive.
                                               
  2. String Length
    In java, Strings are objects that belong to class java.lang.String. A string is a sequence of simple characters. We can get the length of the string by using the method length() of java.lang.String.
                     
  3. Declaring string array in java
    An array is the collection of same data type. Suppose if we have a declare an array of type String, then it will store only the String value not any other data type. When we have a closely related data of the same type and scope, it is better to declare it in an array.
                      
  4. Multidimensional array java
    A two dimensional array can be thought as a grid of rows and columns. The first array will reflect to a row and the second one is column.
      
  5. int array
    Array is a collection of same data type. Suppose if we have declared an array of type int then the array will take only the int values and not any other data types. We can find find out the length of  the variable by using the variable length.  
                                                
  6. JSP string array
    String array cannot hold numbers or vice- versa. Arrays can only store the type of data specified at the time of declaring the array variable.
             
  7. Custom exceptions
    Custom Exception
    inherits the properties from the Exception class. Whenever we have declare our own exceptions then we call it custom exceptions.
                
  8. Throwing an exception
    All methods use the throw statement to throw an exception.  The throw statement requires a single argument a throwable object. Here is an example of a throw statement. 
       
  9. Arrayindexoutofboundsexception
    ArrayIndexOutOfBoundException
    is thrown when we have to indicate that an array has been accessed with an illegal index.  
         
  10. printStackTrace in jsp
    printStackTrace
    is a method of the Throwable class. By using this method we can get more information about the error process if we print a stack trace from the exception.
                    
  11. Runtime Errors
    Errors are arised when there is any logic problem with the logic of the program. 
         
  12. Try catch in jsp
    In try block we write those code which can throw exception while code execution and when the exception is thrown it is caught inside the catch block. 
       
  13. Multiple try catch
    We can have more than one try/catch block. The most specific exception which can be thrown is written on the top in the catch block following by the less specific least. 
                       
  14. Nested try catch
    We can declare multiple try blocks inside the try block. The most specific exception which can be thrown is written on the top in the catch block following by the less specific least. 
               
  15. kilometers per liter to miles per gallon
    Kilometers per liter
    : The distance traveled by a vehicle which is running on gasoline or diesel fuel in a kilometer. 
    Miles per Gallon: The distance traveled by a vehicle which is running on gasoline or diesel fuel in a mile.
                     
  16. Ascii values table
    ASCII
    stands for American Standard Code for Information Interchange. Computers can only understand numbers, so an ASCII code is the numerical representation of a character such as @, #, $, and so on. ASCII was developed when non- printing characters were rarely used. 
                                            
  17. life cycle of a jsp page
    Life of the the jsp page is just same as the servlet life cycle. After get translated the jsp file is just like a servlet. 
                              
  18. Page directive attributes
    A directive is a way to give special instructions to the container at page translation time. The page directive is written on the top of the jsp page.
                       
  19. Html tags in jsp
    In this example we have used the html tag inside the println() method of the out implicit object which is used to write the content on the browser. 
                                                
  20. Password Controls
    In this program we are going to know how the server determines whether the password entered by the user is correct or not. This whole process is controlled on the server side.
                      
  21. Multiple forms in jsp
    The form tag creates a form for user input. A form can contain checkboxes, textfields, radio- buttons and many more. Forms are used to pass user- data to a specified URL which is specified in the action attribute of the form tag.
                     
  22. Interface in jsp
    In interface none of its methods are implemented. All the methods are abstract. There is no code associated with an interface. In an interface all the instance methods are public and abstract. Interfaces are always implemented in the class. They add extra behaviors to the class.
  23. Inheritance in java with example
    Inheritance is one of the concept of the Object- Oriented programming. It allows you to define a general class, and later more specialized classes by simply adding some new details. 
                      
  24. Constructor inheritance
    Constructors are used to create objects from the class. Constructor declaration are just like method declaration, except that they do not have any return type and they use the name of the class. The compiler provides us with a default constructor to the class having no arguments.
  25. Abstract classes
    We does not make a object of the abstract class. This class must be inherited. Unlike interface the abstract class may implement some of the methods defined in the class, but in this class at least one method should be abstract. 
                        
  26. Using Super class Variables With Sub-classed Objects
    One of the strong features of java is that it is follows a OOPs concept, and one of the feature of OOP in java is that, we can assign a subclass object or variable to the variable of the  superclass type.
                                     
  27. Log files
    Log files keeps a records of internet protocol addresses (IP), Http status, date, time, bytes sent, bytes recieved, number of clicks etc.
               
                                         
  28. Calculate a factorial by using while loop
    In this example we are going to find out the factorial of 12 by using the while loop. In while loop the loop will run until the condition we have given gets true.
     
  29. Calculating factorial
    After going through this example you will be understand how you can calculate the factorial by using recursion in jsp. To make a program on factorial, firstly it must be clear what is recursion.
                       
  30. Celsius Fahrenheit
    Celsius is a unit to measure temperature scale on which water freezes at 0 degree and boiling point is 100 degree. This unit is discovered by Celsius in 1742, a Swedish astronomer and physicist, he has invented the centigrade, or Celsius thermometer divided between the freezing and boiling points of water into 100 parts.
               
  31. comment in jsp
    In a jsp we should always try to use jsp- style comments unless you want the comments to appear in the HTML. Jsp comments are converted by the jsp engine into java comments in the source code of the servlet that implements the Jsp page.
                     
  32. Html tag inside out implicit object
    In this program we are going to use a html tag inside a out object. out object is used to display the content on the browser. To make this program run use out object inside which define some html code along with the content you want to display on the browser
             
  33. Jsp methods
    In this example  we are going to show you how we can declare a method and how we can used it. In this example we are making a method named as addNum(int i, int b) which will take two numbers as its parameters and return integer value.
                     
  34. Multiple methods
    Jsp is used mainly for presentation logic. In the jsp we can declare methods just like as we declare methods in java classes. Methods can be declared in either declaration directive or we can declare it in scriptlet. If we declare method inside declaration directive, then the method is applicable in the whole page.
                        
  35. Passing Array method
    Array is a collection of similar data type. It is one of  the simplest data structures. Arrays holds equally sized data elements generally of the similar data type.
            
  36. Two index 
    In this example we will show how we can use two indexes in a for loop. We can solve this problem by using for loop defined inside the scriptlet directive.
                   
  37. Date in JSP
    To print a Date in JSP firstly we are importing a class named java.util.Date of the package java.util. This package is imported in the jsp page so that the Date class and its properties can accessed in the JSP page. 
            
  38. If- Else Ladder
    A ladder means a vertical set of steps. It is a computer generated list of pairings used in eliminations.
               
  39. Nested If 
    We use the if condition to check if the particular condition is true then it should perform a certain task, and if a particular condition is not true then it should do some other tasks. 
                        
  40. Quintessential JSP 
    Quintessential means representing the perfect example of a class or quality. It is pure and concentrated essence of a substance.
                        
  41. Include File JSP using directive and include action
    <%@ include file = " "%>: - This is include directive. This directive has only one attribute named as file, which is used to include a file in the jsp page at the translation time. 
    <jsp:include page = " ">:- This is known as the include standard action. This standard action is used to include a file at run time. This standard action is evaluated at the run time.
                                               
  42. Snoop in JSP
    It mostly contains the request information, ServletContext initialization parameters, ServetContext attributes, request headers, response headers etc.
               
  43. sendRedirect In JSP
    sendRedirect() method is a method of HttpServletResponse interface. In sendRedirect() the object of request will be generated again with the location of page which will perform the request of the client.
            
  44. Request Header in JSP
    Whenever an http client sends a request, it sends the request in the form of get or post method or any other HttpRequest  methhods. It can also sends the headers with it.
                                        
  45. Specific request headers in JSP
    Whenever an http client sends a request, it can also sends the headers with it. All the headers are optional except Content-length, which is required only for POST request.
                           
  46. Java class in JSP
    To use the class inside the jsp page we need to create an object of the class by using the new operator. At last use the instance of the class to access the methods of the java file.
                        
  47. Setting Colors in JSP
    In Jsp also we can set the background color which we want, the font color can be changed. The table can be coloured . By using the colors code we can give colors according to our wish. 
           
  48. Sine Table in JSP
    Mathematically, the sine of an angle is the ratio of the length of the opposite side to the length of the hypotenuse of an imaginary right triangle having that angle in it.
                              
  49. Applet In Jsp
    Applets are small programs or applications written in java. These applets are those small programs that runs on web browsers, usually written in java. We can use the applets in jee also. In jee it runs on the context of web application on a client computer.  
            
  50. Creating a Local Variable in JSP
    Consider a situation where we have to declare a variable as local, then we should declare the methods and variables in tag except the declaration directive.
              
  51. Method in Declaration Tag
    Anything which will be declared inside declaration tag will be applicable within the whole application. We call this tag a Declaration Tag. The syntax of this tag is <%!  --------- %>. 
              
  52. Forward a JSP Page
    The request object  goes to the controller then the controller decides by which jsp or servlet this request will be processed, then the request object is passed to that jsp or servlet and the output is displayed to the browser by the response object.
                        
  53. Random in JSP
    Random numbers are the numbers that are determined entirely by chance. User does not have any control over the working of random numbers. random() is a method of Math class which extends java.lang package.
                       
  54. JSP include directive
    By using the include tag the file will be included in the jsp page at the translation time. In this example we have created a jsp file which has to be included in the other jsp file by using the tag <%@ include file = " "%>.
                     
  55. Literals in JSP
    Literals are the values, such as a number or a text string, that are written literally as part of a program code. A literal is a expression of a value, including a number or a text string.
                          
  56. Passing Parameter using <jsp: param>
    Request parameters can be passed by using <jsp: param>. This tag contains two attributes: 1) name 2) value.  
                       
  57. Tag Handler
    Custom tags are usually distributed in the form of a tag library, which defines a set of related custom tags and contains the objects that implement the tags. Custom tag libraries allow the java programmer to write code that provides data access and other services, and they make those features available to the jsp author in a simple to use XML- like fashion.
                               
  58. UseBean
    Syntax:  <jsp:useBean id= "nameOfInstance" scope= "page | request | session | application" class= "package.class" type= "package.class > </jsp:useBean>.
                    
  59. Expression Language In JSP
    EL means the expression language , it is a simple language for accessing data, it makes it possible to easily access application data stored in JavaBeans components. 
                         
  60. EL Basic Arithmetic
    Addition (+), subtraction (-), multiplication (*), division (/ or div), and modulus (% or mod) are all supported in Expression Language. Error conditions, like division by zero are handled easily by the expression language.
               
  61. EL Basic Comparisons
    The Expression Language has the following Basic Comaparisions: 
    < >   <=   >=       lt   gt le  ge
    = =    !=     eq  ne
    &&   and
    ||    or
                    
  62. EL Implicit Objects
    EL is the JSP 2.0 Expression Language Interpreter from Apache Corporation. EL means the expression language , it makes it possible to easily access application data stored in JavaBeans components. 
                       
  63. Implementing Conditional Content on a JSP Page
    We make use of the condition to check if the entered value is correct. It correct then what will be the output, and in case if it is wrong then what output will be displayed to the browser.
            
  64. Exception Handling in JSP
    An exception can occur if you trying to connect to a database which doesn't exists or the database server is down, it may be thrown if you are requesting for a file which is unavailable, then the exception will be thrown to you. 
                 
  65. Generate Error In JSP
    In software engineering, the term refers to an incorrect action or calculation performed by software. In general we can say that the error results from a combination of a defect and a fault.
                     
  66. Using Operators In JSP
    In mathematics, an operator is a function, usually of a special kind depending on the topic. An operator is a function that acts on functions to produce other functions. A operator symbol or operator name is a notation that denotes a particular operator. 
                          
  67. Precompiling a JSP Page
    Some jsp containers support the capability of precompiling a JSP page. To precompile a jsp page, access the page with a query string of ?jsp_precompile. This we will write in the URL.
                          
  68. JSP Plugin
    Syntax: <jsp: plugin type = "bean |applet" code = "ClassFileName" codeBase = "ClassFileDirectoryName">
                    
  69. Using Protected Access in JSP
    In java there are three types of access specifiers: public, protected, private. We always declare access specifiers to be more accessible.
                           
  70. Conditional Forward using EL
    We can forward one page to another jsp page, html page or any other servlet page. A condition is checked before forwarding the page to
    another page. 
                       
  71. Request Parameters In EL
    In EL we use "param" attribute to read the parameters from the file. If we have multiple values to read from the same field then we will use "paramValues".
                             
  72. Request Headers In EL
    Whenever an http client sends a request, it also sends the headers with it. All the headers are optional except Content-length, which is required only for POST requests.
                          
  73. Break Statement in JSP
    The use of break statement is to escape early from the loop. It allows the program to escape from the for, while, switch and do while loops. A break may only appear on one of these statements
                          
  74. Finding Average in JSP
    In mathematics, an average is a measure of the middle of the data set. The average value is the total amount divided by the total number of elements.
                          
  75. Midpoint of two number
    While solving any mathematical problem it becomes important to find the number that is exactly between two numbers. 
                              
  76. While Loop in JSP
    The while loop is a control flow statement, which allows code to be executed repeatedly based on the given boolean condition. In while loop the condition is firstly evaluated, if it finds that the condition is true, then the code will be executed. 
                                
  77. Using Beans And Page Scope
    The scope in which the Bean exists and the variable named in id is available. The  default value of scope is page. In this bean object is placed in the PageContext object and the object can be accessed within the page only.
                                
  78. Using Beans And Request Scope
    request scope:
    In this scope bean object is placed in the ServletRequest object for the current request only.
                              
  79. Using Beans And Session Scope
    session scope:
    In this the bean will get stored in the HttpSession object associated with the current session.
                                
  80. Using Beans And Application Scope
    application scope:
    In this the bean will get stored in the ServletContextServletContext is shared by all servlets in the same web application.
                               
  81. EL and Complex Java Beans
    Java Beans: They are platform- independent component and usable software programs which you can use develop and assemble easily to create complex applications. JavaBean are also known as beans.
                                     
  82. EL and  Complex Java Beans1
    EL
    means the expression language , EL provides us a way to access the java code. EL is such a language which is liked by java programmers as well as by those who are not programmers like designers.
                 
  83. Use of dot(.) operator in EL
    We use dot(.) operator within EL to access properties of bean and map values. For example in the EL ${useOfDotOperator.name}, the left hand side variable will be a map value or a bean. The thing to the right of the dot operator must be a Map key or a bean property
            
  84. Using [ ] operator of EL with an Array
    An array is a type of container which can hold a fixed number of values of a single type. Array is a collection of similar data types. It means if you have declared an array as string then it can only store the string values. 
              
  85. Using [ ] operator of EL with the ArrayList
    ArrayList is resizable array implementation of the List interface. As the elements are increased in an ArrayList, its capacity also grows automatically. It can store all type of elements including null.
             
  86. Using [ ] operator of EL with the Map
    Map
    stores the values in the form of a key- value pair. Map can't have a duplicate keys, and each key is associated with at least one value. In map we can retrieve the value by using ${map["0"]}.
                         
  87. How to Use Java Functions In EL
    EL to invoke a function:
    We can call the function by using ${prefix:name()}. This name() we have defined in the <name> tag inside the <function> tag.
                    
  88. Catch an Exception using EL
    In this example we are going to catch an exception by using the EL. We can't prevent errors, but of course we can give the user a friendlier error response page. Instead of giving any error codes we should try to show some friendly messages. 
                                  
  89. Convert string to Date in JSP
    To parse a string to a date we use a class named SimpleDateFormat. In the constructor of this class we define the date format we required and ask the user to enter the date in the same format. 
                                               
  90. <c:forEach> in JSTL
     <c:forEach> tag is a simple way to iterate over arrays and collections. This tag is very much similar to for loop. The tag repeats the body of the tag for each element in the array or collection.
                                           
  91. Nested <c:forEach> in JSTL
    <c:forEach>
    tag is a simple way to iterate over arrays and collections. This tag is very much similar to for loop. The tag repeats the body of the tag for each element in the array or collection. The nested <c:forEach> is same as loop inside the loop. This tag can be used for more complex loops.
                                           
  92. JSTL: another for each and status
    In this program we have used the directive to set the attributes of a bean. This attribute is retrieved by the item attribute of the <c: forEach> tag. The LoopTagStatus class has a count property which gives us the current value of the iteration counter.
                                    
  93. JSTL: forEach and status
    It is not a good programming practice to use directive to set the attribute in a bean or a map when we using the jstl. We can do it by using the <c:set> tag of jstl. In this program we are going to use the tag <c: set>.
                                    
  94. JSTL for Loop <c: forEach>
    In the tag <c:forEach var="i" begin="1" end="20" step="1" varStatus ="status">. Here this tag is working exactly as for loop works in a jsp or in java. i is the name of the variable, which begins from 1 and continues upto 20.
                            
  95. Conditional Include with <c:if>
    Imagine if we want to check that the value entered by the user is fulfilling the condition or not, then in these condition the <c:if> tag is used. This tag will not work if the value entered by the user doesn't match any of the condition given in the program.
                          
  96. <c:If> with No Body
    The <c:if> tag is not for applicable if we want to do one thing if the condition is true, and another thing if the condition is false. In this situation this tag will become obsolete.
                      
  97. JSTL If- Else
    The problem with <c:if> is that, this tag will not work if the value entered by the user doesn't match any of the condition given in the program. In this tag there is no way to specify the default value. Here comes the use of  the <c:choose> tag.
                  
  98. Comparing Two String with <c:if>
    In this program we are making use of <c:if>, in which we are going to check whether the given string is equal to another string, the case of the string will be ignored.
                      
  99. <c:forTokens>
    <c:forTokens items="${string}" delims="!," var="tokenName" varStatus="status">.
    This tag is used is for making tokens a string. It iterates over tokens generated, which is separated by the supplied delimiters.
  100. <c: out> in JSTL
    In this example we have used only one attribute i.e. value. It is a must attribute in the tag. We are just calculating the multiplication of two numbers by using the <c: out> core action tag.
                           
  101. <c: out> attribute default
    default:
    We can use this attribute if the resulting value is null. In this example we are just printing the value of the default attribute on the browser.
               
  102. <c: out> attribute escapeXml
    escapeXml
    attribute takes a boolean value and is used to check whether there is any need to convert the &, <, > etc to their character encoding codes.
                 
  103. <c: out> For Simple Calculation and Output
    In this example we have used only one attribute of <c: out>. The attribute value which is a must attribute and by using this attribute we are calculating the sum of numbers and displaying the output on the browser. 
                 
  104. Listing HTTP Headers
    In this example we are going to list the headers of the HttpServletRequest object. We are using the <c: forEach> tag. The items in the header get stored in the attribute var of the jstl core library. Now the value will be displayed to the user by using the core library tag <c: out>.
                 
  105. <c: url> in JSTL
    By using the <c: url> we are doing the URL rewriting but by using the <c: param> core action tag inside the <c: url> our requirement for URL rewriting and URL encoding get fulfilled. 
                                     
  106. Creating URL using <c:url>
    In this example we are going to use one html page from which we are going to enter the parameters which we want to append to the URL. The second page will be a jsp page in which we are going to retrieve the value passed in the html page and in the third we are going to displaying those values.
                               
  107. JSTL <c:import>
    The disadvantage of using the <jsp:include> or the include directive is that we can only include the content or files which are the part of the current web application. But by using the jstl <c:import> we can also include those contents or files which are not a part of the current web application but lying somewhere outside the web application.
                                                     
  108. JSTL <c:import> with <c:param>
    In the program we have also used the <c:param> core action tag which is a child of the <c:import>. The <c:param> takes two attributes, one is name and the second one  is the value. The tag <c:param> is used to customize the thing which we want to include. 
         
  109. Throwing an Exception in JSTL
     In this program we are going to show you what will happen if we try to divide any number by 0. After going through this example you can better understand how the exception can be thrown in JSTL.                      
                 
  110. JSTL <c: catch>
    If we have any doubt that the particular code can invoke a error then we should write those codes inside the <c:catch> core action tag. The beauty of this tag is that it works both like a try and catch.
                      
  111. Catching Exception using c: catch
    If you have written some code which you think that it can invoke a error, and still you are confident that you can recover from that solution by yourself. Then then is one tag provided to you that is <c:catch>.
                       
  112. Catching Exception using c: catch and c:set
    In this tag we are defining the attribute of the <c:catch> core action tag that is var. This attribute is optional and is used when we want to access the exception after the end of the <c:catch> tag. 
                         
  113. JSTL c:catch with c:if
    By performing simple mathematical calculation we are going to teach how can we handle the exception by using the <c: if> tag. 
                              
  114. JSTL : Submit Form TextField
    In this example we are going to see how we can retrieve multiple values from the form. 
                                         
  115. JSTL: Set Session Attribute
    Consider a situation where we are using the jstl and there is a need to set a variable in the session. You all know that it can be done very easily in the servlets and jsp, but what about jstl. Don't worry it can be done very easily in the jstl as compared to servlets and jsp.
                             
  116. JSTL: Removing Session Attribute
    In this example we are going to remove the session attribute which we have set in the session. For this we are going to use the core action library, so we need to use the jstl core library provided by the Sun MicroSystems in our example. 
                         
  117. JSTL: Property Access 
    In this example we are going to show we can access the property by using  the dot(.) and [ ] operator. 
                                                    
  118. JSTL: String Collection
    In this example we are going to set some movies in the attribute var. The var attribute in <c:set> is used for setting attributes variables.  It works like <jsp:setProperty> tag but by using <jsp:setProperty> we can only set the property of a bean.
                                        
  119. JSTL: Form Action Text Field
    In this example we are going to retrieve the value we have entered in the jsp form. 
                      
  120. Using JSTL For Finding Square
    In this program we are going to find out the square of values from 1 to 10. But the approach we are taking to finding out the square is the JSTL. 
                  

Back to JSP Examples.

                          

[an error occurred while processing this directive]