Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8018401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:07:12+00:00 2026-06-04T21:07:12+00:00

I am trying to create a light weight web interface using embedded jetty to

  • 0

I am trying to create a light weight web interface using

embedded jetty to host the server,
and a simple html code with a java script to display the main page, since the pages are not static depending on a condition I would need to make a call to a java code. The sample html code is as follows :

 <body>
<script type="text/javascript">
 function myfunction(frm)
{
  var opt=frm.option.value;
  alert("option is"+frm.option.value);
   // call a java method depending on the value of opt
  frm.option.value="";
 }
</script>
    <h1 style="text-align: center;">Agent Management Interface</h1>
    <ol>

    </ol>
    <form name="management_form">
            Enter Option: <input type="text" id="optiontb" name="option">
            <input type="button" onclick="myfunction(this.form)" value="submit">
    </form>
</body>
</html>

I am not sure if this question has been posted earlier but I am wondering is there a method to pass a variable to a user defined java code and obtain return values and display them on the web interface?

I read around a little bit I am not using any external tool, developing using eclipse, using applets is not an option. I would like to the web interface as light weight as possible.

Edit 2:

I have updated the html file with the suggestions as given below, but this does not seem to be working for me. I suspect it is because of the way I have written the handlers, the log messages are :

2012-05-28 16:02:53.753:DBUG:oejs.AsyncHttpConnection:async request (null null)@16471729 org.eclipse.jetty.server.Request@fb56b1
2012-05-28 16:02:53.754:DBUG:oejs.Server:REQUEST / on org.eclipse.jetty.server.nio.SelectChannelConnector$SelectChannelHttpConnection@bc8e1e@127.0.0.1:8080<->127.0.0.1:47830
2012-05-28 16:02:53.756:DBUG:oejs.Server:RESPONSE /  304
2012-05-28 16:02:53.757:DBUG:oejs.AsyncHttpConnection:async request (null null)@16471729 org.eclipse.jetty.server.Request@fb56b1

The code written for the handlers is as follows

System.setProperty("org.eclipse.jetty.util.log.DEBUG","true"); 
    Server server = new Server(8080);
    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setDirectoriesListed(true);
    resource_handler.setResourceBase(args.length == 2?args[1]:".");
    resource_handler.setWelcomeFiles(new String[]{ "index.html" });
    System.out.println("serving " + resource_handler.getBaseResource());

    ContextHandler context0 = new ContextHandler();
    context0.setContextPath("/senddata");
    Handler handler0=new HelloHandler();
    context0.setHandler(handler0);

    ContextHandlerCollection contexts = new ContextHandlerCollection();
    contexts.setHandlers(new Handler[]{context0});

    HandlerCollection handlersc = new HandlerCollection();
    handlersc.setHandlers(new Handler[]{resource_handler,new DefaultHandler(), contexts});
    server.setHandler(handlersc);
    server.start();
    server.join();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:07:15+00:00Added an answer on June 4, 2026 at 9:07 pm

    The technique that you’re looking for is AJAX. Since JavaScript is client side code and Java is code that runs on the server, the only way to get data to or from the server is to make an HTTP request to the server to request the data.

    Below is an example from the Mozilla Developer Center page on Getting Started with AJAX:

     <script type="text/javascript">
    
      // this is the function that will make the request to the server
      function makeRequest(url) {
        var httpRequest;
    
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
          httpRequest = new XMLHttpRequest();
        } else if (window.ActiveXObject) { // IE
          try {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
          } 
          catch (e) {
            try {
              httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {}
          }
        }
    
        if (!httpRequest) {
          alert('Giving up :( Cannot create an XMLHTTP instance');
          return false;
        }
    
        // here we set the onreadystatechange event to invoke "alertContents"
         // this is called when the response returns. This is a "callback" function.
    
        httpRequest.onreadystatechange = alertContents;
        httpRequest.open('GET', url);
    
        // this starts the send operation
        httpRequest.send();
      }
    
      // here is the callback handler
      function alertContents() {
        if (httpRequest.readyState === 4) {
          if (httpRequest.status === 200) {
    
            // since the response from the Jetty server is <h1>Hello World</h1>
             // the alert should fire with that markup
            alert(httpRequest.responseText);
    
        } else {
          alert('There was a problem with the request.');
        }
      }
    }
    
    // this is your function, with the makeRequest call. 
    function myfunction(frm)
    {
        var opt=frm.option.value;
        alert("option is"+frm.option.value);
        // call a java method depending on the value of opt
        frm.option.value="";
    
        // call makerequest here with your data
        makeRequest('/senddata?value=' + frm.option.value); 
    }
    
    </script>
    

    While the above code will allow you to make the HTTP request from the browser, you’ll need a servlet in your Java application in order to receive the request, process the request, and return the response back to the browser.

    The Embedded Jetty site has an example of how to create a Handler, which you can use to examine an HTTP request, process it, and return a response. I modified the example slightly to extract a query parameter that you would pass in through the AJAX request:

    public class HelloHandler extends AbstractHandler
    {
        public void handle(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response) 
            throws IOException, ServletException
        {
            // the value passed in from the client side
            String value = request.getParameter("value");
    
            // do stuff with that here
    
            // return a response
            response.setContentType("text/html;charset=utf-8");
            response.setStatus(HttpServletResponse.SC_OK);
            baseRequest.setHandled(true);
    
            // this gets sent back down to the client-side and is alerted
            response.getWriter().println("<h1>Hello World</h1>");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a web user interface for a Java application. The user
I have been trying to create a simple point light in HLSL for a
I'm looking for an extremely light-weight lightbox for jQuery. i'm trying to create my
I'm trying to create a simple interface to automate the creation of a flash
I am trying to dynamically create HTML elements using the DOM. The elements are
I'm trying to create a lightweight, self-contained webservice using the spark microframework ( http://www.sparkjava.com/readme.html
I'm trying create a gui using Tkinter that grabs a username and password and
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Ok so I am trying create a login script, here I am using PHP5
trying to create simple package with one procedure: CREATE OR REPLACE PACKAGE PACKAGE1 AS

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.