0

I'm currently learning Java Server Side programming with JSP and Servlet. I have created a simple dynamic web project and deployed it in apache tomcat. Basically, there's only one page(jsp) as the front-end that is dynamically altered with the servlet based on what the user selects(form/radio buttons).

The directory structure:
|   index.jsp
|
+---img
|       apples.jpg
|       oranges.jpg
|       salad.jpg
|       strawberries.jpg
|
\---WEB-INF
    |   web.xml
    |
    \---classes
            DynamicServlet.class
            DynamicServlet.java

I have configured the web.xml to launch index.jsp @(http://localhost:port/contextname)

<web-app>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>    
</web-app>

Relevent code from the index.jsp:

...<head>...<style> 
.jumbotron {
    background-image: linear-gradient( rgba(0, 0, 0, ${grad}), rgba(0, 0, 0, ${grad}) ), url('${pageContext.request.contextPath}/img/${img}');
    background-size: cover;
    color: ${color};
}

...<body>
<div class="container"> <!-- jumbotron-container -->
    <div class="jumbotron">
        <h1>Hello, World!</h1> <!-- <====This line====> -->
    </div>  <!-- /jumbotron --> 
</div>  <!-- /jumbotron container -->

<div class="container"> <!-- form-container -->
    <form action="${pageContext.request.contextPath}/magic" method="get">
        <label class="form-label">Pick your favorite:</label>
        <div class="input-group form-group col-xs-6">
            <span class="input-group-addon">
                <input type="radio" name="option" value="apples">
            </span>
            <input type="text" class="form-control" disabled="disabled" placeholder="Apples">
        </div>  <!-- /apples -->

        <div class="...
        </div>  <!-- /oranges -->

        <div class="...
        </div>  <!-- /strawberries -->

        <div class="...
        </div>  <!-- /salad -->

        <input type="submit" class="btn btn-primary" value="Submit">
    </form> <!-- /form -->
</div>  <!-- /form-container -->

The result is something like this: jumbotron

Now, when a user makes a selection and submits, the servlet updates the jumbotron cover with an image based on user selection. I have used EL for this as you can see.

Servlet code:

    ...doGet(HttpServletRequest.....{
           String option = request.getParameter("option");
           try {
                switch (option) {
                    case "apples":
                        request.setAttribute("img", "apples.jpg");
                        break;

                    case "oranges":....and so on

    }...
  ...request.getRequestDispatcher("index.jsp").forward(request, response);

And it works as expected. What I haven't been able to figure out is how to CHANGE the jumbotron text based on the selection. I'm looking for a way to somehow replace the line <h1>Hello, World!</h1> in index.jsp with custom text(something like <h1>Yay, Apples!</h1>) based on user selection from the servlet preferably using the getRequestDispatcher(). Any hints for achieving this?

Note: 1. The landing page must have a static jumbotron text(Hello, World!)
2. The change must reflect on the same page. I don't intend to create a separate jsp.

I'm a beginner so please be considerate if I have missed an obvious solution. Thanks.

The-Droidster
  • 635
  • 7
  • 13
  • Just set a request attribute and forward to the JSP the same way as you did in doGet()? – BalusC Nov 30 '15 at 12:37
  • @BalusC: I thought of that but couldn't figure out what to put in the HTML since I want a static text initially and then change it from the servlet. So couldn't really decide where to put the EL like I did for the img. Sorry for sounding a bit befuddled. – The-Droidster Nov 30 '15 at 12:42

1 Answers1

1

I gather that your whole question ultimately boils down to something like this:

I would like to show a static default value when an EL expression does not evaluate to anything.

In that case, use the empty operator in a conditional expression.

<h1>${empty title ? 'Hello World' : title}</h1>

Then it's just a matter of below line in servlet in case you'd like to change it.

request.setAttribute("title", "Not a Hello World");

You could even keep it entirely in the view side given that you're basically interested in the value of the request parameter with name option.

<h1>${param.option eq 'apples' ? 'Yay, Apples!' : param.option eq 'oranges' ? 'Yay, Oranges!' : 'Hello World'}</h1>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • +1 for summarizing the question beautifully. And yes it worked, that is exactly what I was looking for. I have been going in circles as to how the ternary EL operator should be used. You explained it wonderfully. More points for mentioning the possibility of keeping it all on the view side, though I'll prefer the servlet implementation for now since this is a learning exercise for me. Just one thing, is it possible to mix EL and text ?I tried: **

    ${empty title ? 'Hello, Bootstrap!' : 'Yay, 'title' it is!'}

    **, but that led to status 500 : JSP error. Can you point out to me the mistake?
    – The-Droidster Nov 30 '15 at 13:22
  • Thank You! The new EL 3.0 **+=** operator worked like a charm. @BalusC: You're my servlet/jsp guru from this point on. :D – The-Droidster Nov 30 '15 at 13:27