Thursday, 6 December 2012

Servlet program for change background color

Change background color using servlet

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

public class ColorServlet extends HttpServlet
{
    public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
    {
        res.setContentType("Text/Html");
        String c=req.getParameter("color");
        PrintWriter pw=res.getWriter();
        if(c.equals("red"))
            pw.println("<body BGCOLOR=red>");
        if(c.equals("green"))
            pw.println("<body BGCOLOR=green>");
        if(c.equals("green"))
            pw.println("<body BGCOLOR=green>");

        if(c.equals("blue"))
            pw.println("<body BGCOLOR=blue>");
        if(c.equals("yellow"))
            pw.println("<body BGCOLOR=yellow>");
        if(c.equals("black"))
            pw.println("<body BGCOLOR=black>");
        pw.println("<center><h2>The selected color is:"+c+"</h2></center>");;
        pw.close();
       
    }
}

HTML code

<HTML>
    <HEAD>
        <TITLE>
            Color Change
        </TITLE>
    </HEAD>
    <BODY>
        <FORM method=get action="http://127.0.0.1:8080/examples/servlets/servlet/ColorServlet">
        <CENTER>
            Select Color :
            <select name="color" id="color">
                <option value="red">Red</option>
                <option value="green">Green</option>
                <option value="blue">Blue</option>
                <option value="yellow">yellow</option>
                <option value="black">Black</option>
            </Select>
        <input type="submit" value="Submit">
        </FORM>

    </BODY>

</HTML>

output

background color using servlet
servlet color select

servlet color program
change color using servlet

5 comments: