Showing posts with label JSP. Show all posts
Showing posts with label JSP. Show all posts

Thursday, 6 December 2012

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

Friday, 16 November 2012

Error page in JSP

Implementation of ERROR page in JSP

errorpage.jsp


<%@ page isErrorPage="true"%>
<%@ page import="java.io.*"%>
<html>
     <head>
             <title>Custom Error Page</title>
     </head>    
    <body>
        <h2>Your application has generated an error</h2>
           <h3>Please check for the error given below</h3>
           <b>Exception:</b><br>
           <font color="red"><%= exception.toString()%></font>
</body>
 </html>


errorexample.jsp

<HTML>
    <HEAD>
        <TITLE>
            Login
        </TITLE>
        <%@ page language="java" contentType="text/html" %>
        <%@page errorPage="errorpage.jsp"%>
       
    </HEAD>
    <BODY>
        <FORM METHODE="post" ACTION="errorexample.jsp">
        <CENTER>
        <TABLE>
            <TR>
                <TD>Number1</TD>
                <TD><input type="text" name="n1" id="n1"></TD>
            </TR>   
            <TR>
                <TD>Number2</TD>
                <TD><input type="text" name="n2" id="n2"></TD>
            </TR>
            <TR>
                <TD>Result</TD>
                <TD><input type="text" name="n3" id="n3" value="<%
                String num1=request.getParameter("n1");
                String num2=request.getParameter("n2");
               
                if((num1!=null)&&(num2!=null))
                {
                int nu1=Integer.parseInt(num1);
                int nu2=Integer.parseInt(num2);
               
                out.println(nu1/nu2);
                }
                    %>">
               
                </TD>
            </TR>
            <TR>
                <TD></TD>
                <TD><input type="submit" value="Divide"></TD>
            </TR>   
        </TABLE>
        </FORM>
    </BODY>
</HTML>

 OUTPUT

Jsp program to division
Jsp program to divide a number

JSP error page
JSP error page
 

Display Date and Time Using JSP


JSP Program to display current date and time

<HTML>
    <HEAD>
        <TITLE>
            Date
        </TITLE>
        <%@page import="java.util.*;" %>
    </HEAD>
    <BODY>
        <P>
        <h1><CENTER>Welcome..</BR>
            Todays Date is
        </br>
        <%=new Date()%>
        </h1>
        </P>
    </BODY>
</HTML>

output: 

Date and Time using JSP
Date and Time using JSP