0

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:

  1. What is wrong with what I have done? (I am getting a 404 error when I click my submit button)

  2. 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.

8
  • I also just noticed that my servlet is NOT being identified by eclipse as a servlet (Is this automatic? or do i need to tell the project that it is a servlet?) If there is any clarification needed... don't hesitate to ask Commented May 22, 2017 at 12:45
  • Please post you web.xml. 404 means mapping is incorrect. so on submit you are trying to send something to the <host>:<port>[<context path>]DATASEARCH/GetUserInputServlet url which cannot be found. Commented May 22, 2017 at 12:47
  • I might sound stupid here, but i have no XML? i didn't write any? is this analagous to my .jsp? Commented May 22, 2017 at 12:51
  • 1
    The how your servlet is registered? How app server knows which URL should be processed by which servlet? Commented May 22, 2017 at 12:55
  • Great question i suppose that is something i will have to research because i don't know. :) Also, i am noticing that my servlet is not identified as a servlet... is this done automatically or do i have to tell the project that it is intended ot be a servlet? Commented May 22, 2017 at 12:57

2 Answers 2

1

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.

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

Comments

1

In from action you need to add only servlet name and in input type name must be same in both place Example In html

In servlet

String abc=request.getparameter("abc");

And if do not won't to used web.xml than you can also used annotations in servlet before servlet class name like

@webservlet

Comments

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.