JSP & Servlets
1.0.0
1.0.0
  • Setting up
  • Pre-requisites
  • Servlet Part 1
  • Servlet Part 2
  • Understanding the servlet
  • Servlet XML Configuration
  • POST method and passing parameters
  • Passing more parameters
  • Understanding GET and POST
  • Request, Session and Context Part -1
  • Request , Session and Context Part - 2
  • Understanding init, service and ServletConfig
  • Hello JSP
  • Understanding JSP
  • JSP Page directives
  • HttpServletRequest Path Decoding
  • Scopes in JSP and the PageContext Object
  • Understanding MVC pattern
  • PART -1 Writing an MVC app
  • JSTL
    • For-each
  • CRUD-1
  • Project Work
    • Create a Simple Java Web Application Using Servlet, JSP/JSTL and JDBC/Transaction
  • Locale Filter
  • Action Plan
    • Schedule
    • Hit webservice
Powered by GitBook
On this page
  • How to get values from radio and select ?
  • Step 1 : change index.html
  • Step 2: Change the controller (xmlservlet):

Was this helpful?

Passing more parameters

PreviousPOST method and passing parametersNextUnderstanding GET and POST

Last updated 6 years ago

Was this helpful?

How to get values from radio and select ?

Step 1 : change index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form method="post" action="xmlservletpath">
		First Name : <input name="userName" />
		Last Name :  <input name="lastName" /> 
		<br>
		<input type="radio" name="profession" value = "Developer"/>Developer
		<input type="radio" name="profession" value = "Manager"/>Manager
		<br>
		<select name = "countries">
			<option value="India">India</option>
			<option value="Pak">Pak</option>
			<option value="USA">USA</option>
		</select>
		<br>
		<select name = "cities" multiple="3">
			<option value="Delhi">Delhi</option>
			<option value="Mumbai">Mumbai</option>
			<option value="Chennai">Chennai</option>
			<option value="Delhi">Punjab</option>
			<option value="Mumbai">Agra</option>
			<option value="Chennai">Mathura</option>
		</select>
		
		<input type="submit" />
	</form>
</body>
</html>

Step 2: Change the controller (xmlservlet):

Use the below to get multiple values from select option.

String[] cities = request.getParameterValues("cities");
package com.gs.controller;

import java.io.IOException;
import java.io.PrintWriter;

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

public class XmlServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("Hello from Get method of XML servlet");
		response.setContentType("text/html");
		String userName = request.getParameter("userName");
		PrintWriter printWriter = response.getWriter();
		printWriter.println("Hello from GET method " + userName);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("Hello from POST method of XML servlet");
		response.setContentType("text/html");
		String userName = request.getParameter("userName");
		String lastName = request.getParameter("lastName");
		String profession = request.getParameter("profession");
		String countries = request.getParameter("countries");
		String[] cities = request.getParameterValues("cities");
		PrintWriter printWriter = response.getWriter();
		printWriter.println("Hello from POST method \n");
		printWriter.println("First Name is \n " + userName);
		printWriter.println("Last Name is \n" + lastName);
		printWriter.println("Profession is \n " + profession);
		printWriter.println("Country is \n " + countries);
		printWriter.println("Cities are \n ");
		for (String s : cities) {
			printWriter.println(s+" ,");
		}

	}

}