JasonD JasonD - 1 year ago 36
HTML Question

Getting user input from HTML to Java servlet using Glassfish

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 javaEE (eclipse 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>


EDIT*
So 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. 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 webapplication. NEW TO HTML also.

for an answer i am looking for:
A)What is wrong with what i have done? (i am getting a 404 error when i click my submit button)

B)how do get the input into a java variable caste 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.

Answer Source

You need to add servlet mapping in your deployment descriptor (web.xml)

    <servlet-mapping>
        <servlet-name>GetUserInputServlet</servlet-name>
        <url-pattern>GetUserInputServlet</url-pattern>
   </servlet-mapping>

This means that whenever you go to URL GetUserInputServlet, the control will be transferred to servlet named GetUserInputServlet.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download