How do I pass current item to Java method by clicking a hyperlink or button in JSP page?

Simplest way: just let the link point to a JSP page and pass row ID as parameter: <a href=”delete.jsp?id=1″>delete</a> And in delete.jsp (I’m leaving obvious request parameter checking/validating aside): <% dao.delete(Long.valueOf(request.getParameter(“id”))); %> This is however a pretty poor practice (that was still an understatement) and due to two reasons: HTTP requests which modifies the data … Read more

How to loop through a HashMap in JSP?

Just the same way as you would do in normal Java code. for (Map.Entry<String, String> entry : countries.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); // … } However, scriptlets (raw Java code in JSP files, those <% %> things) are considered a poor practice. I recommend to install JSTL (just drop the … Read more

How to internationalize/localize a JSP/Servlet web application?

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

How to reference constants in EL?

EL 3.0 or newer If you’re already on Java EE 7 / EL 3.0, then the @page import will also import class constants in EL scope. <%@ page import=”com.example.YourConstants” %> This will under the covers be imported via ImportHandler#importClass() and be available as ${YourConstants.FOO}. Note that all java.lang.* classes are already implicitly imported and available … Read more

javax.el.PropertyNotFoundException: Property ‘foo’ not found on type com.example.Bean

javax.el.PropertyNotFoundException: Property ‘foo’ not found on type com.example.Bean This literally means that the mentioned class com.example.Bean doesn’t have a public (non-static!) getter method for the mentioned property foo. Note that the field itself is irrelevant here! The public getter method name must start with get, followed by the property name which is capitalized at only … Read more

Java / Jakarta EE web development, where do I start and what skills do I need? [closed]

(Updated Apr 2021) First of all, “Java EE” has since Sep 2019 been renamed to “Jakarta EE“, starting with version 8. Historically, there was also the term “J2EE” which covered versions 1.2 until 1.4. The “Java EE” covered versions 5 until 8. See also Java Platform, Enterprise Edition, History on Wikipedia. What exactly do I … Read more

XSS prevention in JSP/Servlet web application

XSS can be prevented in JSP by using JSTL <c:out> tag or fn:escapeXml() EL function when (re)displaying user-controlled input. This includes request parameters, headers, cookies, URL, body, etc. Anything which you extract from the request object. Also the user-controlled input from previous requests which is stored in a database needs to be escaped during redisplaying. … Read more