-6

Whilst working on our coursework for Computer Science, we have had to change from Java to JavaScript in HTML due to a server in-capability. Therefore, I have spent all my research into Java and have a fully working computer program in Java but with this new problem, my whole project needs to be made into JavaScript (or better, HTML)... I have had a brief working with HTML & Dreamweaver so I know how the UI etc. but I need help making a Search Bar that has variables. Previously, it was coded as

(search bar here)
if search == example:
     System.out.println("You have chosen example")
etc

but now we have had to convert everything to HTML and I have no clue on how to make the if statements in this new language...

Any help is welcomed!

1 Answers1

0

In your case you need to consider Java Servlet technology.

You will need to have a servlet on the server (servlet-container), and an HTML page, with JavaScript code, that makes a GET request with parameters to this servlet.

Note, that you need to encode the search string, before passing it to the server.

This servlet receives request from the client (HTML page), does the search, and prints results to the output stream.

Your JavaScript code receives server response and modifies HTML page to show the search results.

To implement client/server interaction via asynchronous requests (AJAX) consider jQuery.

Here is an example, how to make a GET request to the server: https://api.jquery.com/jquery.get/

plain sample:

$.get( "ajax/test.html", function( data ) {
  $( ".result" ).html( data );
  alert( "Load was performed." );
});

And there is an example, how to read GET request params in the servlet:

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

    String param1 = request.getParameter("param1");
    String param2 = request.getParameter("param2");

}
Community
  • 1
  • 1