Example 5.5 JSP with Scriptlet Code Employee List <%-- Display All employees belonging to a department and earning at most the given salary --%> <% // Get the department for which the employees are // to be listed String deptidStr = request.getParameter( Constants.REQ_DEPTID); // Get the max salary constraint String salaryStr = request.getParameter( Constants.REQ_SALARY); // validate parameters // if salary or department not specified, go to // error page if ( (deptidStr == null) || (salaryStr == null ) ) { request.setAttribute(Constants.ATTR_MESSAGE, "Insufficient query parameters specified" + "(Department and Salary)"); request.getRequestDispatcher("/error.jsp"). forward(request, response); } // convert to numerics int deptid = 0; float salary = 0; try { deptid = Integer.parseInt(deptidStr); salary = Float.parseFloat(salaryStr); } catch(NumberFormatException e) { request.setAttribute(Constants.ATTR_MESSAGE, "Invalid Search Values" + "(department id and salary )"); request.getRequestDispatcher("/error.jsp"). forward(request, response); } // check if they within legal limits if ( salary < 0 ) { request.setAttribute(Constants.ATTR_MESSAGE, "Invalid Search Values" + "(department id and salary )"); request.getRequestDispatcher("/error.jsp"). forward(request, response); } %>

List of employees in department # <%=deptid%> earning at most <%= salary %>.

<% Iterator employees = new EmployeeDelegate(). getEmployees(deptid); %> <% while ( employees.hasNext() ) { EmployeeVO employee = (EmployeeVO) employees.next(); // display only if search criteria is met if ( employee.getYearlySalary() <= salary ) { %> <% } } %>
First Name Last Name Designation Employee Id Tax Deductibles Performance Remarks Yearly Salary
<%=employee.getFirstName()%> <%=employee.getLastName()%> <%=employee.getDesignation()%> <%=employee.getId()%> <%=employee.getNoOfDeductibles()%> <%=employee.getPerformanceRemarks()%> <%=employee.getYearlySalary()%>
<%@ include file="/jsp/trace.jsp" %>

Business logic and presentation formatting are intermingled within this JSP view.