JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

JSTL Core c:forToken Tag

February 20, 2014 by Krishna Srinivasan Leave a Comment

The <c:forToken> tag is used to break a string into tokens and iterate through each of the tokens. This tag is also used for looping over tokenized elements of a string.

Syntax For <c:forToken> Tag

[code lang=”html”]
<c:forTokens attribute> body content </c:forTokens>
[/code]

Attributes of <c:forToken> Tag

  • items: This attribute specifies the string to be tokenized. This is the set of data values.
  • var: This attribute specifies the name of the exported scoped variable for the current item of the iteration.
  • delims: This attribute specifies set of delimiters.  Delimiters separate the tokens in the string)
  • begin: This specifies the start index of the iteration. Iteration starts at this index. This is optional attribute.
  • end: This specifies end index of the iteration. Iteration starts at this index. This is also optional attribute.
  • step: Iteration will only process every step tokens of the string, starting with the first one.

Example of <c:forToken> Tag

[code lang=”html”]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<p>Example for forToken in Core Tag Library.</p>
<c:forTokens var="token" items="a,b,c,d" delims=",&">
<c:out value="${token}"/>
</c:forTokens>
[/code]

Steps for Execution

  • Save this file as example.jsp in your eclipse IDE.
  • Now select this jsp file, right mouse click and select Run as ->Run on server

Output

When the execution process is completed successfully we will get the following output :

JSTL Core cforToken tag_demo
In the above example the item attribute is used define the tokens. it will iterate over tokens separated by delimiter. In the above example we use the “, & ” delimiters to tokenize the string.

Previous Tutorial : JSTL Core c:forEach Tag  :: Next Tutorial : JSTL Core c:param Tag

Filed Under: Java EE Tagged With: JSTL Tutorials

JSTL Core c:forEach Tag

February 20, 2014 by Krishna Srinivasan Leave a Comment

The <c:forEach> tag is used to iterate over a collection of data such as arrays. A common use of this tag is to produce a HTML table containing data gathered from SQL query. Using this tag we can break the input data into multiple parts based on the delimiter. This is similar as for loop in java.

The syntax of <c:forEach> Tag

[code lang=”html”]
<c:forEach attribute> body content </c:forEach>
[/code]

The attributes of <c:forEach> Tag

  • items: This specifies the collection of items to iterate in the loop. The collection can be in the form of array list, maps …etc.
  • begin: This specifies the start index of the iteration. Iteration starts at this value. This is optional attribute.
  • end: This specifies end index of the iteration. Iteration ends when index reaches this value. This is also optional attribute.
  • step: This specifies an optional increment for the loop, the default value is 1.
  • varStatus: This specifies name of the scoped variable which holds the loop status of the current iteration.

Example of <c:forEach> Tag

[code lang=”html”]</pre>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<%@ page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>JSTL Example</title>
</head>
<body>
<h3>JavaBeat Tutorial List.</h3><br>
<%
List list = new ArrayList();
list.add("Java");
list.add("Spring");
list.add("Hibernate");
list.add("JSP");
list.add("Servlet");
list.add("JSTL");
list.add("Flex");
list.add("HTML5");
list.add("CSS3");
request.setAttribute("list", list);
%>
Tutorials <br>
<c:forEach var="i" items="${list}"><br>
${i}
</c:forEach>
</body>
</html>
<pre>
[/code]

Steps for Execution

  • Save this file as example.jsp in your eclipse IDE.
  • Now select this jsp file, right mouse click and select Run as ->Run on server

Output

When the execution process is completed successfully we will get the following output :
JSTL Core cforEach tag_demo

Previous Tutorial : JSTL Core c:remove Tag :: Next Tutorial : JSTL Core c:forToken Tag

Filed Under: Java EE Tagged With: JSTL Tutorials

JSTL Core c:remove Tag

February 20, 2014 by Krishna Srinivasan Leave a Comment

The <c:remove> tag is used to remove the variable from the specified scope. If we want to remove the variable from the scope we need to specify the scope if we not specify the scope then it will remove the variables from all scopes.

Syntax For <c:remove> Tag

[code lang=”html”]
<c:remove var=”string” scope=”string”/>
[/code]

Attributes of <c:remove> Tag

  • var: This attribute specify the name of the variable which is to be removed.
  • scope: This attribute specify scope of the variable where the variable can be removed.

Example of <c:remove> Tag

[code lang=”html”]</pre>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>JSTL Example</title>
</head>
<body>
<h3>JSTL Core Tag remove Example.</h3>
<c:set var="str" value="JavaBeat.net"/>
<h4>Values assigned for the string is </h4>
<b>Value of str : </b><c:out value= "${str}" /><br>
<!– Removing the value associated to variable str1 –>
<c:remove var="str"/>
<h4>After Removing the values from the string </h4>
<b>Value of str is null: </b><c:out value="${str}" /><br>
</body>

</html>
[/code]

Steps for Execution

  • Save this file as example.jsp in your eclipse IDE.
  • Now select this jsp file, right mouse click and select Run as ->Run on server

Output

When the execution process is completed successfully we will get the following output :
JSTL core c remove tag_demo

Previous Tutorial : JSTL Core c:import  :: Next Tutorial : JSTL Core c:forEach Tag

Filed Under: Java EE Tagged With: JSTL Tutorials

EclipseLink – javax.persistence.PersistenceException : No Persistence provider for EntityManager

February 19, 2014 by Amr Mohammed Leave a Comment

This post provides possible exception while doing the JPA programming and the suggested solution for resolving the exception. This post will solve the issue that can be happened while the JPA implementation trying to get a persistence provider. The exception explained in this post thrown when you have not configured properly the persistence unit name in the persistence.xml.

Look at the following example:

[code lang=”java”]
EntityManagerFactory factory = Persistence.createEntityManagerFactory("EclipseLink-JPA-Installation");
<span style="font-size: 12px; line-height: 18px;">[/code]

When we are coming to execute an application contains the above snippet of code, we might got the following persistence exception:

[code lang=”java”]
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named EclipseLink-JPA-Installation
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at net.javabeat.eclipselink.JPAImpl.main(JPAImpl.java:12)
<span style="font-size: 12px; line-height: 18px;">[/code]

Such that exception notify you for reviewing the persistence.xml file that located under META-INF which in turn located under your source code. Check your persistence-unit name, you almost probably have a wrong name of the persistence unit or you have no persistence at all.

If you have a persistence unit in the persistence.xml, you have make sure that the persistence unit name is identical to the name that is passed into createEntityManagerFactory method. But if you have no persistence unit at all, you have to create it.

Note : The exception solution is only the possible, it is common that same exception could be thrown for multiple reasons which is not explained in this tutorial. If you come across any such scenario, please write it in the comments section.

To know more about how to create EclipseLink and JPA project, please read our previous tutorial eclipselink jpa installation and configuration.

 

Filed Under: Java EE Tagged With: EclipseLink, JPA

EclipseLink – JPA Fields Annotations – @Basic and @Transient

February 19, 2014 by Amr Mohammed Leave a Comment

As mentioned earlier, EclipseLink – JPA is a persistence service, provides a mechanism to communicate with a different database vendors. That type of communication has been achieved through what has become known as annotation-based configuration, which is considered as an alternative way for doing a configuration rather than the XML. The XML-based configuration remains one of the ultimate ways that can be used to configure the platforms for a long period of time prior to annotations come into picture.

  • Some annotations are used within the classes declaration, while others used with the fields. Also set of annotations have been used by the lifecycle of the JPA.
  • Even though we have the ability to use the annotations within a Java Standard Edition, but also the Java Enterprise Edition has its own annotations.Even if we are coming into accessibility, the annotations is there.
  • A huge amount of annotations are introduced with JPA. Annotations that would be important when we are going to implement a relationship, a mapping or a JPQL.

However, getting started into annotations requires more than understanding the way of the use. But it is important to understand concept like “persistence domain“, that would help us understanding how the JPA implementation works?

Persistence Domain defined as, it is the scope of the classes that can be persisted, which controlled by the JPA implementation. And the persistence, is the process of storing the states of the objects model into permanent storage such as a database system.

The only way that makes your classes seen by the JPA implementation is through mention them into persistence.xml file, using either <class> or <jar-file> tags.

Each referenced classes in the persistence.xml file will form what we called it a “persistence domain“.So the mapping between your different scopes (i.e. objects and the database system) should started from this point as we’ll see in the next coming few lines. And that’s the meaning of Object-Relational Mapping (ORM).

This article will introduce some of the annotations that have been used with the classes’ fields to evaluate the mapping. However, the remaining annotations will be covered into other articles.

JPA Annotations for Fields

The below are the list of annotations defined in JPA for used in the field level. Here we discuss only @Basic and @Transient which are most important.

  • javax.persistence.Id(@Id)
  • javax.persistence.Basic (@Basic) (Should be covered at this article)
  • javax.persistence.Embedded (@Embedded)
  • javax.persistence.ElementCollection (@ElementCollection)
  • javax.persistence.EmbeddedId(@EmbeddedId)
  • javax.persistence.Version(@Version)
  • javax.persistence.Transient(@Transient) (Should be covered at this article)

@Basic Annotation

  • Target: fields (Including property get methods)
  • Uses: @Basic
  • Arguments: fetch and optional. The default value of the fetch is EAGER and it represents the strategy for fetching data from the database. The second value that can be applied is LAZY. The EAGER strategy is a requirement on the persistence provider runtime that data should be eagerly fetched. meanwhile the LAZY strategy is a hint for the persistence runtime that the data should be fetched lazily, when it’s first accessed. The persistence implementation is permitted to eagerly fetch those properties marked as LAZY fetch. The optional is a hint about whether the value of the field or property may be null. This is a hint and is disregarded for primitive types. It is almost used once you would generate the database schema through the persistence implementation itself.

Basic annotation is the simplest type of mapping to a database column, as you have noticed, the Basic annotation can be applied to a persistent property or instance variable. instance variable means that you have ability to use @Basic on the get property of that instance variable.

By default, each properties or instance variables defined within a persistent classes would have an @Basic annotation in case their type is one of the following:

  • Java primitive types
  • Wrappers of the primitive types
  • java.lang.String
  • java.lang.BigInteger
  • java.math.BigDecimal
  • java.util.Date
  • java.util.Calendar
  • java.sql.Date
  • java.sql.Time
  • java.sql.Timestamp
  • byte [], Byte [] , char [] , Character [] , enums and any other type that implements Serializable.

The following examples shows you a simple example of persistence domain and persistence class.By reading the persistence.xml, the JPA implementation would generate the persistence domain. Inside the created domain every properties are mapped implicitly into identical column by using the name of the property with minimal change; it is converted into Uppercase.

[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="EclipseLink-JPA-Installation" transaction-type="RESOURCE_LOCAL">
<class>net.javabeat.eclipselink.data.Employee</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/JavaBeat"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name="eclipselink.logging.level" value="FINEST"/>
</properties>
</persistence-unit>
</persistence>

[/code]

Employee.java

[code lang=”java”]
package net.javabeat.eclipselink.data;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;

@Entity
public class Employee {

@Id
private int employeeId;
@Basic(optional=false)
private String employeeName;

public int getEmployeeId() {
return employeeId;
}

public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}

public String getEmployeeName() {
return employeeName;
}

public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}

public String toString(){
return this.employeeId+" "+this.employeeName;
}

}

[/code]

If you have deleted the Employee class from the persistence.xml, you have got the following error message at the top of the Employee class:

[code lang=”java”]Class "net.javabeat.eclipselink.data.Employee" is managed, but is not listed in the persistence.xml file[/code]

Meawhile, if you have deleted the @Entity annotation, you have got the following exception at the top of the Employee class:

[code lang=”java”]Class "net.javabeat.eclipselink.data.Employee" is listed in the persistence.xml file, but is not annotated[/code]

@Transient Annotation

  • Target: fields (Including property get methods)
  • Uses: @Transient
  • Arguments:no argument was specified.

The Transient annotation is working as opposite for the default implementation of JPA. As mentioned above, The JPA do a default mapping for the properties and instance variables that are located into a persistent class with their columns by using the uppercase of the properties’ name. While the Transient annotation has used with those properties and instance variables that the JPA ordered to ignore them.

Look at the Employee class, if you’ve added a property that haven’t an identical column at the database, you are almost probably getting an exception says:

[code lang=”java”]Unknown column ‘EMPLOYEESALARY’ in ‘field list'[/code]

Even of your code doesn’t display an error at compile time, but it surely will throw a runtime exception if you have tried doing a query for your employees. That exception will tell us about missing column.

[code lang=”java”]
package net.javabeat.eclipselink.data;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;

@Entity
public class Employee {

@Id
private int employeeId;

@Basic(optional=false)
private String employeeName;

private String employeeSalary; // failure possibility

public int getEmployeeId() {
return employeeId;
}

public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}

public String getEmployeeName() {
return employeeName;
}

public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}

public String getEmployeeSalary(){
return this.employeeSalary;
}

public void setEmployeeSalary(String employeeSalary){
this.employeeSalary = employeeSalary;
}

public String toString(){
return this.employeeId+" "+this.employeeName;
}

}
[/code]

To avoid such that exception, the properties being ignored could be marked as @Transient. The implementation of JPA will discard them.

[code lang=”java”]
@Transient
private String employeeSalary;
[/code]

Filed Under: Java EE Tagged With: EclipseLink

JSTL Core c:import Tag

February 18, 2014 by Krishna Srinivasan Leave a Comment

The <c:import> tag is used to include the content of another resource in the current JSP. The resource can be either static or dynamic. The <c:import> tag works like the but it is more flexible and powerful.

The syntax of <c:import> tag is as follows

[code lang=”html”]
<c: import attributes> </c:import>
[/code]

The attributes of <c:import> Tag

  • url: This is the major attribute in the tag and its mandatory attribute. This attribute is used to retrieve and import into the page. This specifies URL of the source to include.
  • var: This is optional attribute. This is used to specifies the name of the variable into which the result has to be stored, if specifies. If this is not specifies then the imported data will be printed on the current page.
  • scope: This is also optional attribute. This attribute is used to specifies the scope into which the variable has to be stored. If we are using var attribute then only we can use the scope attribute.
  • context: This attribute is used to specify the context name in which the page has to be located.

Example of <c:import> Tag

Listing 1:example.jsp

[code lang=”html”]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<h3>test.html</h3>
<div style="border: 10px solid red;">
<c:import var="testHtml" url="/test.html" />
<c:out value="${testHtml}" escapeXml="false" />
</div>
<h3>test.html source code</h3>
<c:import var="testHtmlSource" url="/test.html" />
<c:out value="${testHtmlSource}" escapeXml="True" />
[/code]

Html files for the above program is as follows:

Listing 2:test.html

[code lang=”html”]</pre>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test Page</title>
</head>
<body>
This is the Test page
</body>
</html>
[/code]

How To Run?

  • Save this file as example.jsp in your eclipse IDE.
  • Now select this jsp file, right mouse click and select Run as ->Run on server

Output

When the execution process is completed successfully we get the following output:
JSTL_c import tag_demo

Previous Tutorial : JSTL Core c:choose,  c:when, c:otherwise Tags :: Next Tutorial : JSTL Core c:remove Tag

Filed Under: Java EE Tagged With: JSTL Tutorials

JSTL Core c:choose, c:when, c:otherwise Tags

February 18, 2014 by Krishna Srinivasan Leave a Comment

JSTL Core <c:choose> Tag

The <c:choose> tag of JSP core tag library is used for conditional execution of statement. The choose tag is used to construct an <c:if> statement. The <c:choose> tag acts like a java switch statement. The <c:choose> tag executes the conditional block statements which is embedded with the sub-tags <c:when> and <c:otherwise>.

The syntax of <c:choose> Tag

[code lang=”html”]
<c:choose> body content </c:choose>
[/code]

The <c:choose> tag has no attribute.

JSTL Core <c:when> Tag.

It is a subtag of <c:choose> tag .<c:when> tag is like the block of if control statements which executes when the condition is true. The <c:when> tag encloses a single case within the <c:choose> tag.

The syntax of <c:when> Tag

[code lang=”html”]
<c:when attribute> body content </c:when>
[/code]

Attribute used in <c:when> Tag

There is only one attribute used in the <c:when> tag that is test. test attribute is used to provide conditional statement for evaluation.

JSTL Core <c:otherwise> Tag

<c:otherwise> tag is similar to default statement which works when all the statements of <c:otherwise> holds false. This tag is like else of if control statement of java program.This is also a subtag of <choose> tag .
The <c:otherwise> tag is evaluated if <c:when> or nested <c:when> tag attribute test condition is not evaluated to true.

syntax of <c:otherwise> Tag

[code lang=”html”]
<c:otherwise> body content </c:otherwise>
[/code]

The body content of the <c:otherwise> tag is evaluated if none of the <c:when> conditions in the <c:choose> tag are resolved to true. There is a no attribute for <c:otherwise> tag.

Example using <c:choose>, <c:when> and <c:otherwise> Tags

[code lang=”html”]
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<span style="font-size: 12px; line-height: 18px;"> pageEncoding="ISO-8859-1"%></span>
<span style="font-size: 12px; line-height: 18px;"><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" </span>
<span style="font-size: 12px; line-height: 18px;">"http://www.w3.org/TR/html4/loose.dtd"></span>
<span style="font-size: 12px; line-height: 18px;"><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %></span>
<span style="font-size: 12px; line-height: 18px;"><html></span>
<span style="font-size: 12px; line-height: 18px;"><head></span>
<span style="font-size: 12px; line-height: 18px;"><title>c:choose, c:when and c:otherwise Tag Example</title></span>
<span style="font-size: 12px; line-height: 18px;"></head></span>
<span style="font-size: 12px; line-height: 18px;"><body></span>
<span style="font-size: 12px; line-height: 18px;"><c:set var="number1" value="${6546}"/></span>
<span style="font-size: 12px; line-height: 18px;"><c:set var="number2" value="${12}"/></span>
<span style="font-size: 12px; line-height: 18px;"><c:set var="number3" value="${10}"/></span>
<span style="font-size: 12px; line-height: 18px;"><c:choose></span>
<span style="font-size: 12px; line-height: 18px;"> <c:when test="${number1 < number2}"></span>
<span style="font-size: 12px; line-height: 18px;"> ${"number1 is less than number2"}</span>
<span style="font-size: 12px; line-height: 18px;"> </c:when></span>
<c:when test="${number1 <= number3}">
<span style="font-size: 12px; line-height: 18px;"> ${"number1 is less than equal to number2"}</span>
<span style="font-size: 12px; line-height: 18px;"> </c:when></span>
<span style="font-size: 12px; line-height: 18px;"> <c:otherwise></span>
<span style="font-size: 12px; line-height: 18px;"> ${"number1 is largest number!"}</span>
<span style="font-size: 12px; line-height: 18px;"> </c:otherwise></span>
<span style="font-size: 12px; line-height: 18px;"></c:choose></span>
<span style="font-size: 12px; line-height: 18px;"></body></span>
<span style="font-size: 12px; line-height: 18px;"></html></span>
[/code]

How To Run?

  • Save this file as example.jsp in your eclipse IDE.
  • Now select this jsp file, right mouse click and select Run as ->Run on server

Output

When the execution process is completed successfully we will get the following output :
jstl_core c choose tag_demo

Above example demonstrate the use of <c:choose>, <c:when> and <c:otherwise> tags. In the above example we used the <c:set> tag first for assigning the value for variables and the we use the <c:when> and <c:otherwise> tags. These tags are sub tags of <c:choose>. These tags execute the the conditional statements specified by the <c:when test=””>.
Above example demonstrate the comparisons between three numbers and displays the greater number.

 

Previous Tutorial : JSTL Core c:if Tag :: Next Tutorial : JSTL Core c:import Tag

Filed Under: Java EE Tagged With: JSTL Tutorials

JSTL Core c:if Tag

February 18, 2014 by Krishna Srinivasan Leave a Comment

The <c:if> tag is the most useful of tags of JSTL core tag library. This tag evaluates an expression and displays its body content only if the expression is true. This tag allows test for condition, condition like checking for particular parameters in requestScope, sessionScope or pageScope.

Syntax For c:if Tag

[code lang=”html”]
<c:if attribute> body content </c:if>
[/code]

Attribute In c:if Tag

  • test: This condition determines whether or not the body content should be processed.
  • var: Name of the exported scoped variable for the resulting value of the test condition. The type of the scoped variable is Boolean.
  • scope: Scope of the variable to store the condition’s result.

Example of c:if Tag

[code lang=”html”]
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<span style="font-size: 12px; line-height: 18px;"><html></span>
<span style="font-size: 12px; line-height: 18px;"><head></span>
<span style="font-size: 12px; line-height: 18px;"><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"></span>
<span style="font-size: 12px; line-height: 18px;"><title>JSTL If Tag Example</title></span>
<span style="font-size: 12px; line-height: 18px;"></head></span>
<span style="font-size: 12px; line-height: 18px;"><body></span>
<span style="font-size: 12px; line-height: 18px;"> <c:set var="weight" value="92"></span>
<span style="font-size: 12px; line-height: 18px;"> </c:set></span>
<span style="font-size: 12px; line-height: 18px;"> <c:if test="${weight != null}"></span>
<span style="font-size: 12px; line-height: 18px;"> Weight of the product is ${weight * 0.453592} kgs. </span>
<span style="font-size: 12px; line-height: 18px;"> </c:if> </span>
<span style="font-size: 12px; line-height: 18px;"></body></span>
</html>
[/code]

How To Run?

  • Save this file as example.jsp in your eclipse IDE.
  • Now select this jsp file, right mouse click and select Run as ->Run on server

Output

When the execution process is completed successfully we will get the following output :
jstl_core c if tag_demo

Previous Tutorial : JSTL Core c:catch Tag :: Next Tutorial : JSTL Core c:choose,  c:when, c:otherwise Tags

Filed Under: Java EE Tagged With: JSTL Tutorials

JSTL Core c:catch Tag

February 18, 2014 by Krishna Srinivasan Leave a Comment

<c:catch> tag is used to handle the exceptions that can be raised by any of the tag which are located inside the body of the tag. The tag <c:catch> is member of core tag library of jstl.

The <c:catch> tag catches any throwable that occurs in the body of the tag. This tag is used to handle exception during run time.

Syntax For c:catch Tag

[code lang=”html”]
<c:catch> attributes> body content </c:catch>
[/code]

The tag accepts the var attribute, which takes the name of the variable that stores the exception thrown.

Example of c:catch Tag

[code lang=”html”]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<span style="font-size: 12px; line-height: 18px;"><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" </span>
<span style="font-size: 12px; line-height: 18px;">"http://www.w3.org/TR/html4/loose.dtd"></span>
<span style="font-size: 12px; line-height: 18px;"><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %></span>
<span style="font-size: 12px; line-height: 18px;"><html></span>
<span style="font-size: 12px; line-height: 18px;"><head></span>
<span style="font-size: 12px; line-height: 18px;"><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%></span>
<span style="font-size: 12px; line-height: 18px;"><html></span>
<span style="font-size: 12px; line-height: 18px;"><head></span>
<span style="font-size: 12px; line-height: 18px;"><title>JSTL c:catch Example</title></span>
<span style="font-size: 12px; line-height: 18px;"></head></span>
<span style="font-size: 12px; line-height: 18px;"><body></span>
<c:catch var="CatchNullPointerException">
<span style="font-size: 12px; line-height: 18px;"> <% String str = null; str.trim(); %></span>
</c:catch>
<c:if test="${CatchNullPointerException != null}">
<p>
Exception is : ${CatchNullPointerException} <br />
</p>
</c:if>
</body>
</html>
[/code]

How To Run?

  • Save this file as example.jsp in your eclipse IDE.
  • Now select this jsp file, right mouse click and select Run as ->Run on server

Output

When the execution process is completed successfully we will get the following output :
jstl_c catch tag_demo

Advantages of c:catch Tag

  • It is used to handle exception generated during run time.
  • This tag is used to capture any throwable exception that happens on the body of a page.

Previous Tutorial : JSTL Core Out Tag :: Next Tutorial : JSTL Core c:if Tag

Filed Under: Java EE Tagged With: JSTL Tutorials

EclipseLink – Configuration error. Class [com.mysql.jdbc.Driver] not found [javax.persistence.PersistenceException]

February 16, 2014 by Amr Mohammed Leave a Comment

The EclipseLink – JPA API is persistence service that provided a standard way to communicate with the database. The major important issue needs to take care of while development of a project using a JPA, is a database driver that can help the JPA communicates with the suggested database.

  • This post will cover a proper way to avoid such that exception can be thrown when a Database Driver is not deducted from the classpath.
  • If you have created your database connection and ping it successfully as already made in the article EclipseLink – JPA installation, there are no promises for your project to work. And once you have executed your application you’ve got the exception:

[code]
Exception Description: Configuration error. Class [com.mysql.jdbc.Driver] not found.
Exception in thread "main" javax.persistence.PersistenceException: Exception [EclipseLink-4003] (Eclipse Persistence Services – 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Configuration error. Class [com.mysql.jdbc.Driver] not found.
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy
(EntityManagerSetupImpl.java:766)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession
(EntityManagerFactoryDelegate.java:204)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.
createEntityManagerImpl(EntityManagerFactoryDelegate.java:304)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl
(EntityManagerFactoryImpl.java:336)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager
(EntityManagerFactoryImpl.java:302)
at net.javabeat.eclipselink.JPAImpl.main(JPAImpl.java:9)
Caused by: Exception [EclipseLink-4003] (Eclipse Persistence Services – 2.5.1.v20130918-f2b9fc5):
org.eclipse.persistence.exceptions.DatabaseException
Exception Description: Configuration error. Class [com.mysql.jdbc.Driver] not found.
at org.eclipse.persistence.exceptions.DatabaseException.configurationErrorClassNotFound
(DatabaseException.java:89)
at org.eclipse.persistence.sessions.DefaultConnector.loadDriverClass(DefaultConnector.java:267)
at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:85)
at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.setOrDetectDatasource
(DatabaseSessionImpl.java:204)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource
(DatabaseSessionImpl.java:741)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login
(EntityManagerFactoryProvider.java:239)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy
(EntityManagerSetupImpl.java:685)
… 5 more
[/code]

The main cause of this exception is because the driver of the database is missed from your eclipse project’s classpath. It will not be added by default, you have to add it by yourself.

In case you are using a MySQL database, then you should add mysql-connector-java-5.1.24-bin.jar into your classpath.

To add the JAR follow the steps below:

  1. Right-Click on the eclipse project that uses the JPA.
  2. Select the properties from the menu.
  3. Make the Libraries tab activated.
  4.  Click on the Add External JAR
  5. Navigate into the location of the mysql-connector-java-5.1.24-bin.jar JAR file.
  6. Select it and click open.
  7. Click Okay.
  8. Try to execute the program once again, it should work properly.

Filed Under: Java EE Tagged With: EclipseLink, JPA

  • « Previous Page
  • 1
  • …
  • 6
  • 7
  • 8
  • 9
  • 10
  • …
  • 21
  • Next Page »

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved