RTF
RTF

Reputation: 6484

How to pass values from servlet to html page

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?

Upvotes: 2

Views: 20842

Answers (2)

mhmxs
mhmxs

Reputation: 267

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'}

Upvotes: 2

Sas
Sas

Reputation: 2503

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");
 }

Upvotes: 4

Related Questions