0

I'm using EL(expression language) tags to display values from my java code in my JSP. Unfortunately the only way to update those values seems to be using forms, which send httpRequests to servlets. Also the user needs to click a button to send this form. I've been researching for days now how to automatically update those values, without forcing the user to do anything. But I couldn't find a solution for this, that doesn't involve scriplets, which are deprecated.

Is there a way to accomplish this? If yes, a small example would help me a lot, since I'm pretty new to the web world.

Fuby
  • 66
  • 10
  • 1
    Firstly, scriptlets are not going to help you regardless of whether they are deprecated or not. JSP whether using scriptlets or whatever are executed on the server whereas what you need is a client side (JavaScript) solution. Firstly learn how to make an ajax request using a button click and then look at making the same request using a poller. Jquery may help in both regards. See for example http://www.programming-free.com/2012/08/ajax-with-jsp-and-servlet-using-jquery.html – Alan Hay May 20 '16 at 18:14
  • 1
    What you need is a push from Server, so client will know that there are some changes, instead of client polling server all the time, to check if there are any updates. This post can be useful for you:http://stackoverflow.com/questions/5064372/sending-data-from-server-to-clients-using-ajax – Amit Mahajan Jun 01 '16 at 22:17
  • That would be the next step to reduce traffic, thanks! – Fuby Jun 03 '16 at 12:28

1 Answers1

0

Thanks to Alan Hay's comment I've finally managed to find an solution. You need to download this magical thing called "ajax" and then RC your Project -> Propertys -> Java Build Path -> Libraries -> Add External Jar -> Select the previously downloaded Ajax.zip.

After that I've come up with the following code. The code updates anything that I write in the input field, every 3 seconds.

JSP

<script>
src = "http://code.jquery.com/jquery-latest.js"

var ajax_call = function() {
  $(document).ready(function() {
    var username = $('#user').val();
    $.get('UpdateListsServlet', {
      user: username
    }, function(responseText) {
      $('#TEST').text(responseText);
    });
  });
};

var interval = 3000; //Update every 3s

setInterval(ajax_call, interval);
</script>
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="style.cs" />
</head>

<body>

  <form>
    <input type="text" id="user" />
    <div id="TEST"></div>
  </form>

</body>

</html>

Servlet

 package ajax;

 import java.io.IOException;
 import javax.servlet.ServletException;
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;

 public class UpdateListsServlet extends HttpServlet 
 {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse  response) throws ServletException, IOException 
    { 
       String name = null;
       name = request.getParameter("user");

       if(request.getParameter("user").toString().equals(""))
           name="Hello User";

           response.setContentType("text/plain");  
           response.setCharacterEncoding("UTF-8"); 
           response.getWriter().write(name); 
   }

   protected void doPost(HttpServletRequest request, HttpServletResponse 
   response) throws ServletException, IOException {}

}

XML

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">

 <!-- Welcome -->
 <display-name>WebApp</display-name>
 <welcome-file-list>
   <welcome-file>index.html</welcome-file>
 </welcome-file-list>

 <!-- Servlets -->
 <servlet>
   <servlet-name>UpdateListsServlet</servlet-name>
   <servlet-class>ajax.UpdateListsServlet</servlet-class>
 </servlet>
 <servlet-mapping>
   <servlet-name>UpdateListsServlet</servlet-name>
   <url-pattern>/UpdateListsServlet/*</url-pattern>
 </servlet-mapping>

 </web-app>
Fuby
  • 66
  • 10