Home » Java programming language

Reading All parameter's values at a time in JSP

By: Vanka Manikanth, on 22 JAN 2017

A web page may contain number of fields like textbox, checkbox, radio button, dropdown etc for every field you literally need a getParameter method to call its value. So writing getParameter every time is quite obsolete to it makes code larger. So in order to make the code precise and short we are using Enumeration of java.util.* package.


Here we are going to input the values from user. Do have a close look at their defined names.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Values Reading Single Time</title>
	<h1>Reading All Parameter Values by Enumeration</h1>
		<form  action="ReadingAllValues" method="post">
			<label>USER ID</label>
			<input type="text" name="Userid" id="id">

			<label>USER NAME</label>
			<input type="text" name="Username" id="username">

			<select name="Department">
			<option value="">Select</option>
			<option value="Training">Training</option>
			<option value="Marketing">Marketing</option>
			<option value="Development">Development</option>
			<option value="Staff">Staff</option>
			<input type="radio" name="Gender" value="Male">Male
			<input type="radio" name="Gender" value="Female">Female
			<button type="submit">Sign Up</button>


• Define a com.controller package and place ControllerServlet.java

Here we are using Enumeration of java.util package; Firstly we are getting all the parameter names using a while loop and casting them to String. And we are getting all the String array of values with the names which we have got earlier. And we are looping the String array with their length and printing the values correspondingly.

package com.controller;

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

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

public class ControllerServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			PrintWriter out=resp.getWriter();
			//here we are getting the parameter names which are defined in index.jsp
		Enumeration paramNames = req.getParameterNames();
		while(paramNames.hasMoreElements()) { 
			//looping through the parameter names
			String paramName = (String)paramNames.nextElement();
			//here we made type cast to String/which comes as a Object
			out.print("<h1>" + paramName  + " : ");
			//printing all the Parameter names
			String[] paramValue = req.getParameterValues(paramName); 
			//An array of String parameter values are produced for the Parameter Names
			for(int i=0;i<paramValue.length;i++){
				//looping through the values with length
				//if we doesnot loop they print as an Object form
				String it=paramValue[i];
				//making all the values to String
				out.print(it +"</h1>");
				//printing them all


Here we have a declared our ControllerServlet with its mapping.

<?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">




Reading All parameter values at a time in JSP

Reading All parameter’s values in JSP

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.