Mega Code Archive

 
Categories / Java Tutorial / Servlet
 

Servlet Form Processor CheckBox

import javax.servlet.http.*; import javax.servlet.*; import java.io.IOException; public class MyServlet extends HttpServlet {   public void doPost (HttpServletRequest req, HttpServletResponse res) throws IOException   {     ServletOutputStream out = res.getOutputStream();     res.setContentType("text/html");     out.println("<html><head><title>Basic Form Processor Output</title></head>");     out.println("<body>");     out.println("<h1>Here is your Form Data</h1>");     //extract the form data here     String title = req.getParameter("title");     String name = req.getParameter("name");     String city = req.getParameter("city");     String country = req.getParameter("country");     String tel = req.getParameter("tel");     String age = req.getParameter("age");     // extracting data from the checkbox field     String[] interests = req.getParameterValues("interests");     //output the data into a web page     out.println("Your title is "  + title);     out.println("<br>Your name is "  + name);     out.println("<br>Your city is "  + city);     out.println("<br>Your country is "  + country);     out.println("<br>Your tel is "  + tel);     out.println("<br>Your interests include<ul> ");     for (int i=0;i<interests.length; i++) {     out.println("<li>" + interests[i]);   }   out.println("</ul>");   out.println("<br>Your age is "  + age);     out.println("</body></html>");   } } <?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app>     <servlet><servlet-name>MyServletName</servlet-name>              <servlet-class>MyServlet</servlet-class>                   </servlet>          <servlet-mapping><servlet-name>MyServletName</servlet-name>         <url-pattern>*.do</url-pattern>     </servlet-mapping> </web-app> <html> <head> <title>JSP Unleashed, Chapter 1 - A Basic HTML Form</title> </head> <body> <h1>Please enter your information</h1> <form method="POST" action="formProcessor.do">   Title: <select size="1" name="title">   <option>Mr</option>   <option>Mrs</option>   <option>Miss</option>   <option>Ms</option>   <option>Other</option>   </select><br>   Name: <input type="text" name="name" size="20"><br>   City: <input type="text" name="city" size="20"><br>   Country: <input type="text" name="country" size="20"><br>   Telephone: <input type="text" name="tel" size="20">   <P>Please inform us of your interests:<br>   <input type="checkbox" name="interests" value="Sport">Sport<br>   <input type="checkbox" name="interests" value="Music">Music<br>   <input type="checkbox" name="interests" value="Reading">Reading<br>   <input type="checkbox" name="interests" value="TV and Film">TV and Film</p>   <P>Your age   <input type="radio" name="age" value="25orless" checked>Less than 25   <input type="radio" name="age" value="26to40">26-40   <input type="radio" name="age" value="41to65">41-65   <input type="radio" name="age" value="over65">Over 65</p>   <P><input type="submit" value="Submit"></p> </form> </body> </html>