java4all@1986 java. Powered by Blogger.

Exception Handling In java?

>> Tuesday, June 21, 2011

Exception Handling:   Exception, that means exceptional errors. Actually exceptions are used for handling errors in programs that occurs during the program execution. During the program execution if any error occurs and you want to print your own message or the system message about the error then you write the part of the program which generate the error in the try{} block and catch the errors using catch() block. Exception turns the direction of normal flow of the program control and send to the related catch() block. Error that occurs during the program execution generate a specific object which has the information about the errors occurred in the program.

In the following example code you will see that how the exception handling can be done in java program. This example reads two integer numbers for the variables a and b. If you enter any other character except number ( 0 - 9 ) then the error is caught by NumberFormatException object. After that ex.getMessage() prints the information about the error occurring causes.

   
import java.io.*;

public class exceptionHandle{
  public static void main(String[] args) throws Exception{
  try{
  int a,b;
  BufferedReader in = 
  new BufferedReader(new InputStreamReader(System.in));
  a = Integer.parseInt(in.readLine());
  b = Integer.parseInt(in.readLine());
  }
  catch(NumberFormatException ex){
  System.out.println(ex.getMessage() 
  + " is not a numeric value.");
  System.exit(0);
  }
  }
}
The output will as follow
1).If we enter are trying to write a value other than integer it throws error
I.e Entering a as value it throws,For input string: "a" is not a numeric value.

Read more...

What is meant by Project Architecture?

Project Architecture :
                         
     Project architecture means nothing but ,the when the client sends the request to the server and server generates the response.How the functionality works is nothing but project Flow.

Here in above image we are using 3-Tier architecture.

Project Architecture means like take the below example.

There are like 2-Tier,3-Tier and so on n-Tier architecture.

So generally for any project we are using 3-Tier Architecture 1.Client(Browser),
2.Server (Tomcat) 3.Back end Database(Oracle).

So explain like Requests will be taken from the Client and
it will be process in Tomcat server the server will contact
the database and process the request.The request will be get
back to the service method and inturn it will send to the
client nothing but browser.

so in this way you have to tell about your project.
 

Read more...

Struts Dispatch Action Example?

>> Monday, June 20, 2011





Dispatch Action 
               DispatchAction provides a mechanism for grouping a set of related functions into a single action,
thus eliminating the need to create seperate actions for each functions. In this example we will see
how to group a set of user related actions like add number , subtract number , multiply number and
dividing numbers  into a single action called UserAction.

The class UserAction extends org.apache.struts.actions.DispatchAction. This class does not provide an
implementation of the execute() method as the normal Action class does. The DispatchAction uses the
execute method to manage delegating the request to the individual methods based on the incoming request parameter.
For example if the incoming parameter is "method=add", then the add method will be invoked. These methods should
have similar signature as the execute method.

Let us look the Example:

Step1:
   Take a jsp named as Calculate.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">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<title>Insert title here</title>
</head>
<body>
 <h2>Do your Calculations</h2>
 <html:form action="/calc.do">
 <table>
 <tr>
  <td width="60%">
  Enter First Number:<html:text property="fno"></html:text><br>
  Enter Second Number:<html:text property="sno"></html:text><br>
  
  
  <html:submit property="method" value="add">add</html:submit>
  <html:submit property="method" value="sub">sub</html:submit>
  <html:submit property="method" value="mul">mul</html:submit>
  <html:submit property="method" value="div">div</html:submit>
  </td>
  <td><img src="http://www.accountservonline.com/images/calculator_djk3.jpg" height="100" width="150"></img></td> 
 </tr>

 </table>
 </html:form>
 
 </body>
</html>

 Take a FormBean Class i.e CalculateForm.java
package com.bhaskar.dispatch;

import org.apache.struts.action.ActionForm;

public class CalculateForm extends ActionForm{
 
 /**
  * 
  */
 private static final long serialVersionUID = 1L;
 private int fno;
 private int sno;
 
 
 public int getFno() {
  return fno;
 }
 public void setFno(int fno) {
  this.fno = fno;
 }
 public int getSno() {
  return sno;
 }
 public void setSno(int sno) {
  this.sno = sno;
 }
 
 

}
Take An Action Class Which extends DispatchAction i.e CalculateAction.java
package com.bhaskar.dispatch;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class CalculateAction extends DispatchAction {
  
 private int a = 0;
 private int b = 0;
 private int c1 = 0;
 Calculate c;
 ArrayList al=null;
 
 public String key="";
 
 public ActionForward add(ActionMapping maping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
  CalculateForm cf = (CalculateForm)form;
  a = cf.getFno();
  b = cf.getSno();
  c1 = (a+b);
  Calculate c=new Calculate();
  c.setResult(c1);
  c.setFno(a);
  c.setSno(b);
  al=new ArrayList();
  al.add(c);
  HttpSession session=request.getSession();
  session.setAttribute("add", al);
  return maping.findForward("add1");
 }
 
 public ActionForward sub(ActionMapping maping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
  CalculateForm cf = (CalculateForm)form;
  
  a = cf.getFno();
  b = cf.getSno();
  if(a>b){
   c1 = (a-b);
   Calculate c=new Calculate();
   c.setResult(c1);
   c.setFno(a);
   c.setSno(b);
   al=new ArrayList();
   al.add(c);
   HttpSession session=request.getSession();
   session.setAttribute("sub", al);
   
  }
  
  else{
   
   c1 = (b-a);
   Calculate c=new Calculate();
   c.setResult(c1);
   c.setFno(a);
   c.setSno(b);
   al=new ArrayList();
   al.add(c);
   HttpSession session=request.getSession();
   session.setAttribute("sub", al);
  }
  
  return maping.findForward("sub1"); 
 }
 
 public ActionForward mul(ActionMapping maping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
  CalculateForm cf = (CalculateForm)form;//It is  a Dto Class
  a = cf.getFno();
  b = cf.getSno();
  c1 = (a*b);
  Calculate c=new Calculate();
  c.setResult(c1);
  c.setFno(a);
  c.setSno(b);
  al=new ArrayList();
  al.add(c);
  HttpSession session=request.getSession();
  session.setAttribute("mul", al);
  return maping.findForward("mul1"); 
 }
 
 public ActionForward div(ActionMapping maping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
  CalculateForm cf = (CalculateForm)form;
  a = cf.getFno();
  b = cf.getSno();
  if(b==0){
   c1 = 0;
   Calculate c=new Calculate();
   c.setResult(c1);
   c.setFno(a);
   c.setSno(b);
   al=new ArrayList();
   al.add(c);
   HttpSession session=request.getSession();
   session.setAttribute("div", al);
  }
  else{
  c1 = (a/b);
  Calculate c=new Calculate();
  c.setResult(c1);
  c.setFno(a);
  c.setSno(b);
  al=new ArrayList();
  al.add(c);
  HttpSession session=request.getSession();
  session.setAttribute("div", al);
  }
  
  return maping.findForward("div1"); 
 }
}


Take A Dto class DTO means Data Transfer Object(It is Same A bean class)  i.e Calculate.java
package com.bhaskar.dispatch;
public class Calculate {
 private int fno;
 private int sno;
 private int result;
 public int getFno() {
  return fno;
 }

 public void setFno(int fno) {
  this.fno = fno;
 }

 public int getSno() {
  return sno;
 }

 public void setSno(int sno) {
  this.sno = sno;
 }

 

 public int getResult() {
  return result;
 }

 public void setResult(int result) {
  this.result = result;
 }


 

}


Configuring Form uri,actionForm And action Class And Respective Jsp Pages in struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>

    <form-beans>
        <form-bean name="Calculator" type="com.bhaskar.dispatch.CalculateForm"/>     
    </form-beans>
    
    <global-exceptions></global-exceptions>

    <global-forwards></global-forwards>
   //If you notice the signature of the add, sub,mul and div  methods are 
similar to the execute method except the name. The next step is to 
create an action mapping for this action handler. The request parameter 
name is specified using the parameter attribute. Here the request 
parameter name is method.

    <action-mappings>
        <action  parameter="method" name="Calculator" path="/calc" scope="session" type="com.bhaskar.dispatch.CalculateAction">
            <forward name="add1" path="/add.jsp" />
            <forward name="sub1" path="/sub.jsp" />
            <forward name="mul1" path="/mul.jsp" />
            <forward name="div1" path="/div.jsp" />
            
        </action>
    </action-mappings>
    <message-resources parameter="application"/>
</struts-config>

First the output looks is shown below

  
add.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">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<logic:present name="add" scope="session">
<table cellpadding="1" cellspacing="1">
<tr>
<th>FIRST NUMBER</th>
<th>SECONDNUMBER</th>
<th>TOTAL</th>
</tr>
<logic:iterate id="c" name="add" >
<tr>
<td><bean:write name="c" property="fno"/></td>
<td><bean:write name="c" property="sno"/></td>
<td><bean:write name="c" property="result"/></td>
</logic:iterate>
</table>
</logic:present>
</body>
</html>

sub.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">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<logic:present name="sub" scope="session">
<table cellpadding="1" cellspacing="1">
<tr>

<th>FIRST NUMBER</th>
<th>SECONDNUMBER</th>
<th>TOTAL</th>
</tr>
<logic:iterate id="c" name="sub" scope="session">
<tr>
<td><bean:write name="c" property="fno"/></td>
<td><bean:write name="c" property="sno"/></td>
<td><bean:write name="c" property="result"/></td>
</logic:iterate>
</table>




</logic:present>
</body>
</html>
mul.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">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<logic:present name="mul" scope="session">
<table cellpadding="1" cellspacing="1">
<tr>

<th>FIRST NUMBER</th>
<th>SECONDNUMBER</th>
<th>TOTAL</th>
</tr>
<logic:iterate id="c" name="mul" scope="session">
<tr>
<td><bean:write name="c" property="fno"/></td>
<td><bean:write name="c" property="sno"/></td>
<td><bean:write name="c" property="result"/></td>
</logic:iterate>
</table>




</logic:present>
</body>
</html>
div.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">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<logic:present name="div" scope="session">
<table cellpadding="1" cellspacing="1">
<tr>

<th>FIRST NUMBER</th>
<th>SECONDNUMBER</th>
<th>TOTAL</th>
</tr>
<logic:iterate id="c" name="div" scope="session">
<tr>
<td><bean:write name="c" property="fno"/></td>
<td><bean:write name="c" property="sno"/></td>
<td><bean:write name="c" property="result"/></td>
</logic:iterate>
</table>




</logic:present>
</body>
</html>






OUTPUT AS SHOWN BELOW IMAGES ONE AFTER ONE
 Entering Values Are 5,2
When we click on add button the outout is shown in below image





     When we click on subtract button the output shown below

        
When we click on "mul"  button the output vis shown below

         
When we click on "div"   the output is shown below
    


Read more...

JavaScripy wait while paging loading display image?

<html>
<head>
</head>

<body onLoad="init()">
<div id="loading" style="position:absolute; width:100%; text-align:center; top:300px;"><img src="webpageloading.jpeg" border=0></div>
<script>
var ld=(document.all);

var ns4=document.layers;
var ns6=document.getElementById&&!document.all;
var ie4=document.all;

if (ns4)
 ld=document.loading;
else if (ns6)
 ld=document.getElementById("loading").style;
else if (ie4)
 ld=document.all.loading.style;

function init()
{
if(ns4){ld.visibility="hidden";}
else if (ns6||ie4) ld.display="none";
}
</script>
<center><img src="webpageloading.jpeg"></center>
</body>
</html>

 }
  function mousehandler(e){
  var myevent = (isNS) ? e : event;
  var eventbutton = (isNS) ? myevent.which : myevent.button;
 alert(&quot;right is disabled due some security reasons&quot;);
    if((eventbutton==2)||(eventbutton==3)) return false;
 }
 document.oncontextmenu = mischandler;
 document.onmousedown = mousehandler;
 document.onmouseup = mousehandler;
  &lt;/script&gt;
 &lt;/head&gt;

 &lt;body&gt;
  
 &lt;

Read more...

FaceBook Login

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

Total Pageviews

STATCOUNTER

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP