Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
JSTL XML Tags 
 

In this fourth & last part of the tutorial on JSTL,the author deals with the 'sql' tags in JSTL and shows how they greatly simplify simple database operations like 'select' queries. In another demo, common database operations like 'add','modify' , 'dele

 

JSTL XML Tags

                         

PART-4 - JSTL  &  SQL-TAGS  

JSP Tutorials HomeTutorial Home | Part 1 | Part 2Part 3 | Part 4

In this fourth & last part of the tutorial on JSTL,the author deals with the 'sql' tags in JSTL  and shows how they greatly simplify simple database operations like 'select' queries.  In another demo, common database operations like 'add','modify' , 'delete' & 'verify'also are dealt with.

The Struts community has ordained that JSP should be  strictly a 'view-technology', in the Model-View-Controller Architecture. According to Struts philosophy, JSP should not deal with Data-Accesss and such data access  should be done by 'Model' components only.( read 'beans'). JSTL , however, provides for sql tags, inspired by ColdFusion! ( please see a very short tutorial on DB-Operations using ColdFusion' available in this issue as a separate lesson. and compare JSTL code and CF code!).And , a few months back, the editor of 'Java Lobby' magazine was all admiration for the absolutely nice features of these sql tags, whatever, 'struts' may say! Just as EJB may be 'overkill', except for really big Enterprise applications, Struts also may be  an unnecessary complication for small and medium level projects. In such cases, it is much more direct to provide for data access by the JSP itself, but using JSTL 'sql' tags.We take up these 'sql' tags in  this part of the tutorial.

Let us begin with 'sql.htm'. It just provides a simple form with just a textarea & submit button. Normally, queries by MIS department will be very complex and so we have provided a textarea for the 'select' query.After filling up the query, it is submitted and the corresponding query.jsp is invoked.

// query.htm

<html>  
<body>  
<form  method=post  action="query.jsp">  
<textarea    name='area1'    rows=10  cols=30>  
</textarea>  
<input  type=submit>  
</form>  
</body>  
</html>

------------------------------------
query.jsp is given below. In the standard jdbc code,we begin by asking for the availability of the driver. "jdbc.odbc.JdbcOdbcDriver". And then, we specify the URL of the database as  'jdbc:odbc:telephone'.

Similarly, in JSTL also, we begin with

<sql:setDataSource tag.

  It has attributes for 'driver'   and 'url'. We will refer to the database as 'db'.   The next step is to collect the query typed in area1 by the user.

<c:set  var="s"  value="${param.area1}"  />

is used for this purpose.We also check up whether the query typed by the user has indeed been correctly received.

<c:out   value="${s}"  />

<br>

Next, the '<sql:query'  tag, takes  three attributes., such as, symbolic name:   
 
var="query1" datasource="${db}        sql="${s}

The query result is then displayed in table form.It should be possible to follow the code now.In our example, we are having an Access db, with table1, having two fields, (name, place). registered with ODBC.

// query.jsp

============

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

<html>  
<body>  

<sql:setDataSource       var="db" 
    
 driver="sun.jdbc.odbc.JdbcOdbcDriver" 
     
url="jdbc:odbc:dbdemo"  />  

<c:set
   var='s'   value="${param.area1}"   />  
<c:out
   value="${s}"    />  <br>  
<sql:query 
 var="query1"    dataSource="${db}"    sql="${s}"     />   
</sql:query>  

<table border="1">  
  <c:forEach   var="row"  items="${query1.rows}" >  
 
<tr> 
   
<td> <c:out value="${row.name}" /></td> 
   
<td> <c:out value="${row.place}" /></td>  
 
</tr>  

 
</c:forEach>  
</table>  
</body>  
</html>

===============================================

In the second example,(dbeditor.htm & dbeditor.jsp) we provide a combo, with options such as: add, modify, remove and verify.

In JSTL , we have a separate sql tag known as '<sql:update'   for 'add', 'modify' and 'remove' operations.

When we want to verify, we must use' <sql:query'  tag, because, resultset will be returned.

------------------

dbeditor.htm

<html>  
<body bgcolor=lightgreen>  
<form method=post action="dbeditor.jsp">  
<input type=text name='text1'>name<br>  
<input type=text name='text2'>number<br>  
<input type=text name='text3'> criterion<br>  
<select name=combo1> 
  
<option value="add">add 
  
<option value="delete">delete 
  
<option value="modify">modify 
  
<option value="verify">find 
</select>  
<br>  
<input type=submit>  
</body>  
</html>  
-------------------------------------------------

dbeditor.jsp

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

<sql:setDataSource      
var="db" 
     
driver="sun.jdbc.odbc.JdbcOdbcDriver" 
     
url="jdbc:odbc:dbdemo"  />

<c:set     var='a'   value='${param.text1}'  />  
<c:set     var='b'    value='${param.text2}' />  
<c:set     var='c'    value='${param.text3}' />  
<c:set     var='d'   value='${param.combo1}' />  
----------------------------------------------

<c:if  test="${d == 'add'}" >

<sql:update var="query1"  dataSource="${db}"  
 
sql="insert into table1 values${a}','${b}')" > 
</sql:update>  
<c:out      value="record added"/>  
</c:if>  
---------------------------------------------

<c:if             test="${d == 'delete'}" >  
 
<sql:update  var="query1"  dataSource="${db}" 
 
sql="delete from table1 where name='${a}'" >   
 
</sql:update> 
 
<c:out      value="record deleted"/>  
</c:if>
-------------------------------------------

<c:if  test="${d == 'modify'}" >
<
sql:update var="query1"  dataSource="${db}"  
   sql="update table1 set table1.name='${a}', 
  
table1.place='${b}' where 
  
table1.name='${c}'" >   

<--sql  should be typed in a single line  -->

</sql:update>
<c:out value="record modified"/>
</c:if>
------------------------------------------
<c:if  test="${d == 'verify'}" >  
 
<sql:query   var="query1"  dataSource="${db}"  
 
sql="select * from table1 where name='${a}'" >   
 
</sql:query>  
<table border="1">  
 
<c:forEach   var="row" tems="${query1.rows}" >  

 
<c:set       var="n" value="OK" />  
 
<tr> 
   
<td> <c:out value="${row.name}" /></td> 
   
<td> <c:out value="${row.place}" /></td>  
 </tr>

 </c:forEach>  
 
<c:if  test="${n != 'OK'}" >  
 
<c:out value="No such Records" />  
 
</c:if>
<
/table>
</c:if>
</html>
<body>
====================

These are, afterall, the essentials. All else, are only, ornamental. Thus, it will be seen that the JCP has done a great job in creating really fine standard tags, for simplifying routine tasks. But, Java coders, have to take note that , their job as JSP coders, will be slowly eroded  by page-authors using JSTL!

Suggested Reference Books

  1) Pro  JSP  by 
    
SIMON BROWN & OTHERS 
    
(A-Press) 
    
(Excellent material)

    2) JavaServer Pages   by
        Hans Bergsten ( Third Edition)
       (O'Reilly Press/SPD)

  3) EARLY ADOPTER JSP Standard Tag Library    by
     JAYSON FALKNER & OTHERS
     Wrox Press

  4) JSTL in Action   by
     SHAWN BAYERN
     (Manning/ DreamTech).

JSP Tutorials HomeTutorial Home | Part 1 | Part 2Part 3 | Part 4

                         

 

» View all related tutorials
Related Tags: c xml library file files stl orm ant form io user sed jstl tag parameter transform this js for add

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

15 comments so far (
post your own) View All Comments Latest 10 Comments:

In JSTL tutorials
Section: JSTL and SQL Tags
Example: query.jsp

uri="http://java.sun.com/jstl/core" &
uri="http://java.sun.com/jstl/sql"
this uri attribute must be with in <%@ taglib /> directive

moreover this uri attribute must have following value in order to compile all the exmples of tutorial:
uri="http://java.sun.com/jsp/jstl/core" for core tags and
uri="http://java.sun.com/jsp/jstl/" for sql tags

Thanks and Regards
Achint Kumar

Posted by Achint Kumar on Saturday, 08.2.08 @ 10:55am | #70480

This is one of the simplest tutorial for JSTL.It made my concepts of jstl really very strong.

Posted by Mandar Satam on Monday, 05.19.08 @ 10:42am | #60428

Thanks RoseIndia,You done a great job.Thank you for helping me .

Posted by kedari on Wednesday, 03.12.08 @ 15:30pm | #52464

its very useful for me.
thanks,,,

Posted by sarath on Thursday, 02.28.08 @ 10:31am | #50380

Excellent tutorial, that can be easily understood by a new programmer. Keep up the good work.

Posted by raghav on Thursday, 12.27.07 @ 03:02am | #43888

Don't know any Java or JSTL. But after this amazingly helpful tutorial, I feel confident in implementing JSTL.

Saved me $50+ on books that would cover the same thing.

Thanks

Posted by Kenny on Thursday, 12.20.07 @ 00:09am | #43014

this is vary use ful to us

Posted by himadri on Monday, 12.10.07 @ 12:02pm | #41715

cool tutorial to jump start on JSTL...
Wonderful Ramasamy..

Thanks



Siva

Posted by Siva on Tuesday, 10.2.07 @ 18:33pm | #30953

Great tutorial.. excellent for starters. Though there are slight mistakes/errors to be rectified but still a great work... keep up the good work!

Posted by Guest on Thursday, 09.27.07 @ 11:51am | #30067

it is very useful for learning jstl

Posted by venu on Sunday, 08.26.07 @ 10:39am | #24157

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.