Program designed by Mr RamanjaneyuluDayinaboyina JEE project manager
In this program we are going to
select the various colors 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:
కామెంట్లు లేవు:
కామెంట్ను పోస్ట్ చేయండి