Reputation: 59491
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.
Upvotes: 1
Views: 3093
Reputation: 621
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 :)
Upvotes: 3