Good afternoon, ladies and gentlemen!

I have a problem. It is necessary to implement the following functionality.

There is a page with two fields in which you need to enter two numbers, and with a button on which these two numbers should be sent to the server part, which should be in Java. The server must, based on these two numbers, calculate the response and show the client to the browser.

I can write a simple web server that will display information to the browser, but I cannot imagine how to transfer information from the fields to the java program on the server.

I would be very grateful for the indication of the direction where to dig. Thank you for reading to the end)

  • @despc, why write a server from scratch? Take the tomcat. - etki
  • @Etki, raising a volume to process one request isn’t it hard? I usually use a script of 10 lines on JRuby + Sinatra for such tasks. - Nofate

1 answer 1

but how to transfer the information from the fields to java on the server program

How it is done everywhere: via GET or POST-parameters.

Fields on the page are usually attached to the form. The form has a method attribute that indicates which HTTP method to use to send data. Suppose the form fields are named param1 and param2 :

  • if the GET method is specified, the field values ​​will be in the form of address parameters: http://.......?param1=...¶m2=... In this case, on the server side, you will need to access the parameter like this (depending on the selected server): httpRequest.getParameter("param1") ;
  • if the POST method is specified, the field values ​​will be sent in the body of the POST request, packed in the application/x-www-form-urlencoded or multipart/form-data . For example, param1=Some+Value¶m2=%40 . You can get the values ​​by retrieving the request body and decrypting it. Something like URLDecoder.decode(httpRequest.getRequestBody()) .

In general, I recommend to start with the HTTP protocol and its methods. When it comes to the realization that working with the protocol does not depend on the language and in fact you are accessing the same primitives, it will be easier to search for information.

Shl. In order not to engage in cycling on primitive tasks, you can try to use the web server built into the JVM (this is not for serious production, of course):

 HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0); server.createContext("/", new HttpHandler() { @Override public void handle(HttpExchange httpExchange) throws IOException { // тут ваш обработчик запросов httpExchange.sendResponseHeaders(200, 0); httpExchange.close(); } }); server.start(); 

For something serious, you can take, for example, Jetty - it can be easily embedded in any application.

  • Thank you for the direction. Maybe you will tell more about httpRequest. I could not find such a class, only javax.servlet.http.HttpServletRequest, and this is not what I need, as I understand it. Tell me, with the help of which class can I get the parameters passed? - despc
  • @despc, I'll tell you if you tell me what your web server is. - Nofate