Sep 18

When using JTST to access a request parameter we typically use something like this:

${param.xyz}

If the parameter name contains a . then this becomes

${param.xyz.abc}

Problem is that JSTL interprets this as being the abc property of the xyz bean which is not what we want.

In this case we instead need to use the more long winded JSTL syntax for specifying the property name of interest:

${param['xyz.abc']}

This now correctly returns the value of the request paramater with name ‘xyz.abc’.

written by objects \\ tags: , , , ,

Jun 08

If you require multiple imports then you can either use multiple import page directives, or specify them all in one page directive which each import commas separated

Multiple import directives

<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.net.*" %>

One import directive (comma separated)

<%@ page import="java.util.*, java.io.*, java.net.*" %>

written by objects \\ tags: ,

Apr 15

Sometimes you need the path of a file in your web application, for example if you need to open a file for reading. You could hard code where your web application is deployed but that will break if you ever move where your application is deployed.

Luckily the ServletContext class provides a getRealPath() method to determine the path of any file contained within your web application.

public class ImageServlet extends HttpServlet
{
   public void doGet(HttpServletRequest req,
      HttpServletResponse resp) throws IOException
   {
    	ServletContext application = getServletContext();
    	
    	// Get the absolute path to file that lives in this application
  
        String filepath = application.getRealPath("WEB-INF/images/a.gif");
        File file = new File(filepath);
        FileInputStream in = new FileInputStream(file);


written by objects \\ tags: , , , ,