String[]
getParameterValues(String name)
Returns
an array of String objects containing all of the values the given
request parameter has, or null if the parameter does not exist.
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <title>Registration</title> </head> <body> <form method="post" action="/servlet/Registration"> <table id="registrationTable"> <caption>Please fill the details to Register</caption> <tr> <td>User Name:<sup>*</sup></td> <td><input type="text" name="userName" required="required" /></td> </tr> <tr> <td>Mail Id:<sup>*</sup></td> <td><input type="text" name="mailID" required="required" /></td> </tr> <tr> <td>Password:<sup>*</sup></td> <td><input type="password" name="passwd" required="required" /></td> </tr> <tr> <td>Confirm Password<sup>*</sup></td> <td><input type="password" name="confPasswd" id="confPasswd" required="required" /></td> </tr> <tr> <td>Hobbies<sup>*</sup></td> <td> <input type="checkbox" name="hobby" value="Cricket" />Cricket<br> <input type="checkbox" name="hobby" value="Movies" />Movies<br> <input type="checkbox" name="hobby" value="FootBall" />FootBall<br> <input type="checkbox" name="hobby" value="Chess" />Chess<br> <input type="checkbox" name="hobby" value="Driving" />Driving<br> </td> </tr> <tr> <td><input type="submit" value="Register" /></td> <td><input type="reset" value="Clear Data" /></td> </tr> </table> </form> </body> </html>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> <servlet> <servlet-name>Registration</servlet-name> <servlet-class>Registration</servlet-class> </servlet> <servlet-mapping> <servlet-name>Registration</servlet-name> <url-pattern>/Registration</url-pattern> </servlet-mapping> </web-app>
Registration.java
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 Registration extends HttpServlet { @Override public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{ String[] values = req.getParameterValues("hobby"); PrintWriter out = res.getWriter(); res.setContentType("text/html"); String str1 = "<html>" + "<head>" + "<title> Hello World </title>" + "</head>" + "<body><h1>Your Hobbies are <br />"; String str2 = "</h1> </body>" + "</html>"; StringBuilder str3 = new StringBuilder(); for(String s : values){ str3.append(s +"<br />"); } out.println(str1 + " "+str3.toString() +" " + str2); } }
No comments:
Post a Comment