In this section we will discuss how we can select various colors and displayed it to the user.
Color Selection in servlet
In this section we will discuss how we can select various colors and displayed it to the user.
In the example given below I have created a html page onto which I make an options of choosing colors and a submit button on which by clicking the values we have selected will gone to the server. Further on the server the values which we had selected into the html page will be fetched by the HttpServletRequest's object getParameterValues() method.
Example :
colorSelection.html
<html> <head> <title>color's List</title> </head> <body> <form action="/colorSelection/ColorSelection"> <select name="clr" multiple> <option>Brown</option> <option>Red</option> <option>Pink</option> <option>Blue</option> <option>White</option> </select> <p><input type="submit" name="submit"/></p></form> </body> </html>
ColorSelection.java
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class ColorSelection extends HttpServlet implements Servlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter pw = res.getWriter(); HttpSession ssn = req.getSession(); String clrs[] = req.getParameterValues("clr"); if (ssn != null) { ssn.setAttribute("color", clrs); ssn.setMaxInactiveInterval(60); } pw.println("<html><body bgcolor =cyan>"); for (int i = 0; i < clrs.length; i++) { pw.println("colors selected :" + clrs[i] + "<br>"); } pw.println("<form action = /colorSelection/GetColor>"); pw.println("<input type = submit name= submit/>"); pw.println("</form></body></html>"); } }
GetColor.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; import javax.servlet.http.HttpSession; public class GetColor extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter pw = res.getWriter(); HttpSession ssn = req.getSession(false); if (ssn == null) { pw.println("No session is available"); pw.println("We are creating a session for you. Creating....."); ssn = req.getSession(); } else { String getColors[] = (String[]) ssn.getAttribute("color"); pw.println("<html><body bgcolor = cyan>"); for (int i = 0; i < getColors.length; i++) { pw.println("colors selected by you are : " + getColors[i] + "<br>"); } pw.println("<html><body>"); } } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>ColorSelection</display-name> <servlet> <servlet-name>ColorSelection</servlet-name> <servlet-class>ColorSelection</servlet-class> </servlet> <servlet> <servlet-name>GetColor</servlet-name> <servlet-class>GetColor</servlet-class> </servlet> <servlet-mapping> <servlet-name>ColorSelection</servlet-name> <url-pattern>/ColorSelection</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>GetColor</servlet-name> <url-pattern>/GetColor</url-pattern> </servlet-mapping> </web-app>
Output :
When you will execute the above example you will get the following outputs :
This is the first page where a color is to be selected.
This is the page for selecting the color.
This page displays, like it shows that the following colors are selected before finalize the color list.
This page is displaying the final colors list selected by you.
[ 0 ] Comments