Showing posts with label Appache. Show all posts
Showing posts with label Appache. Show all posts

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>");

Login page using JSP

JSP simple log in page

jsp code that implement the business logic

<HTML>
    <HEAD>
        <TITLE>
            Welcome..
        </TITLE>
        <%@page language="java" contentType="text/html"%>
    </HEAD>
    <BODY><center><h2>
        <% if((request.getParameter("name").equals("sscollege"))&&(request.getParameter("pass").equals("college"))) 
            {
                  out.println("Login successfully...");
             }
            else
            {
                  out.println("Invalid username or password");
             }   
        %>
        </h2>

JSP custom tag with body

 Custom tag JSP

Steps for creating custom tag get from here

code for creating jsp custom tag with body

Tag implementation


TagBody.java

package st;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;


public class TagBody extends TagSupport
{

     public int doStartTag() throws JspException
     {
        try{
               JspWriter out = pageContext.getOut();
               out.println( "<table border=1>");
           
                      out.println("<tr><td>  ");
                  

            }
        catch(Exception ex)
        {
        }
        return EVAL_BODY_INCLUDE;
     }

    public int doEndTag() throws JspException

Custom tag using JSP without body

JSP custom tag without body

steps

first create a folder named as st in WEB-INF\classes directory

Then create java file named as HellTag.java  inside st

paste the following on  HellTag.java file

package st;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;

public class HellTag extends TagSupport
{
    public int doStartTag()
    {
        try{
            JspWriter js=pageContext.getOut();
            js.print("Hello");
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        return SKIP_BODY;
    }
}

compile the HelloTag.java file by following manner
javac HellTag.java -classpath "C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\jsp-api.jar"

Then create st.tld file inside the WEB-INF directory