Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML

Search:
   Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML Facing Programming Problem? Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java Servlets
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
Select Color
In this program we are going to selected the various color and on the basis of the selection the output will be displayed to the user.
 
 

Select Color

                         

In this program we are going to selected the various color and on the basis of the selection the output will be displayed to the user. 

To make this program firstly we need to make one html page. Inside the page we will have one select option in which we will have our colors. We will also have a submit, clicking on which the values we have entered will be transferred to the server. 

On the server we will create a session. The values which we have entered in the html form will be retrieved by the getParameterValues() of the request object. It returns the array of String. We will check the condition if  there is any session available or not. If yes then we will set the attribute by using the setAttribute() method of the HttpSession object. The attribute we have set will be retrieved by the getAttribute method of the HttpSession object in the next page and the value will be displayed on the browser by the PrintWriter object. 

The code of the program is given below:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Select the list of colors</title>
</head>
<body>
	<form action = "/ServletProject/ColorPage">
		<select name = "colors" size = 5 multiple>
			<option selected>Green</option>
			<option>Red</option>
			<option>Yellow</option>
			<option>Blue</option>
			<option>Black</option>
		</select>
		<input type = "submit" name = "submit">
	</form>
</body>
</html>

 

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
* Servlet implementation class for Servlet: ColorPage
*
*/
public class ColorPage extends javax.servlet.http.HttpServlet implements 
javax.servlet.Servlet {
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public ColorPage() {
super();


/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet
                   (HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
HttpSession session = request.getSession();
String colors[] = request.getParameterValues("colors");
if(session!=null)
{
session.setAttribute("color",colors);
session.setMaxInactiveInterval(60);
}
pw.println("<html><body bgcolor =cyan>");
for(int i = 0; i<colors.length; i++)
{
pw.println("The selected colors are" + colors[i]+ "<br>");
}
pw.println("<form action = /ServletProject/GetColors>");
pw.println("<input type = submit name= submit)>");
pw.println("</form></body></html>");


/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, 
            HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException {
// TODO Auto-generated method stub

}

 

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
* Servlet implementation class for Servlet: GetColors
*
*/
public class GetColors extends HttpServlet {
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public GetColors() {
super();


/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(
HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
HttpSession session = request.getSession(false);
if(session == null)
{
pw.println("No session is available");
pw.println("We are creating a session for you. Creating.....");
session = request.getSession();
}
else
{
String getColors[] = (String[])session.getAttribute("color");
pw.println("<html><body bgcolor = cyan>");
for(int i= 0; i<getColors.length;i++)
{
pw.println("The selected colors are " + getColors[i] + "<br>");
}
pw.println("<html><body>");
}


/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(
HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException {
// TODO Auto-generated method stub

}

The output of the program is given below:

 

 

 

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

5 comments so far (
post your own) View All Comments Latest 10 Comments:

this site is very nice 2 learn anything

Posted by dilip on Thursday, 10.2.08 @ 19:21pm | #80847

can any please post the answer for the diffarence between doGet() and doPost() please it is very urgent

Posted by pavan on Wednesday, 08.29.07 @ 09:44am | #24396

Hi,
Im unable to run the above 2 applications.It shows the errors in package statements.and HttpServlets also. can u please tell me the reason y it should happend.
Thanks & reards,...
ShivaRamakrishna v,...

Posted by shiva ramakrishna on Thursday, 07.26.07 @ 14:22pm | #21984

Hi,
Im unable to run the above 2 applications.It shows the errors in package statements.and HttpServlets also. can u please tell me the reason y it should happend.
Thanks & reards,...
ShivaRamakrishna v,...

Posted by shiva ramakrishna on Thursday, 07.26.07 @ 14:21pm | #21982

this is excellent to serve the who seeking the problems

Posted by sambashivarao on Wednesday, 04.4.07 @ 16:15pm | #13331

Latest Tutorials:
Styles in HTML
Style Tags Used in HTM
Check Box in HTML
Action Submit Html
Cascading Style Sheet(
Horizontal Rule Attrib
Horizontal Rule in HTM
Show Hyperlink in HTML
Unordered Lists
Paragraph in HTML
Password Field in HTML
Radio Buttons in HTML
Set Background Colors
Table & the Border att
Text Area in HTML
Text Field in HTML
Use of Text Field
Table Caption in HTM
J2ME Servlet Example
J2ME Cookies Example
J2ME Frame Animation2
J2ME Frame Animation
J2ME RMS Read Write
J2ME Record Data Base
J2ME Audio Record
J2ME Record Listener
J2ME Text Box Example
J2ME Timer Animation
J2ME Vector Example
J2ME Video Control Exa
J2ME Event Handling Ex
J2ME HashTable Example
J2ME Icon MIDlet Examp
J2ME Image Item Exampl
J2ME Image Example
J2ME Item State Listen
J2ME Key Codes Example
J2ME KeyEvent Example
J2ME Label Example
J2ME Random Number
J2ME Read File
J2ME RMS Sorting Examp
J2ME Timer MIDlet Exam
Custom Item in J2ME
Creational Design Patt
Design Patterns
Throwing Run time exce
Grid in Echo3
Creating Table in Echo
JPA Introduction
Java bigdecimal toBigI
Java bigdecimal shortV
Java bigdecimal shortV
Java bigdecimal signum
Java bigdecimal stripT
Java bigdecimal subtra
Java bigdecimal subtra
Java bigdecimal toBigI
Java bigdecimal toEngi
Java bigdecimal toPlai
Java bigdecimal toStri
Java bigdecimal ulp ex
Java bigdecimal unscal
Java bigdecimal valueO
Java bigdecimal valueO
Java bigdecimal valueO
Java bigdecimal setSca
Java bigdecimal setSca
Java bigdecimal scaleB
Java bigdecimal scale
Java bigdecimal round
Java bigdecimal remain
Java bigdecimal remain
Java bigdecimal precis
Java bigdecimal pow me
Java bigdecimal pow ex
Java bigdecimal plus m
Java bigdecimal plus e
Java bigdecimal negate
Java bigdecimal negate
Java bigdecimal multip
Java bigdecimal multip
Java BigDecimal movePo
Java bigdecimal movePo
Java bigdecimal min ex
Java bigdecimal max ex
CheckBox component in
Visibility of Componen
Loading delay componen
Simple input applicati
Opening a new window i
Hello World in Echo3 f
Use of Local Inner cla
JSP bean set property
Java Method Synchroniz
Java Method Return Val
JAVA Method Wait
JDBC vs ORM
Java BigDecimal divide
Java BigDecimal divide
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.