JSTL Core Tags

JSTL Core Tags:
JSTL – JavaServer Pages Standard Tag Library,
Component of the Java Web application which extends the JSP specification by adding a tag library of JSP tags.

JSTL helps us to avoid using java codes within JSP. JSTL tags can be classified into core, formatting, sql, xml and jstl function tags.

In this article we are going to see JSTL Core Tags. We are going to see some examples of JSTL core tags.

<c:set> – helps to set a value to variable
<c:out> – Used to print the value
<c:if> – If loop
<c:choose> – Similar to If else
<c:when> – used within <c:choose>
<c:forEach> – For loop
<c:forTokens> – For loop with Delimiter
<c:redirect> – Forwarding to another page
<c:url> – Forward to another url

For using JSTL core in JSP page, We need to import JSTL core library

<%@ taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c” %>

The difference between forEach and forTokens is, forEach is similar to for loop in java. forTokens helps us to iterate over a collection by removing the delimiters (i.e .,/'”;:…etc). We will be able to understand once we go through the example code.

Now let us see an example for JSTL core tags,

Most of the functions are self-explanatory as they are similar to java functions.

op1

Read More

Context Parameter and Init Parameter in Servlets

Context Parameter and Init Parameter in Servlets
The deployment descriptor can include configuration information for Servlet.

There are 2 types of Parameters,

  • Context Parameters
  • Init Parameters

 

Context Parameter Init Parameter
Context Parameter are declared outside the Servlet
and can be used anywhere in
web application
Init Parameters are declared inside the servlet
and can be used only within
that servlet
Reading Context Parameters: String context=
this.getServletContext().getInitParameter(“paramname”);
Reading Init Parameter, there are 2 ways –
1) String name=
this.getInitParameter(“paramname”);
2) name=
getServletConfig().getInitParameter
(“paramname”);

Context Param with param name “sname” and value “Alpha” is declared in web.xml
Init Param with param name “ename” and value “Beta” is declared inside servlet in web.xml
Web.xml

struct1

In  web.xml we can see that, Context Param is declared independent of Servlets so context-param value can be used anywhere in entire web application and in any Servlets.
Init param is declared inside servlet1, so can be used only inside servlet1.

For our understanding, lets create couple of JSP pages and Servlets,

Match.jsp – Inside body enter “<h1> Value Matched </h1>”
error.jsp – Inside body enter “<h1> Mismatch Error </h1>”

servlet.java
servlet1.java

Init Parameter Example:
Init Parameter is declared in Servlet1, lets check Servlet1 first:
servlet.java

Output:

op1
Init parameter is declared in servlet1, so the value is matched and Match.jsp showed.
Now lets try with Servlet2.java

Servlet2.java

Output:
op2
Since Init parameter is declared and valid only inside servlet.java, it works inside that. So when we tried with servlet1.java we were taken to error page.

Context Parameter Example:
servlet2.java

Output:
op1
servlet3.java

Output:
op1
Since context parameters are independent of Servlets, it takes us to match page.

 

 

 

 

Read More

CRUD using JSP Hibernate (JSP + Hibernate Example)

CRUD using JSP Hibernate (JSP + Hibernate Example):
(JSP + Hibernate CRUD)

Here is a code for creating, updating, deleting and reading using JSP and Hibernate. This code is tested in Netbeans 8.0.2.

Steps:
1. File –>New Project –> Java Web –>Web Application (Choose Project)
2. Web Application name –>jsphiber then click–> Next (Name and Location)
3.  click next (Server and Settings)
4. In Frameworks select Hibernate and choose the Database from the dropdownlist and click Finish
Video Tutorial:

 

Project Structure:
structure

javainfinite.pojo.EmployeeDetails.java

javainfinite.DAO.EmployeeDao

javainfinite.DAOImpl.EMployeeDaoImpl

javainfinite.DAOImpl.HibernateUtil.java

Hibernate.cfg.xml

EmployeeAdd.jsp

ShowAll.jsp

Controller.EmployeeController

web.xml

You can download this example here: Download Source Code (Note: JSP and Src files are included, Please dont forget to include libraries to run this code. – JSTL, Hibernate Framework, mySQL driver)

Output:
add
add1
CLick Show after Add,
update
After Update,
delete
After Delete,
afterdelete

 

 

 

 

 

 

 

 

 

 

Read More

Autocomplete textbox in JSP with Database

Autocomplete textbox in JSP with Database
Autocomplete textbox in JSP with Database using Jquery. You can retrieve your data from database and make it as suggestions for autofill textbox in JSP.

Requirements:
Download jquery-ui.js,  jquery-1.10.2.js, jquery-ui.css

JSP Page:

 

Read More

Keyword Density Analysis using Java

Keyword Density Analysis Using Java

This code counts number of repeated words in a paragraph, count its number of appearance and calculates the density of the word.

Tested with NetBeans 8.0

JSP Page to get Input from User:

Servlet:

Show.jsp

 

Read More