Servlet Form Processor CheckBox : Form « Servlet « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Servlet » Form 
25.3.3.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 resthrows 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 - 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>
  Download:  ServletFormProcessorCheckBox.zip( 1,068 k)
25.3.Form
25.3.1.Servlet Post Form Data to Self Servlet
25.3.2.Get Form Text Field as a Servlet parameter
25.3.3.Servlet Form Processor CheckBox
25.3.4.Servlet Based GuestBook
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.