How to access at request attributes in JSP?
EL expression: ${requestScope.Error_Message} There are several implicit objects in JSP EL. See Expression Language under the “Implicit Objects” heading.
EL expression: ${requestScope.Error_Message} There are several implicit objects in JSP EL. See Expression Language under the “Implicit Objects” heading.
One reason would be performance and memory. If you have a page that doesn’t need to be involved in a session (like say, an about.jsp or faq.jsp) then the default behaviour of involving every JSP in a session will impose the overhead of creating a new session object (if one doesn’t already exist) and increased … Read more
Just use LoopTagStatus#isLast(). <c:forEach items=”${fileList}” var=”current” varStatus=”loop”> { id: 1001, data: [ “<c:out value=”${current.fileName}” />”, “<c:out value=”${current.path}” />”, “<c:out value=”${current.size}” />”, “<c:out value=”${current.type}” />” ] }<c:if test=”${!loop.last}”>,</c:if> </c:forEach> You can also use the conditional operator in EL instead of <c:if>: ${!loop.last ? ‘,’ : ”}
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
You need to enclose that in <%! %> as follows: <%! public String getQuarter(int i){ String quarter; switch(i){ case 1: quarter = “Winter”; break; case 2: quarter = “Spring”; break; case 3: quarter = “Summer I”; break; case 4: quarter = “Summer II”; break; case 5: quarter = “Fall”; break; default: quarter = “ERROR”; } … Read more
redirect sets the response status to 302 [1], and the new url in a Location header, and sends the response to the browser. Then the browser, according to the http specification, makes another request to the new url forward happens entirely on the server. The servlet container just forwards the same request to the target … Read more
Note: I find it hard to think of any “hidden features” for JSP/Servlet. In my opinion “best practices” is a better wording and I can think of any of them. It also really depends on your experience with JSP/Servlet. After years of developing you don’t see those “hidden features” anymore. At any way, I’ll list … Read more
If your actual concern is the dynamicness of the webapp context (the “AppName” part), then just retrieve it dynamically by HttpServletRequest#getContextPath(). <head> <link rel=”stylesheet” href=”https://stackoverflow.com/questions/4764405/${pageContext.request.contextPath}/templates/style/main.css” /> <script src=”${pageContext.request.contextPath}/templates/js/main.js”></script> <script>var base = “${pageContext.request.contextPath}”;</script> </head> <body> <a href=”${pageContext.request.contextPath}/pages/foo.jsp”>link</a> </body> If you want to set a base path for all relative links so that you don’t need to … Read more
Using java string concatenation works better. #{var1 == 0 ? ‘hi’ : ‘hello’.concat(var2)} The benefit here is you can also pass this into a function, for instance #{myCode:assertFalse(myVar == “foo”, “bad myVar value: “.concat(myVar).concat(“, should be foo”))}
In case of a basic JSP/Servlet webapplication, the basic approach would be using JSTL fmt taglib in combination with resource bundles. Resource bundles contain key-value pairs where the key is a constant which is the same for all languages and the value differs per language. Resource bundles are usually properties files which are loaded by … Read more