Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest 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:

 

 

 

                         

» View all related tutorials
Related Tags: c http com ide session list stl server orm resources process form binding transactions https object io connection processes make

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

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

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 | Flex 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.