7 Jun 2013

Servlet Example

Creating a login page using servlet -
Concepts of Dispatcher 
First you need to create a html page

<html>
<head>
<title></title>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
</head>
<body>
username:<input type="text" name="username"><br>
Password:<input type="text" name="password"><br>
<input type="submit" value="submit">
</body>
</html>

Now create servlet
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "dispatcherservlet", urlPatterns = {"/dispatcherservlet"})
public class dispatcherservlet extends HttpServlet {

    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String ab=request.getParameter("username");
            String bc=request.getParameter("password");
            out.println(ab);
            out.println(bc);
            if(ab.equals("root")&& bc.equals("om"))
            {
                out.println("successful login");
                
            }
                else
            {
                out.println("loging failure");
                RequestDispatcher rd=request.getRequestDispatcher("path of any file");
                rd.forward(request, response);
            }
                 
        } finally {            
            out.close();
        }
    }

How to create a login page for any website

Login Page in Html 

<html>
<head>
<title></title>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
</head>
<body>
username:<input type="text" name="username"><br>
Password:<input type="text" name="password"><br>
<input type="submit" value="submit">
</body>
</html>



Output:
 Username:
Password:

20 Feb 2013

C/C++ programs for boundary fill with 4 connect


C/C++ programs for boundary fill with 4 connect 

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void b_fill(int x,int y,int newc,int boundc)
{
          int g;
          g=getpixel(x,y);
          if((g!=boundc)&&(g!=newc))
          {

                   putpixel(x,y,newc);
                   delay(1);
                   b_fill(x+1,y,newc,boundc);
                   //b_fill(x-1,y,newc,boundc);
                   b_fill(x,y-1,newc,boundc);
                    b_fill(x,y+1,newc,boundc);     
          }
}

void main()
{
          int gd=DETECT,gm;
          initgraph(&gd,&gm,"C:\\tc\\bgi");
          rectangle(50,50,150,150);
          b_fill(51,51,GREEN,WHITE);
          rectangle(150,150,250,250);
          b_fill(151,151,RED,WHITE);
          rectangle(250,250,350,350);
          b_fill(251,251,BLUE,WHITE);
          getch();
}

OUTPUT:

7 Feb 2013

C/C++ program for printing a pattern of an ellipse using bresenham algorithm and implementation of floodfill and setfillstyle


/* Program to Draw an Ellipse using Bresenham Algorithm  and implementation of floodfill and setfillstyle */
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#include <graphics.h>
#include<dos.h>
//void ellipseMidpoint(float, float, float, float);
void drawEllipse(float, float, float, float);
void ellipseMidpoint(float xc, float yc, float rx, float ry)
{
float rxSq = rx * rx;
float rySq = ry * ry;
float x = 0, y = ry, p;
float px = 0, py = 2 * rxSq * y;
drawEllipse(xc, yc, x, y);
//Region 1
p = rySq - (rxSq * ry) + (0.25 * rxSq);
while (px < py)
{
x++;
px = px + 2 * rySq;
if (p < 0)
p = p + rySq + px;
else
{
y--;
py = py - 2 * rxSq;
p = p + rySq + px - py;
}
drawEllipse(xc, yc, x, y);
delay(30);
}
//Region 2
p = rySq*(x+0.5)*(x+0.5) + rxSq*(y-1)*(y-1) - rxSq*rySq;
while (y > 0)
{
y--;
py = py - 2 * rxSq;
if (p > 0)
p = p + rxSq - py;
else
{
x++;
px = px + 2 * rySq;
p = p + rxSq - py + px;
}
drawEllipse(xc, yc, x, y);
delay(30);
}
}
void drawEllipse(float xc, float yc, float x, float y)
{
putpixel(xc+x, yc+y, WHITE);
putpixel(xc-x, yc+y, WHITE);
putpixel(xc+x, yc-y, WHITE);
putpixel(xc-x, yc-y, WHITE);
}
void main()
{
float xc, yc, rx, ry;
int gd = DETECT, gm,i;
initgraph(&gd, &gm, "C:\\tc\\bgi");
ellipseMidpoint(200, 200, 60, 70);
ellipseMidpoint(300,300,60,70);
     
for(i=1;i<=10;i++)
{
setfillstyle(i,i+2);
floodfill(200,200,getmaxcolor());
delay(200);
setfillstyle(i,i+1);
floodfill(300,300,getmaxcolor());
delay(200);
}


setfillstyle(9,BLUE);
floodfill(1,1,getmaxcolor());

getch();
}

OUTPUT


6 Feb 2013

c/c++ Program for printing a circle using midpoint circle algorithm

/* Program in c/c++ for printing a circle using midpoint circle algorithm */


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void draw_circle(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,2);
putpixel(xc+x,yc-y,2);
putpixel(xc-x,yc+y,2);
putpixel(xc-x,yc-y,2);
putpixel(xc+y,yc+x,2);
putpixel(xc-y,yc+x,2);
putpixel(xc+y,yc-x,2);
putpixel(xc-y,yc-x,2);
}
void main()
{
int x=0,y,xc=200,yc=200,gd=DETECT,gm,p,r=100;
initgraph(&gd,&gm,"C:\\tc\\BGI");
y=r;
p=1-r;
while(x<y)
{
draw_circle(xc,yc,x,y);
x++;
if(p<0)
{
p=p+2*x+1;
}
else
{
y--;
p=p+2*(x-y)+1;
}
draw_circle(xc,yc,x,y);
}
getch();
}

OUTPUT 

Program in c/c++ for printing a pattern using dda algorithm


/* Program in c/c++ for printing a pattern using dda algorithm */
#include<graphics.h>
#include<conio.h>
void add(int x1,int y1,int x2,int y2)
{
  int dx,dy,m,s;
  float xi,yi,x,y;
   dx = x2 - x1;
    dy = y2 - y1;

    if (abs(dx) > abs(dy))
    {
s = abs(dx);
    }
    else
    {
s = abs(dy);
     }
    xi = dx / (float) s;
    yi = dy / (float) s;

    x = x1;
    y = y1;

    putpixel(x1, y1, 2);
delay(3);

    for (m = 0; m < s; m++)
{
x += xi;
y += yi;
putpixel(x, y, 2);
delay(3);
    }

}
void main ()
{
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"C://tc/BGI");
add(20,50,250,50);
add(250,50,250,200);
add(250,200,20,200);
add(20,200,20,50);
add(40,70,80,70);
add(80,70,80,100);
add(80,100,40,100);
add(40,100,40,70);
add(190,70,230,70);
add(230,70,230,100);
add(230,100,190,100);
add(190,100,190,70);
add(115,150,155,150);
add(155,150,155,180);
add(155,180,115,180);
add(115,180,115,150);
getch();
}


OUTPUT:

DDA algorithm coding for printing a triangle


/* Program in c for printing a triangle with the help of DDA aglorithm */
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>

void drawline(int x1,int y1,int x2,int y2)
{
  int dx,dy,m,s;
  float xi,yi,x,y;
   dx = x2 - x1;
    dy = y2 - y1;

    if (abs(dx) > abs(dy))
s = abs(dx);
    else
s = abs(dy);

    xi = dx / (float) s;
    yi = dy / (float) s;

    x = x1;
    y = y1;

    putpixel(x1, y1, WHITE);

    for (m = 0; m < s; m++)
{
x += xi;
y += yi;
putpixel(x, y, WHITE);
    }

}
void main()
{

    int gd = DETECT, gm = DETECT, x1, y1, x2, y2;

    clrscr();

    initgraph(&gd, &gm, "C:\\tc\\bgi");
    drawline(20,100,70,30);
    drawline(70,30,120,100);
    drawline(20,100,120,100);
    getch();
}

OUTPUT