Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How to use JSTL to fetch data from database?

user-image
Question added by Mohd Nadeem Mohd Rehman Mohammed , Software Engineer , Softworx Technologies
Date Posted: 2016/09/16
Mohd Nadeem Mohd Rehman Mohammed
by Mohd Nadeem Mohd Rehman Mohammed , Software Engineer , Softworx Technologies

The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which encapsulates core functionality common to many JSP applications. JSTLhas support for common, structural tasks such as iteration and conditionals, tags for manipulating XML documents, internationalization tags, and SQL tags.

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

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> 

 

define datasource inside web.xml so all jsp files access that database easily

web.xml

<context-param>

    <param-name>javax.servlet.jsp.jstl.sql.dataSource</param-name>

    <param-value>jdbc:mysql://localhost:3306/dbname,

  com.mysql.jdbc.Driver,username,password  </param-value>

 

  </context-param>

 

showData.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

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

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>    

<!DOCTYPE html>

<html>

<body>

 

<sql:query>select Message from test<sql:query>

 

<p>Inside query tag you have to write query and parameter if you want.

and access you resultset using forEach loop </p>

 

<c:forEach var="row" items="${result.rows }">

<c:out value="${row.Message }"></c:out>

</c:forEach>

 

</body>

</html>

 

More Questions Like This