Saturday, 28 February 2015

Program for jsp forward

index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home</title>
</head>
<body>
<jsp:forward page="welcome.jsp">
<jsp:param value="anant mahale" name="name"/>
</jsp:forward>
</body>
</html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Welcome to welcome to jsp</h2>
 </body>
</html>


Program for jsp include

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home</title>
</head>
<body>
<h2>This is Index page</h2>
<jsp:include page="welcome.jsp"></jsp:include>

</body>
</html>


welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Today's Date</title>
</head>
<body>
<h2> Welcome page included :) </h2>
</body>
</html>




Program for jsp usebean

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Index</title>
</head>
<body>
<form action="welcome.jsp" method="post">
<h3>Enter your user name and password</h3>
Name <input type="text" name="uname"/><br>
password <input type="password" name="pass"/><br>
<input type="submit" value="Register" />
</form>
</body>
</html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Process</title>
</head>
<body>
<jsp:useBean id="t" class="com.getset.Info"></jsp:useBean>
<jsp:setProperty property="*" name="t"/>

Your User Name and password is<br>
<jsp:getProperty property="uname" name="t"/><br>
<jsp:getProperty property="pass" name="t"/>
</body>
</html>

info.java

package com.getset;

public class Info {
   
    String uname,pass;

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

}





First jsp program step by step



1. Install tomcat server on your pc / laptop.
2.       Start eclipse and create new project like, File Menu -> New -> dynamic web project -> write project name. Suppose project name is “First_Pro” 

3.       After entering name of project click on next button, again click on next button, click on check box “generate web.xml deployment descriptor”

  then click on finish button.
4.       Now right click on your project name click on new and select jsp file

5.       After clicking on “Jsp File” as like above image.At the time creation of jsp file make sure your file is place in webcontent folder as shown picture.Name your first index file as index.jsp. and click on finish button

6.       After clicking on finish button your index.jsp file is look like this.

7.       Now edit the readymade code like this 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>First Pro<title>
</head>
<body>
<h1>Its my First JSP program</h1>

</body>
</html>
8.       Save the code file->save OR ctrl+s
9.       To load libraries right click on your project -> build path -> configure build path


After clicking on configure build path, java build path dialog box is shown. In libraries tab -> click on “Add external jars”

In jar selection dialog box go to your “c:\program files\tomcat\lib” and select servelet-api.jar and tomcat-api.jar. The servlet-api.jar need for run servlet programs and tomcat-api.jar need for connect with tomcat. After selecting files click on open button.

After selecting jar files they will appear in your java build path as shown in image after that click on deployment assembly as marked in image 

After clicking on deployment assembly click on apply button.

In web deployment assembly click on add button.


After clicking on add button, new assembly directive dialog box will appear. In that click on java build path entities and click on next button.

After clicking on next button, you will see servlet-api.jar and and tomcat-api.jar. select both files and click on finish button.

Now click on apply and save button.
10.   Now create path -> go to “My Computer” -> properties
Then go to “Advance system settings”

In system properties click on environment variable
In environment variable dialog box click on new button as shown in image
After in “Edit Variable User” click on new button enter “Variable Name” as “path” and “Variable value” as your jdk bin path and its end use ;.; and click on Ok button again Ok button again Ok button.
11.   Now to the run the program right click on your project -> run as -> run on server

12.   After clicking on “Run on Server” select your tomcat server version. After then click on next button.
If in the configure box there are more than one project are available then click on remove all. And click on finish button.
After that your output will shown like this.

Wednesday, 25 February 2015

Applet Calculater Program

/*java code */

import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;

public class Calculater extends Applet implements ActionListener
{
    JTextField txt_show;
    JButton btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_0,btn_add,btn_sub,btn_mul,btn_div,btn_eql;
    String str_get,str_set,str_final,Str_null="";
     int load_add,load_sub,load_mul,load_div,getval;
    int i=0;
  public void init()
  {
          setLayout(null);
       
        txt_show=new JTextField();
        txt_show.setBounds(0,0,500,30);
        add(txt_show);
       
        btn_add=new JButton("+");
        btn_add.setBounds(0,30,100,30);
        btn_add.addActionListener(this);
        add(btn_add);
       
        btn_sub=new JButton("-");
        btn_sub.setBounds(100,30,100,30);
        btn_sub.addActionListener(this);
        add(btn_sub);
       
        btn_mul=new JButton("*");
        btn_mul.setBounds(200,30,100,30);
        btn_mul.addActionListener(this);
        add(btn_mul);
       
        btn_div=new JButton("/");
        btn_div.setBounds(300,30,100,30);
        btn_div.addActionListener(this);
        add(btn_div);
       
        btn_eql=new JButton("=");
        btn_eql.setBounds(400,30,100,30);
        btn_eql.addActionListener(this);
        add(btn_eql);
       
        btn_1=new JButton("1");
        btn_1.setBounds(0,60,100,30);
        btn_1.addActionListener(this);
        add(btn_1);
       
        btn_2=new JButton("2");
        btn_2.setBounds(100,60,100,30);
        btn_2.addActionListener(this);
        add(btn_2);
       
        btn_3=new JButton("3");
        btn_3.setBounds(200,60,100,30);
        btn_3.addActionListener(this);
        add(btn_3);
       
        btn_4=new JButton("4");
        btn_4.setBounds(300,60,100,30);
        btn_4.addActionListener(this);
        add(btn_4);
       
        btn_5=new JButton("5");
        btn_5.setBounds(400,60,100,30);
        btn_5.addActionListener(this);
        add(btn_5);
       
        btn_6=new JButton("6");
        btn_6.setBounds(0,90,100,30);
        btn_6.addActionListener(this);
        add(btn_6);
       
        btn_7=new JButton("7");
        btn_7.setBounds(100,90,100,30);
        btn_7.addActionListener(this);
        add(btn_7);
       
        btn_8=new JButton("8");
        btn_8.setBounds(200,90,100,30);
        btn_8.addActionListener(this);
        add(btn_8);
       
        btn_9=new JButton("9");
        btn_9.setBounds(300,90,100,30);
        btn_9.addActionListener(this);
        add(btn_9);
       
        btn_0=new JButton("0");
        btn_0.setBounds(400,90,100,30);
        btn_0.addActionListener(this);
        add(btn_0);
  }
  public void actionPerformed(ActionEvent ae)
  {
      if(ae.getSource()==btn_0)
    {
        str_get=txt_show.getText();
        str_set="0";
        str_final=str_get.concat(str_set);
        txt_show.setText(str_final);   
    }
    else if(ae.getSource()==btn_1)
    {
        str_get=txt_show.getText();
        str_set="1";
        str_final=str_get.concat(str_set);
        txt_show.setText(str_final);       
    }
    else if(ae.getSource()==btn_2)
    {
        str_get=txt_show.getText();
        str_set="2";
        str_final=str_get.concat(str_set);
        txt_show.setText(str_final);       
    }
    else if(ae.getSource()==btn_3)
    {
        str_get=txt_show.getText();
        str_set="3";
        str_final=str_get.concat(str_set);
        txt_show.setText(str_final);       
    }
    else if(ae.getSource()==btn_4)
    {
        str_get=txt_show.getText();
        str_set="4";
        str_final=str_get.concat(str_set);
        txt_show.setText(str_final);       
    }
    else if(ae.getSource()==btn_5)
    {
        str_get=txt_show.getText();
        str_set="5";
        str_final=str_get.concat(str_set);
        txt_show.setText(str_final);       
    }
    else if(ae.getSource()==btn_6)
    {
        str_get=txt_show.getText();
        str_set="6";
        str_final=str_get.concat(str_set);
        txt_show.setText(str_final);       
    }
    else if(ae.getSource()==btn_7)
    {
        str_get=txt_show.getText();
        str_set="7";
        str_final=str_get.concat(str_set);
        txt_show.setText(str_final);       
    }
    else if(ae.getSource()==btn_8)
    {
        str_get=txt_show.getText();
        str_set="8";
        str_final=str_get.concat(str_set);
        txt_show.setText(str_final);       
    }
    else if(ae.getSource()==btn_9)
    {
        str_get=txt_show.getText();
        str_set="9";
        str_final=str_get.concat(str_set);
        txt_show.setText(str_final);       
    }
    else if(ae.getSource()==btn_add)
    {
        load_add=Integer.parseInt(txt_show.getText());
        txt_show.setText(Str_null);   
            i=1;   
    }
    else if(ae.getSource()==btn_sub)
    {
        load_sub=Integer.parseInt(txt_show.getText());
        txt_show.setText(Str_null);   
            i=2;   
    }
    else if(ae.getSource()==btn_mul)
    {
        load_mul=Integer.parseInt(txt_show.getText());
        txt_show.setText(Str_null);       
            i=3;
    }       
    else if(ae.getSource()==btn_div)
    {
        load_div=Integer.parseInt(txt_show.getText());
        txt_show.setText(Str_null);   
            i=4;   
    }
    else if(ae.getSource()==btn_eql)
    {
        String str1_get=txt_show.getText();
        if(i==1)
        {
           
            int    load_add1=Integer.parseInt(txt_show.getText());
            int add=load_add+load_add1;
            txt_show.setText(""+add);
        }
        else if(i==2)
        {
            int    load_sub1=Integer.parseInt(txt_show.getText());
            int sub=load_sub-load_sub1;
            txt_show.setText(""+sub);
        }
        else if(i==3)
        {
            int    load_mul1=Integer.parseInt(txt_show.getText());
            int mul=load_mul*load_mul1;
            txt_show.setText(""+mul);           
        }
        else if(i==4)
        {
            int    load_div1=Integer.parseInt(txt_show.getText());
            int div=load_div/load_div1;
            txt_show.setText(""+div);
        }
    }
  }
}

/*html code*/

/*

<<HTML>
<HEAD>
</HEAD>
<BODY>
<div >
<APPLET CODE="Calculater.class" WIDTH="800" HEIGHT="800">
</APPLET>
</div>
</BODY>
</HTML>

*/