Check a collection size with JSTL

<c:if test=”${companies.size() > 0}”> </c:if> This syntax works only in EL 2.2 or newer (Servlet 3.0 / JSP 2.2 or newer). If you’re facing a XML parsing error because you’re using JSPX or Facelets instead of JSP, then use gt instead of >. <c:if test=”${companies.size() gt 0}”> </c:if> If you’re actually facing an EL parsing … Read more

How do you import classes in JSP?

Use the following import statement to import java.util.List: <%@ page import=”java.util.List” %> BTW, to import more than one class, use the following format: <%@ page import=”package1.myClass1,package2.myClass2,….,packageN.myClassN” %>

if…else within JSP or JSTL

Should I use JSTL ? Yes. You can use <c:if> and <c:choose> tags to make conditional rendering in jsp using JSTL. To simulate if , you can use: <c:if test=”condition”></c:if> To simulate if…else, you can use: <c:choose> <c:when test=”${param.enter==’1′}”> pizza. <br /> </c:when> <c:otherwise> pizzas. <br /> </c:otherwise> </c:choose>

Difference between getAttribute() and getParameter()

getParameter() returns http request parameters. Those passed from the client to the server. For example http://example.com/servlet?parameter=1. Can only return String getAttribute() is for server-side usage only – you fill the request with attributes that you can use within the same request. For example – you set an attribute in a servlet, and read it from … Read more

JSP tricks to make templating easier?

As skaffman suggested, JSP 2.0 Tag Files are the bee’s knees. Let’s take your simple example. Put the following in WEB-INF/tags/wrapper.tag <%@tag description=”Simple Wrapper Tag” pageEncoding=”UTF-8″%> <html><body> <jsp:doBody/> </body></html> Now in your example.jsp page: <%@page contentType=”text/html” pageEncoding=”UTF-8″%> <%@taglib prefix=”t” tagdir=”/WEB-INF/tags” %> <t:wrapper> <h1>Welcome</h1> </t:wrapper> That does exactly what you think it does. So, lets expand … Read more

How should I use servlets and Ajax?

Indeed, the keyword is “Ajax”: Asynchronous JavaScript and XML. However, last years it’s more than often Asynchronous JavaScript and JSON. Basically, you let JavaScript execute an asynchronous HTTP request and update the HTML DOM tree based on the response data. Since it’s pretty tedious work to make it to work across all browsers (especially Internet … Read more

Design Patterns web based applications [closed]

A bit decent web application consists of a mix of design patterns. I’ll mention only the most important ones. Model View Controller pattern The core (architectural) design pattern you’d like to use is the Model-View-Controller pattern. The Controller is to be represented by a Servlet which (in)directly creates/uses a specific Model and View based on … Read more

Evaluate empty or null JSTL c tags

How can I validate if a String is null or empty using the c tags of JSTL? You can use the empty keyword in a <c:if> for this: <c:if test=”${empty var1}”> var1 is empty or null. </c:if> <c:if test=”${not empty var1}”> var1 is NOT empty or null. </c:if> Or the <c:choose>: <c:choose> <c:when test=”${empty var1}”> … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)