2

I'm developing a web server using servlet (v3.0) and jetty v9. I have to serve a HTML page but before I do, I need to modify the CSS inline-styles for a couple of elements on the page, dependent on the value of a boolean variable.

I've been looking at JSP tutorials and examples for ages, and I don't feel like I'm any closer to figuring it out. To simplify it, this is what I'm trying to do:

page.jsp: (in /WAR/html)

<html>
    <head>My Page</head>
    <body>
        <p <% style="display:none" if variable is true %>></p>
    </body>
</html>

GetPage.java:

public class GetPage extends HttpServlet {
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        boolean decision = getDecision();
        // get the JSP page and process the JSP with the boolean variable...
        resp.setContentType("text/html");
        resp.getWriter().print(???);
    }
}

I've been using Java for years but never used JSP before. I would have thought this is JSP 101 but I don't understand how it works. Also, my real use case is not too far off that example above. Is JSP overkill for this purpose and if so, is there a better alternative?

Sas
  • 2,473
  • 6
  • 30
  • 47
RTF
  • 6,214
  • 12
  • 64
  • 132
  • 1
    Possible duplicate of http://stackoverflow.com/questions/5414600/pass-data-from-java-servlet-to-jsp – mkobit Sep 25 '14 at 18:26

2 Answers2

4

Without JSP, you can simply write the html from the servlet something like below:

 response.setContentType("text/html");  
 PrintWriter out = response.getWriter();  
 out.println("<html>");
 out.println("<body>");
 if(yourcondition){
   <p style="display:none;"></p>
 }else{
   <p></p>
 }
 out.println("</body>");
 out.println("</html>");

Alternatively with Jquery Ajax(without jsp) you can send ajax request to your servlet and get response from your servlet. This way you don't need to couple your html page writing in your servlet as shown above.

HTML:

public void doGet(HttpServletRequest req, HttpServletResponse resp) 
  throws IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("yourvalue");
 }

<script>
 $(document).ready(function(){
    //sends ajax get request
    $.get( "myServlet", function( data ) {
        //Do your logic with the response
        if(data == "myvalue"){
            $("p").css("display", "none");
         }
    });
 });
</script>

With jsp:

Servlet

public void doGet(HttpServletRequest req, HttpServletResponse resp) 
  throws IOException {
    request.setAttribute("myvalue",val);
    RequestDispatcher rd = sc.getRequestDispatcher("mypage.jsp");
    rd.forward(req, res);
 }

JSP

<p <c:if test="${your condition}">style="display: none;"</c:if>></p>

OR

<p style="display: ${ var == 'myvalue' ? 'none': 'block'};"></p>

Jquery

 var myVal= '${val}'

 if(myVal == "none"){
    $("p").css("display", "none");
 }
Sas
  • 2,473
  • 6
  • 30
  • 47
  • Thanks for the detailed response. However, I definitely want to go the route of using JSP (or any other templating engine). I understand what's going in with the actual replacement syntax in JSP, but it's the server part I don't get. I don't want to write the HTML from the servlet, there's too much HTML. The part I have trouble with is how to load the JSP page from the servlet, pass the variable into some JSP parsing object (presumably) and then returning the final transformed HTML page. I'm looking into the solution to the dup question posted by Mike Kobit – RTF Sep 26 '14 at 18:05
  • yes, that post have enough info to get you started. To brief on how to forward request from servlet to jsp: first you set object you wanna pass into request object, and use getRequestDispatcher(url) to define the target jsp/servlet (you can also forward to other servlets), and finally forward the request. The container will take care of the parsing, etc. Also, I updated my answer with the servlet code. Sometime I found it's easier understand with the code than explanation.. :) – Sas Sep 26 '14 at 20:53
  • How does the framework know that it should use the jsp engine to process the page? Is it simply because of the jsp file extension? – RTF Sep 26 '14 at 22:23
  • Jsp's are actually a servlet. Especial kind of servlets that is compiled during the application startup or on demand by the container. Essentially, these jsp servlets does the println() for you. Instead of you writing them down. Take a look at page-291 here: http://books.google.ca/books?id=WoA3F0UQfvQC&printsec=frontcover#v=onepage&q&f=false – Sas Sep 27 '14 at 01:19
  • I would recommend you reading chapter 7 from "Head First Servlets and JSP" on your spare time if you are curious... :) – Sas Sep 27 '14 at 01:24
2

Servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    request.setAttribute("attrib", true);
    request.getRequestDispatcher("/page.jsp").include(request, response);
}

Jsp

${ attrib ? 'none': 'block'}
mhmxs
  • 267
  • 1
  • 2
  • 13