Tuesday, April 9, 2013

How to return a List in Ajax using servlet


selectuser.js


var xmlHttp;

function showUser(str)
{ 
    
xmlHttp=GetXmlHttpObject()
    
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="TestServlet"
//alert(url=url+"?q="+str);
url=url+"?q="+str

url=url+"&sid="+Math.random()
    
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
 } 
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();

 }
catch (e)
 {

 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
// JavaScript Document


index.jsp


<html>
<head>
<script src="selectuser.js"></script>
</head>

<body>
<center>

<form><br>
<br>
Select a ID: <select name="users"
    onchange="return showUser(this.value);">
    <option name=select>plz select</option>

    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select></form>
<p>
<div id="txtHint"><b></b></div>
</center>
</body>
</html>



TestServlet

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;



      public TestServlet()
      {
            super();
      }

            protected void doGet(HttpServletRequest request,
                  HttpServletResponse response) throws ServletException, IOException {

            String str = request.getParameter("q");
            System.out.println("data="+str);
            PrintWriter out = response.getWriter();
            int counter = 1;
            if (str.equals("1")) {
                 
                  out.print("<table border=1>");
                  out.println("<tr >");
                  out.println("<td >EMPLOYEE DETAILS</td>");
                  out.println("</tr >");
                  out.println("<tr >");
                  out.println("<td ><b>ID</b></td><td>1</td>");
                  out.println("</tr >");
                  out.println("<tr >");
                  out.println("<td ><b>Name</b></td><td>Himanshu</td>");
                  out.println("</tr >");
                  out.println("<tr >");
                  out.println("<td ><b>DOB</b></td><td  >1987</td>");
                  out.println("</tr >");
                  out.println("<tr >");
                  out.println("<td ><b>DOJ</b></td><td>1986</td>");
                  out.println("</tr >");
                  out.println("<tr >");
                  out.println("<td ><b>Location</b></td><td>UP</td>");
                  out.println("</tr >");
                  out.println("<tr >");
                  out.println("<td ><b>Email</b></td><td>swain.himansu@gmail.com</td>");
                  out.println("</tr >");
                  out.println("<tr >");
                  out.println("<td ><b>Mobile</b></td><td>8010999606</td>");
                  out.println("</tr >");
                  out.println("<tr >");
                  out.println("<td ><b>Address</b></td><td  >UP India</td>");
                  out.println("</tr >");
                  out.println("</table>");
            }
           
            else if(str.equals("2"))
            {
                  out.println("<h3>There is no information on ID=2</h3>");
            }
            else if(str.equals("3"))
            {
                  out.println("<h3>There is no information on ID=3</h3>");
            }
            }

}


web.xml


<?xml version="1.0" encoding="UTF-8"?>
  <display-name>Test2</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>TestServlet</display-name>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>TestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/TestServlet</url-pattern>
  </servlet-mapping>
</web-app>

Monday, April 8, 2013

How to return a List in Ajax using JSP

selectuser.js


var xmlHttp;

function showUser(str)
{
   
xmlHttp=GetXmlHttpObject()
   
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="getselect.jsp"
//alert(url=url+"?q="+str);
url=url+"?q="+str

url=url+"&sid="+Math.random()
   
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 {
 document.getElementById("txtHint").innerHTML=xmlHttp.responseText
 }
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();

 }
catch (e)
 {

 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
// JavaScript Document


index.jsp


<html>
<head>
<script src="selectuser.js"></script>
</head>

<body>
<center>

<form><br>
<br>
Select a ID: <select name="users"
    onchange="return showUser(this.value);">
    <option name=select>plz select</option>

    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select></form>
<p>
<div id="txtHint"><b></b></div>
</center>
</body>
</html>


getselect.jsp

<link href="css/stylesheet.css" rel="stylesheet" type="text/css">

<% String email=request.getParameter("q");%>

<html>
<body >
<center>
<fieldset style="width:350px;" > <legend >&nbsp;EMPLOYEE DETAILS&nbsp;</legend>
<table  width=320 border=0 cellspacing="1" cellpadding="0" >
<%int counter=1; %>
<%
if(email.equals("1")){%>
<tr class="headbgcolor" align="center"><td colspan="2" class="headtitle">EMPLOYEE DETAILS</td></tr>
<tr class="bodybgcolor"><td class="texttitle" ><b>ID</b></td><td class="texttitle" >1</td></tr>
<tr class="bodybgcolor"><td class="texttitle" ><b>NAME</b></td><td class="texttitle" >Himanshu</td></tr>
<tr class="bodybgcolor"><td class="texttitle" ><b>DOB</b></td><td class="texttitle" >1897</td></tr>
<tr class="bodybgcolor"><td class="texttitle" ><b>DOJ</b></td><td class="texttitle" >1987</td></tr>
<tr class="bodybgcolor"><td class="texttitle" ><b>LOCATION</b></td><td class="texttitle" >odisha</td></tr>
<tr class="bodybgcolor"><td class="texttitle" ><b>EMAIL</b></td><td class="texttitle" >swain.himansu@gmail.com</td></tr>
<tr class="bodybgcolor"><td class="texttitle" ><b>MOBILE</b></td><td class="texttitle" >990909090</font></td></tr>
<tr class="bodybgcolor"><td class="texttitle" ><b>ADDRESS</b></td><td class="texttitle" >Odisha,India</td></tr>
<tr><td colspan=2><hr><b>END OF <%=counter%> TABLE</b><hr></td></tr>
<% counter++;}%>
</table>
</fieldset>

</font>
</center>
</body>
</html>

Wednesday, April 3, 2013

How to upload file in JSP

index.jsp



<HTML>
<HEAD>
<TITLE>Display file upload form to the user</TITLE>
</HEAD>
<BODY>
<FORM ENCTYPE="multipart/form-data" ACTION="upload.jsp" METHOD=POST>
<br>
<br>
<br>
<center>
<table border="0" bgcolor=#ccFDDEE>
      <tr>
            <center>
            <td colspan="2" align="center"><B>UPLOAD THE FILE</B>
            <center>
            </td>
      </tr>
      <tr>
            <td colspan="2" align="center"></td>
      </tr>
      <tr>
            <td><b>Choose the file To Upload:</b></td>
            <td><INPUT NAME="file" TYPE="file"></td>
      </tr>
      <tr>
            <td colspan="2" align="center"></td>
      </tr>
      <tr>
            <td colspan="2" align="center"><input type="submit"
                  value="Send File"></td>
      </tr>
      <table>
            </center>
            </FORM>
</BODY>
</HTML>


upload.jsp




<%@ page import="java.io.*"%>
<%
      String saveFile = "";
      String contentType = request.getContentType();
      if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
            DataInputStream in = new DataInputStream(request.getInputStream());
            int formDataLength = request.getContentLength();
            byte dataBytes[] = new byte[formDataLength];
            int byteRead = 0;
            int totalBytesRead = 0;
            while (totalBytesRead < formDataLength) {
                  byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
                  totalBytesRead += byteRead;
            }
            String file = new String(dataBytes);
            saveFile = file.substring(file.indexOf("filename=\"") + 10);
            saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
            saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
            int lastIndex = contentType.lastIndexOf("=");
            String boundary = contentType.substring(lastIndex + 1, contentType.length());
            int pos;
            pos = file.indexOf("filename=\"");
            pos = file.indexOf("\n", pos) + 1;
            pos = file.indexOf("\n", pos) + 1;
            pos = file.indexOf("\n", pos) + 1;
            int boundaryLocation = file.indexOf(boundary, pos) - 4;
            int startPos = ((file.substring(0, pos)).getBytes()).length;
            int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
            saveFile = "C:/himanshu/" + saveFile;
            File ff = new File(saveFile);
            FileOutputStream fileOut = new FileOutputStream(ff);
            fileOut.write(dataBytes, startPos, (endPos - startPos));
            fileOut.flush();
            fileOut.close();
%><Br>
<table border="2">
      <tr>
            <td><b>You have successfully upload the file by the name of:</b>
            <%
                  out.println(saveFile);
                  }
            %>
            </td>
      </tr>
</table>





Oracle JRE will no longer trust MD5-signed code by default

















How to Calculate the Future Investment Value of Investment.

Below java program for calculate future investment value. Here calculate s using the formula futureInvestmentValue =investmentAmount x (...