Wednesday, December 18, 2013

Insert through JSTL

index.jsp

<form method="POST" action="insertLocations.jsp">
                        <label> Country : </label> <input type="text" name="country" id="country" /><br/><br/>
                        <label> State : </label> <input type="text" name="state" id="state" /><br/><br/>
                        <label> City :  <input type="text" name="city" id="city" /><br/><br/>
                        <span class="submit"><input type="submit" name="Add" value="Add Location" /></span>
</form>

insertLocations.jsp

<%@page contentType="text/html" 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" %>
<c:import url="include/connection.jsp"></c:import>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<c:import url="include/connection.jsp"></c:import>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    </head>
    <body>
       <c:if test="${pageContext.request.method=='POST'}" >
           <c:catch var="exception">
           <c:if test="${empty param.country or empty param.state or empty param.city}">
               <c:redirect url="addLocations.jsp?message">
                   <c:param name="message"  value="Please fill all details."> </c:param>
               </c:redirect>
           </c:if>
          
           <sql:query var="database" dataSource="${ds}">
                   select count(*) as kount  from tbl_locations where `country`='${param.country}' && `state`='${param.state}' &&`city`='${param.city}'
           </sql:query>
            <c:forEach items="${database.rows}" var="r">
            <c:choose>
                <c:when test="${r.kount gt 0}">
                    <c:redirect url="addLocations.jsp?message=Location Already exists."></c:redirect>
                </c:when>
           </c:choose>
           
            </c:forEach>                  
                  
          
          
       <sql:update var="insert_loc" dataSource="${ds}" >
           insert into tbl_locations (`country`,`state`,`city`) values (?,?,?)
           <sql:param value="${param.country}"></sql:param>
           <sql:param value="${param.state}"></sql:param>
           <sql:param value="${param.city}"></sql:param>          
       </sql:update>
         
           <c:if test="${insert_loc>0}">
               <c:redirect url="addLocations.jsp">
                   <c:param name="message" value="Location added succesfully."></c:param>
               </c:redirect>
           </c:if>
           </c:catch>
           <c:if test="${not empty exception}">
               <c:redirect url="addLocations.jsp">
                   <c:param name="message" value="Something went wrong."></c:param>
               </c:redirect>
            </c:if>
       </c:if>
    </body>
</html>
 

No comments:

Post a Comment