Pass variables from servlet to jsp

It will fail to work when: You are redirecting the response to a new request by response.sendRedirect(“page.jsp”). The newly created request object will of course not contain the attributes anymore and they will not be accessible in the redirected JSP. You need to forward rather than redirect. E.g. request.setAttribute(“name”, “value”); request.getRequestDispatcher(“page.jsp”).forward(request, response); You are accessing … Read more

How to access objects in EL expression language ${}

The expression ${foo} uses behind the scenes JspContext#findAttribute() which searches for attributes in PageContext, HttpServletRequest, HttpSession and ServletContext in this order by their getAttribute(“foo”) method whereby foo from ${foo} thus represents the attribute name “foo” and returns the first non-null object. So, if you do in a servlet ArrayList<Person> persons = getItSomehow(); request.setAttribute(“persons”, persons); // … Read more

Convert and format a Date in JSP

In JSP, you’d normally like to use JSTL <fmt:formatDate> for this. You can of course also throw in a scriptlet with SimpleDateFormat, but scriptlets are strongly discouraged since 2003. Assuming that ${bean.date} returns java.util.Date, here’s how you can use it: <%@ taglib prefix=”fmt” uri=”http://java.sun.com/jsp/jstl/fmt” %> … <fmt:formatDate value=”${bean.date}” pattern=”yyyy-MM-dd HH:mm:ss” /> If you’re actually using … Read more

How to pass Unicode characters as JSP/Servlet request.getParameter?

That can happen if request and/or response encoding isn’t properly set at all. For GET requests, you need to configure it at the servletcontainer level. It’s unclear which one you’re using, but for in example Tomcat that’s to be done by URIEncoding attribute in <Connector> element in its /conf/server.xml. <Connector … URIEncoding=”UTF-8″> For POST requests, … Read more

EL expressions not evaluated in JSP

Yes, i have doctype in web.xml <!DOCTYPE web-app PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN” “java.sun.com/dtd/web-app_2_3.dtd”; > Remove that <!DOCTYPE> from web.xml and make sure that the <web-app> is declared conform Servlet 2.4 or newer and all should be well. A valid Servlet 3.0 (Tomcat 7, JBoss AS 6-7, GlassFish 3, etc) compatible web.xml look … Read more

How to transfer data from JSP to servlet when submitting HTML form

Create a class which extends HttpServlet and put @WebServlet annotation on it containing the desired URL the servlet should listen on. @WebServlet(“/yourServletURL”) public class YourServlet extends HttpServlet {} And just let <form action> point to this URL. I would also recommend to use POST method for non-idempotent requests. You should make sure that you have … Read more

Why Facelets is preferred over JSP as the view definition language from JSF2.0 onwards?

True, JSP has some templating capabilities, but the biggest disadvantage of using JSP in JSF is that JSP writes to the response as soon as it encounters template text content, while JSF would like to do some pre/post processing with it. In JSF 1.0/1.1 the following JSF code <h:outputText value=”first”> second <h:outputText value=”third”> fourth would … 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