Basic Form processor : Form « Servlets « Java

Basic Form processor

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="" 
         xsi:schemaLocation="" version="2.4">




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

public class BasicFormProcessor extends HttpServlet {
  public void doPost(HttpServletRequest req, HttpServletResponse res)
      throws IOException {
    ServletOutputStream out = res.getOutputStream();
        .println("<html><head><title>Basic Form Processor Output</title></head>");
    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("<br>Your age is " + age);

Related examples in the same category

1.Servlets Form TextField
2.HTML Forms with Servlets
3.React to user form input Form