5 Replies - 587 Views - Last Post: 15 August 2017 - 10:40 AM

#1 roparzhhemon  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 06-December 15

POST Request ignoring submitted data

Posted 15 August 2017 - 03:13 AM

I have a very basic Java EE project, consisting of one servlet Main.java and one
associated jsp file (their contents are below).

The problem I'm encountering is that, when in debugging mode I fill the fields in the form and
press the "Submit" button, I can see that the request.getParameterNames is empty, when it
should contain "left", "right", etc.

Any help appreciated.

Contents of Main.java file :

package com.miniCalculatrice.servlets;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Main
 */
@WebServlet("/Main")
public class Main extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Main() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		this.getServletContext().getRequestDispatcher
		("/WEB-INF/main.jsp").
		forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Enumeration<String> enu=request.getParameterNames();
		boolean see=enu.hasMoreElements();
		for (; enu.hasMoreElements();)/>/> {
			   String elt=enu.nextElement();
		       System.out.println(elt);
		}       
		doGet(request, response);
	}

}



Contents of main.jsp file :

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Calculatrice</title>
</head>
<body>
<p>
</p>
<form method="post" action="main" enctype="multipart/form-data">
            <input type="text" name="left" id="left" />
              <select multiple>
  								<option value="plus">+</option>
  								<option value="minus">-</option>
  								<option value="times">x</option>
  								<option value="divided">/</option>
             </select> 
            <input type="text" name="right" id="right" />
        <input type="submit" value="Calculer"/>
    </form>
</body>
</html>


Also, the web.xml file of my project is as follows :
<?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" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <servlet>
    <servlet-name>Main</servlet-name>
    <servlet-class>com.miniCalculatrice.servlets.Main</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Main</servlet-name>
    <url-pattern>/main</url-pattern>
  </servlet-mapping>
</web-app>



Is This A Good Question/Topic? 0
  • +

Replies To: POST Request ignoring submitted data

#2 Dankwansere  Icon User is offline

  • D.I.C Regular

Reputation: 82
  • View blog
  • Posts: 256
  • Joined: 09-November 09

Re: POST Request ignoring submitted data

Posted 15 August 2017 - 08:40 AM

The request parameters are being sent fine. I tested the parameters value and names and successfully accessed them in the servlet.

try the following basic test in the servlet

Enumeration<String> paramNames = request.getParameterNames();
		
		while(paramNames.hasMoreElements()){
			System.out.println(paramNames.nextElement());
		}

		String left = request.getParameter("left");
		String right = request.getParameter("right");
		
		System.out.println("Left: " + left);	
		System.out.println("right: " + right);




If you see all your parameter names and values printed to the console as I did, then your code is working fine. By the way in your jsp action change the main to upper case.

action="Main"


Was This Post Helpful? 1
  • +
  • -

#3 roparzhhemon  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 06-December 15

Re: POST Request ignoring submitted data

Posted 15 August 2017 - 09:30 AM

View PostDankwansere, on 15 August 2017 - 03:40 PM, said:

The request parameters are being sent fine. I tested the parameters value and names and successfully accessed them in the servlet.

try the following basic test in the servlet (...)


Thank you for your feedback.
I tested again after reading it, and once again I get an empty Enumeration object when accessing request.getParameterNames ; also request.getParameter("left") and request.getParameter("right") are null on my machine.
I am truly clueless as to why I do not get the expected behaviour on such a simple project.
Perhaps there are items in the Request object that I might take a closer look at in the debugger.
Was This Post Helpful? 0
  • +
  • -

#4 Dankwansere  Icon User is offline

  • D.I.C Regular

Reputation: 82
  • View blog
  • Posts: 256
  • Joined: 09-November 09

Re: POST Request ignoring submitted data

Posted 15 August 2017 - 09:38 AM

if you are receiving null on the parameter values, then double check the JSP file. The request parameters from the JSP is not being reached on the servlet.
Was This Post Helpful? 1
  • +
  • -

#5 roparzhhemon  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 19
  • Joined: 06-December 15

Re: POST Request ignoring submitted data

Posted 15 August 2017 - 10:02 AM

View PostDankwansere, on 15 August 2017 - 04:38 PM, said:

if you are receiving null on the parameter values, then double check the JSP file. The request parameters from the JSP is not being reached on the servlet.


You are absolutely right. I just stopped, it was the "enctype="multipart/form-data" in my form that made everything go wrong. As usual, I hastily copy-pasted another code snippet. It's working fine now, thank you so much
Was This Post Helpful? 0
  • +
  • -

#6 Dankwansere  Icon User is offline

  • D.I.C Regular

Reputation: 82
  • View blog
  • Posts: 256
  • Joined: 09-November 09

Re: POST Request ignoring submitted data

Posted 15 August 2017 - 10:40 AM

Apparently when you use the
"enctype="multipart/form-data"
in your jsp, you will not be able to directly get the parameters in your servlet class by using
request.getParameters()
because the fields are not available as parameters in the request, they are instead included in the stream. So to be able to access those fields you would have to access them in the Stream.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1