0

Eclipse gives me all sorts of evil mojo when I type in the following:

<img src=<%if(request.getParameter("x")==null){"/images/x.gif">}  
else{"get-it?x=<%=request.getParameter("x")}%>">

What is the correct syntax for specifying a default inage to be displayed if the parameters for a dynamically-generated image are null? Also, I may want to add other conditions which would need to cause the default image to be displayed.

I should add that this is in a jsp page controlled by a java servlet.

CodeMed
  • 9,527
  • 70
  • 212
  • 364
  • Now, I don't understand the role of your JSP here. You are setting the imgURL in `img src`, and then where are you using it? In the `2nd` servlet, you are only retrieving the parameter `x`, which was set in `1st` servlet. – Rohit Jain Aug 06 '13 at 17:03
  • @CodeMed.. Hmm. Then what's the use of 1st servlets. I suspect, one of your servlets is an extra servlet in the middle, which is not needed. – Rohit Jain Aug 06 '13 at 17:16
  • OK. Let me update my answer. – Rohit Jain Aug 06 '13 at 17:26
  • Check my updated answer. From whatever you explained, I guess you need to do that way. – Rohit Jain Aug 06 '13 at 17:37
  • You don't need any dependency for `EL`. It comes with tomcat container, if you are using that. You only need `JSTL` libraries. And wait, what? You don't want to use JSTL and EL, and want to use almost a decade old Scriplets? – Rohit Jain Aug 06 '13 at 18:51
  • Wait, did you go through the link I've added at the bottom of my answer? Those are the best Crash Course, you will get in this topic. And regarding the harm, well, I can show you how to do it that way, but it's bad practice, which you shouldn't use any more in new code. – Rohit Jain Aug 06 '13 at 18:56
  • In the new code that you showed, you have changed the location of `req.getAttribute()`, which should be in `JSP`, and `request.getParameter()` which should be in the Servlet. You set the attribute using `request.setAttribute()` in the servlet. – Rohit Jain Aug 06 '13 at 18:58
  • @RohitJain Yes, I imported jstl-1.2.jar into the lib folder using eclipse. I even restarted eclipse afterwards, just to be sure. – CodeMed Aug 06 '13 at 20:25
  • Use this: `<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>` – Rohit Jain Aug 06 '13 at 20:27

2 Answers2

2

Use EL (Scriplets are dead long back):

<c:set var="imgURL" value="get-it?x=${param.x}" />
<img src="${empty param.x ? '/images/x.gif' : imgURL}" />

or you can also do it without setting another attribute:

<img src="${empty param.x ? '/images/x.gif' : 'get-it?x='}${param.x}" />

Ternary operator will work if you just have an if-else. As stated in comments by @Sotirios if you have multiple conditions to choose from - if-else if-else ladder, then you would need to use <c:choose> tag.

Note, you need to add JSTL libraries in your lib folder, and include the core taglib.


Having said all that, you should also consider preparing the Image URI in the Servlet itself, forwarding to a JSP.

Suppose you have an HTML form:

<form action="/servlet1" method="POST">
    <input type = "text" name="x" />
</form>

In Servlet mapped at /servlet1, you should get the parameter x, and create the Image URL based on that. And then put that image url in request attribute:

String x = request.getParameter("x");
if (x == null) {
    // Set Default image in request attribute
    request.setAttribute("imageURL", "images/x.gif");

} else {
    // Else create the image, and set it in request attribute
    resp.setContentType("image/gif");
    BufferedImage bi = new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB);
    GetBI fetchBI = new GetBI(); 
    bi = fetchBI.get_bi(x); 
    ImageIO.write(bi,"gif",resp.getOutputStream()); 

    request.setAttribute("imageURL", "get-it?x="+x);
}

// Forward the request to the required JSP

then in JSP Page, you can fetch the *imageURL*, using EL:

<img src="${imageURL}" />

See, I just needed one Servlet. Take a look, and comment, if I missed something. I think what you want to do can be done simply using a single Servlet.

See also:

Community
  • 1
  • 1
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • @CodeMed. You need to add `JSTL` libraries, and include the taglib in your jsp page. See our [JSTL Tag Wiki](http://stackoverflow.com/tags/jstl/info) – Rohit Jain Aug 06 '13 at 16:15
  • @RohitJain I prefer Qwerky's suggestion of putting it in the servlet instead of the jsp. The problem is that the servlet would have to switch between sending a uri or doing imageio.write depending on whether it is the correct file. I am not finding an easy answer at http://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageIO.html . Any suggestions about that approach? – CodeMed Aug 06 '13 at 16:29
  • @CodeMed. Can you update your question with some details about the flow between those servlets and JSP. How you have it now? – Rohit Jain Aug 06 '13 at 16:33
1

The normal approach would be to work out the image URI in the logic of your application (ie the servlet) and not in the view (ie the JSP).

In your servlet;

String uri;
String x = request.getParameter("x");
if (x == null) {
  uri = "/images/x.gif";
} else {
  uri = "get-it?x=" + x;
}
request.setAttribute("imageUri", uri);

In your JSP;

<img src="${imageUri}"/>
Qwerky
  • 18,217
  • 6
  • 44
  • 80