1

I want to send some HTML form data to a program I've written in Java. I'm intending to use AJAX to send the data and add some classes to my Java-code to have it behave as an http-server. So far I've always used php for things like these, but I'm new to both AJAX and setting up http servers. I've looked on-line for guides but most pages are either not similar enough to what I'm doing or just too confusing for a beginner like me.

Can someone show me a basic example of (1) sending a form element with AJAX and (2) retrieving that data in Java? Even a link to a decent guide (that show both) would be appreciated.

I know there are examples out there that show how to do (1), but I can't seem to test the client without a working server, and vice versa.

I'm also unsure of which server solution to implement in Java. I've ready guides suggesting Jetty, others Simple, and then others suggesting websocket is the way forward. I'm undecided of how to proceed.

As for (2), I assume that you'd have to somehow extract the data from the http request? How to do this?

I'm not looking for a full solution, nor a specific answer. Just something to get me started (be it code or a guide) but that's relative to what I want to do. Cheers.

7
  • Have you created your java application that'll handle your ajax request? Commented Apr 23, 2015 at 12:44
  • @yellen, no I haven't yet. The reason is two-fold: I'm not sure if I need ajax or websocket for this, and I'm also not sure of how to handle that request. The guides out there show how to setup a server but not how to extract the data from the request. My biggest issue is that it seems that you cannot simply just work on, say, the client first and then figure out how the server will work. It seems you need to figure out how to do them both at the same time since both "sides" are so dependent on each other. Commented Apr 23, 2015 at 12:46
  • If you are intending to use Java then you're going to have to create a java web project that will be deployed in your application server/ container. And you'll need to write services that you can invoke ajax. There are so many options available. Commented Apr 23, 2015 at 12:50
  • 1
    howtodoinjava.com/2013/05/29/… Commented Apr 23, 2015 at 12:52
  • 1
    howtodoinjava.com/restful-web-service Commented Apr 23, 2015 at 12:53

1 Answer 1

3

Since you are new to Java application, I recommend you Java Servlets, which is the most basic type of java server.

Here is all of my example code, considering AJAX to server, and retriving the data in Java servlet.

You can deploy the program on Tomcat yourself, but it's more easy to use some IDEs (Intellij IDEA, Eclipse, NetBeans...).

Here's my code. These are all you need, nothing more.

Servlet.java

import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;


public class Servlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException
    {
        String text = request.getParameter("text");  //Retrieve data
        PrintWriter out = response.getWriter();
        out.println("Hello, you've typed" + text);
    }
}

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: pwwpche
  Date: 2014/4/21
  Time: 14:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>

    <title></title>
    <script type="text/javascript" src="bootstrap/js/jquery.min.js"></script>
    <script type="text/javascript">
        function submit(){
            var msgContent = document.getElementById("txtMessage").value;
            $.ajax({
                url: "myServlet",
                data: {
                    text : msgContent
                },
                success: function (data) {
                    alert(data);
                },
                error: function (data) {
                    console.log(data);
                }
            });
        }
    </script>
</head>
<body>
<form action="myServlet">
    <input type="text" name="mytext" id="mytext">
    <input type="submit" onclick="submit()">
</form>
</body>
</html>

web.xml

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

    <servlet-mapping>
        <servlet-name>servlet</servlet-name>
        <url-pattern>/myServlet</url-pattern>
    </servlet-mapping>
</web-app>

As for how to deploy, you can save your energy by making IDE to do the work. Due to some network problems, I can't upload image here :( But you can Google with keyword "java servlet tomcat" or something. There are tons of tutorials on the web :)

Sign up to request clarification or add additional context in comments.

1 Comment

I didn't try your code exactly as you provide it, but Ajax and Servlet did the trick. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.