Project Explorer :
index.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Getting all parameter values in servlet</title> </head> <body> <h1>Getting all parameter values in servlet</h1> <form action="parametervalues" method="post"> <b>Name:</b><br/> <input type="text" name="name" placeholder="Enter your name"/><br/> <b>Contact No:</b><br/> <input type="text" name="contactNo" placeholder="Enter your contact no"/><br/> <input type="text" name="contactNo" placeholder="Enter your contact no"/><br/> <input type="text" name="contactNo" placeholder="Enter your contact no"/><br/> <input type="text" name="contactNo" placeholder="Enter your contact no"/><br/> <b>Email:</b><br/> <input type="email" name="email" placeholder="Email address"/><br/> <input type="email" name="email" placeholder="Email address"/><br/> <input type="email" name="email" placeholder="Email address"/><br/> <input type="email" name="email" placeholder="Email address"/><br/> <b> Web site</b><br/> <input type="url" name="website" placeholder="Website"/><br/> <input type="url" name="website" placeholder="Website"/><br/> <input type="url" name="website" placeholder="Website"/><br/> <input type="url" name="website" placeholder="Website"/><br/> <input type="submit" value="Submit"/> </form> </body> </html> |
ParameterValues.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | package com.ebhor.parametervalues; import java.io.IOException; import java.io.PrintWriter; 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; @WebServlet(name = "ParameterValues", urlPatterns = {"/parametervalues"}) public class ParameterValues extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet ParameterValues</title>"); out.println("</head>"); out.println("<body>"); Enumeration<String> parameterNames = request.getParameterNames(); out.println("<h1>Parameter name and values are</h1>"); while (parameterNames.hasMoreElements()) { String parameterName = parameterNames.nextElement(); out.println("<h3>"+parameterName+"</h3>"); String[] values = request.getParameterValues(parameterName); for (String value : values) { out.println( value+"<br/>"); } } out.println("</body>"); out.println("</html>"); } finally { out.close(); } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } } |
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ebhor</groupId> <artifactId>parameterValues</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>parameterValues</name> <properties> <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> <compilerArguments> <endorseddirs>${endorsed.dir}</endorseddirs> </compilerArguments> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.1</version> <executions> <execution> <phase>validate</phase> <goals> <goal>copy</goal> </goals> <configuration> <outputDirectory>${endorsed.dir}</outputDirectory> <silent>true</silent> <artifactItems> <artifactItem> <groupId>javax</groupId> <artifactId>javaee-endorsed-api</artifactId> <version>6.0</version> <type>jar</type> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> |
Result