Handling Choice in HTML form in Servlets

Handling choice in Servlets
The following illustrates handling choice in HTML form in Servlets. Till now we have seen working with data in the text box now let us see how to work out with the drop downs. A drop down or a choice looks like this.



Here are all the files..

Folder structure

Project folder: C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps

selectser
           |_ index.html
           |_ WEB-INF
                             |_ web.xml
                             |_ classes
                                          |_ SelectExample.java
                                          |_ SelectExample.class

HTML File(s)

index.html


 <html>
    <head>
        <title>Select a language</title>
    </head>
   
    <body>
        <form action="http://localhost:8080/selectser/ss" method="post">
            <select name="lang">
                <option value="C">C</option>
                <option value="C++">C++</option>
                <option value="Java">Java</option>
                <option value="Python">Python</option>
            </select>

            <input type="submit" value="SUBMIT"/>
        </form>
    </body>
</html>

Deployment Descriptor - web.xml


<web-app>
    <servlet>
        <servlet-name>sser</servlet-name>
        <servlet-class>SelectExample</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>sser</servlet-name>
        <url-pattern>/ss</url-pattern>
    </servlet-mapping>
</web-app>

Servlet Programs


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class SelectExample extends HttpServlet
{
    public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
    {
   
    // Get value first
    String val=req.getParameter("lang");
   
    // Get writer from res
    PrintWriter pw=res.getWriter();
   
    pw.println("<h1> You selected "+val+"</h1>");
    }
}

Working? Let's see.

The following output shows that it is working.

HTML Form containing dropdown. Select some value and click SUBMIT
The HTML form

The value enclosed in <h1> tag is printed here
Print what is selected

Explaining things..

index.html

<select name="lang"> The <select> does it all. It encloses a set of options to be selected. It has a name which can be used to retrieve which option is selected.

<option value="C">C</option> The <option> defines an item in the drop down. The value of this is returned when we call the getParameter("lang") provided this option is selected and submitted. Here value is for the programmers to get and the name that is enclosed between the opening and closing of the option tag is for the user to see.

web.xml

<url-pattern>/ss</url-pattern> This is the url pattern. The tomcat observes this and invokes the class associated with it.
<servlet-class>SelectExample</servlet-class> SelectExample is the class associated with the above pattern.

SelectExample.java

String val=req.getParameter("lang"); Get the value of the option that is submitted. Here i have used the lang which is the name you have encountered above. When this is passed, we get the value of the option that is selected.

PrintWriter pw=res.getWriter(); Get the PrintWriter object to write something on the dynamic page for the user.

pw.println("<h1> You selected "+val+"</h1>"); Print the value the user has selected in a <h1> tag.

In this way we can handle a drop down (choice) in HTML form in Servlets. Hope this helps. Feel free to drop a comment.

No comments: