This is my first attempt at HTML interaction with Java. I Understand that I need to use a Java servlet and an HTML index. I am doing this over a local GlassFish 4 server.
What I would like is a simple user input to my Java EE (Eclipse v4.5 (Mars)) project. I am not positive how to do this. I have watched tutorials and read multiple guides, but none explicitly explain the interaction. (This is why I know I need a servlet.)
So here is my setup thus far, and I have no clue where to go from here.
Java servelet
package servletPackage1;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GetUserInputServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
String userInput = req.getParameter("UserInput");
}
}
Again I am not positive that this is even set up correctly, but how do I get the input from my index file?
Here is my index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>DSS</title>
</head>
<body>
<form action="DATASEARCH/GetUserInputServlet" method="get">
Search for details:<br>
<input type="text" name="text/plain" value="Enter search criteria">
<br>
<br><br>
<input type="submit" class="btn-success btn-md" style="margin-right:5px" id="T1" value="Submit">
</form>
</html>
I have found out that I need to have my deployment descriptor (web.xml file). I have created one, but I am still receiving a 404 upon submission. The XML is below:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>DATASEARCH</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>Index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
This will be connected to a much larger project (which is finished and works perfectly) to search across a database. I only need to get the user input across a web application. NEW TO HTML also.
For an answer I am looking for:
What is wrong with what I have done? (I am getting a 404 error when I click my submit button)
How do I get the input into a Java variable casted as a string?
For now I will only be using ONE servlet as I only need access to a single user input from a single page. I do not understand this servlet mapping as well as I should, but I can not find information on how to map. Any help would be appreciated.