postheadericon 15. Assume that we have got three pdf files for the MCA-1 Syllabus, MCA-2 Syllabus and MCA-3 Syllabus respectively, Now write a Servlet which displays the appropriate PDF file to the client, by looking at a request parameter for the year (1, 2 or 3).


Index.html


<html>
<head>
<title>EX-15: WTAD : www.bipinrupadiya.com</title>
</head>
<body>
<center>
<FORM action="/Ex15/bipinrupadiya.blogspot.com">
Select MCA Syllabus : <select size=1 name="MCA">
<option value="MCA-1">MCA1</option>
<option value="MCA-2">MCA2</option>
<option value="MCA-3">MCA3</option>
</select>
<INPUT TYPE="SUBMIT" value="Show" >
</FORM>
</center>
</body>
</html>

Servlet


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;


@WebServlet(
name="WTAD",
urlPatterns={"/Ex15","/bipinrupadiya.blogspot.com"}
)
public class ShowPDF extends HttpServlet
{
  public void doGet(HttpServletRequest request, HttpServletResponse response)throws
ServletException,IOException
{
String sem = request.getParameter("MCA");
response.setContentType("application/pdf");
response.sendRedirect("/"+sem+".pdf");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException
{
doGet(request,response);
}
}


NOTE: Put Three .pdf file on application's root directory (MCA-1.pdf, MCA-2.pdf, MCA-3.pdf)







postheadericon 14. Write a Servlet which displays a message and also displays how many times the message has been displayed (how many times the page has been visited).

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import javax.servlet.annotation.WebServlet;

@WebServlet("/PageVisitCounter")

public class PageVisitCounter extends HttpServlet
{
    public void doGet(HttpServletRequest req , HttpServletResponse res) 
	throws IOException , ServletException
    {
        PrintWriter out = res.getWriter();
		Cookie[] myCookie = req.getCookies();
		boolean found = false;
		int v=0;
        if(myCookie != null)
        {
			for(int i=0; i<myCookie.length;i++)
			{
				if(myCookie[i].getName().equals("pageCount"))
				{     
					v = Integer.parseInt(myCookie[i].getValue());
					v++;     
					Cookie c1 = new Cookie("pageCount",String.valueOf(v));
					res.addCookie(c1);
					out.println("Visit No.:"+v);
					found = true;
					break;
				}				 
			}           
        }
		if(found==false)
		{
			Cookie c1 = new Cookie("pageCount",String.valueOf(v));
            res.addCookie(c1);
            out.println("<h1>Welcome to www.BipinRupadiya.com site</h1><br><br><br>You have done first visit..!");
		}      
    }	
}
NOTE :  Enable Cookie in your browser to run this example.

postheadericon 13. Write a Servlet to display all the attributes available from request and context.


import javax.servlet.*;
import java.util.*;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;

@WebServlet(
name="WTAD",
urlPatterns={"/Ex13","/bipinrupadiya.blogspot.com"}
)
public class RequestAttributes2 extends HttpServlet
{
 
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<br> Server Port : " + request.getServerPort());
        out.println("<br> Server Name : " + request.getServerName());
        out.println("<br> Protocol    : " + request.getProtocol());
        out.println("<br> Character Encoding    : " + request.getCharacterEncoding());
        out.println("<br> Content Type    : " + request.getContentType());
        out.println("<br> Content Length  : " + request.getContentLength());
        out.println("<br> Remote Address  : " + request.getRemoteAddr());
        out.println("<br> Remote Host     : " + request.getRemoteHost());
     
out.println("<table border=1 align='CENTER'><tr bgcolor='#FFAD00'>") ;
out.println("<th>Header Name</th><th>Header Value</th></tr>");

        Enumeration parameters = request.getParameterNames();
        while(parameters.hasMoreElements())
        {
            String ParaName= (String) parameters.nextElement();
            out.println("<tr><td>" + ParaName+"</td>");
            out.println("<td>" + request.getParameter(ParaName)+"</td></tr>");          

        }
out.println("</table><br><br>");
out.println("<table border=1 align='CENTER'><tr bgcolor='#FFAD00'>") ;
out.println("<th>Attribute Name</th><th>Attribute Value</th></tr>");

        Enumeration attributes = request.getAttributeNames();
        while(attributes.hasMoreElements())
        {
            String attName = (String) attributes.nextElement();
            out.println("<tr><td>" + attName+"</td>");
            out.println("<td> " + request.getAttribute(attName)+"</td></tr>");          
        }
out.println("</table>");
    }
    public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}

}

postheadericon 12. Write a Servlet to display parameters available on request.




Index.html

<html>
<head>
<title>EX-12: WTAD : www.bipinrupadiya.com</title>
</head>
<body>

<form action="/Ex12/bipinrupadiya.blogspot.com" method="get">
<br>User:<input type="text" name="usr">
<br>
Password : <input type="text" name="pwd">
<br>
<input type="Submit" value="Login"><input type="reset" value="Clear">
</form>

<body>
</html>



Servlet



import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;

@WebServlet(
name="WTAD",
urlPatterns={"/Ex12","/bipinrupadiya.blogspot.com"}
)

public class readLogin extends HttpServlet
{
  public void doGet(HttpServletRequest request,HttpServletResponse response)
      throws ServletException, IOException
 {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<br><B>USER :</B>: "
                + request.getParameter("usr") +
                "<br><B>Password</B>: "
                + request.getParameter("pwd") +
                 "");
  }
}


output





postheadericon 11. Write a Servlet to display all the headers available from request.


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import javax.servlet.annotation.WebServlet;

/** Showing all request headers of current request. */

@WebServlet(
name="WTAD",
urlPatterns={"/Ex11","/bipinrupadiya.blogspot.com","/RequestHeaders"}
)
public class ShowRequestHeaders extends HttpServlet {
  public void doGet(HttpServletRequest request,HttpServletResponse response)
      throws ServletException, IOException
{
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String title = "GTU : Ex-11 : Showing all Request Headers";
    out.println("<HTML>\n" +
                "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
                "<BODY BGCOLOR=\"#FDF5E6\">\n" +
                "<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" +
                "<B>Request Method: </B>" +
                request.getMethod() + "<BR>\n" +
                "<B>Request URI: </B>" +
                request.getRequestURI() + "<BR>\n" +
                "<B>Request Protocol: </B>" +
                request.getProtocol() + "<BR><BR>\n" +
                "<TABLE BORDER=1 ALIGN=\"CENTER\">\n" +
                "<TR BGCOLOR=\"#FFAD00\">\n" +
                "<TH>Header Name<TH>Header Value");
    Enumeration headerNames = request.getHeaderNames();
    while(headerNames.hasMoreElements()) {
      String headerName = (String)headerNames.nextElement();
      out.println("<TR><TD>" + headerName);
      out.println("    <TD>" + request.getHeader(headerName));
    }
    out.println("</TABLE>\n</BODY></HTML>");
  }


  public void doPost(HttpServletRequest request,HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }
}

output

postheadericon create GenericServlet

Step 1 :  Create .java File using text editor


import javax.servlet.*;
import java.io.*;
import javax.servlet.annotation.WebServlet;
@WebServlet(
  name="WTAD",
  urlPatterns={"/Ex10","/index.html","/BipinRupadiya.blogspot.com"}
)
public class GenricServletDemo extends GenericServlet
{
 public void service(ServletRequest request, ServletResponse response)
  throws ServletException,IOException
 {
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.println("<html><head><title>WTAD</title></head> <body>");
  String str="<h1>
welcome to </h1>
";
  str=str+"<br />www.bipinrupadiya.blogspot.com";
  out.println(str);
          out.println("</body> </html>");
 }
}

postheadericon 9. Write a JavaScript to show a pop up window with a message Hello and background color lime and with solid black border.

Note : Only Internet Explorer support the method window.createPopup(); 


Solution : 1 (Popup)



<html>
<head>
<title>EX-9: WTAD : www.bipinrupadiya.com</title>
<script type="text/javascript">

// createPopup() supported by IE only
function show_popup()
{
var p=window.createPopup();
var pbody=p.document.body;
pbody.style.backgroundColor="lime";
pbody.style.border="solid black 5px";
pbody.innerHTML="<h1> HELLO</h1><br><br><br><br>Click outside the pop-up to close.";
p.show(150,150,400,200,document.body);
}
</script>
</head>

<body>
<input type="button" value="click me" onclick="show_popup()" >
</body>

</html>





Solution : 2 (Window)



<html>
<head>
<title>EX-9: WTAD : www.bipinrupadiya.com</title>
</head>
<script type="text/javascript">
function show_popup()
{
myWindow = window.open("", "mywindow", "width=300,height=100,border:2px solid #ffffff;");
myWindow.moveTo(400, 400);
myWindow.document.write('<h1>HELLO!</h1>');
myWindow.document.bgColor = "#BFFF00";
}
</script>
<body>
<input type="button" value="click me" onclick="show_popup()" >
</body>
</html>

postheadericon 8. Write a JavaScript to find a string from the given text. If the match is found then replace it with another string.





<html>
<head>
<title>EX-8: WTAD : www.bipinrupadiya.com</title>
<script language="javascript">
var temp=new Array();
function findMyStr()
{
myStr=(document.getElementById("txt1").value);
str2bSearch=(document.getElementById("txt2").value);
myStr_length=myStr.length;
str2bSearch_length=str2bSearch.length;
for(i=0;i<myStr_length;i++)
{
temp[i]=myStr.substring(i,str2bSearch_length+i);
}
for(i=0;i<temp.length;i++)
{
if(temp[i]==str2bSearch)
{
found=true;
break;
}
else
{
found=false;
}
}
if(found==true)
document.getElementById("msg").innerHTML='String found ...';
else
document.getElementById("msg").innerHTML='string not found';
}
</script>
</head>
<body>
<br>Enter String : <input type="Text" ID="txt1">
<br>Search String : <input type="Text" ID="txt2">
<br><input type="button" value=" SEARCH" onClick="findMyStr()">
<div id="msg" style="color:red"></div>
</body>
</html>

postheadericon 7. Write a JavaScript to convert Celsius to Fahrenheit.




<html>
<head>
<title>EX-7: WTAD : www.bipinrupadiya.com</title>
<script type="text/javascript">
function convert(a)
{
var F=(a-32) * 5 / 9;
document.getElementById("far").innerHTML=a + ' Celsius = ' + F + ' Farenheit';
}
</script>
</head>
<body>
<br>Enter Celsius : <input type="text" id="txt" onkeyup="convert(this.value)">
<div id="far"></div>
</body>
</html>

postheadericon 6. Write a JavaScript to remove the highest element from the array and arrange the array in ascending order.




<html>
<head>
<title>EX-6: WTAD : www.bipinrupadiya.com</title>
<script type="text/javascript">
var a=new Array();
function addEle()
{
a.push(parseInt(document.myForm.txtEle.value));
document.getElementById("arrayEle").innerHTML=a;
document.myForm.txtEle.value="";
document.myForm.txtEle.focus();
}
function removeEle()
{

var l=a.length;
for(i=0;i<=l;i++)
{
for(j=i+1;j<l;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
document.getElementById("delEle").innerHTML="<br><br><b><font color='red'>The Highest element [ "+a.pop()+" ] is deleted</font></b><br>";
document.getElementById("sortedEle").innerHTML="<br><br><b><u>Sorted Array:</u></b><br>"+a;
}
</script>
</head>
<body>
<form name=myForm>
Enter Array Element:<input type="text" id="txtEle">
<input type="button" value="ADD" onClick="addEle();">
<input type="button" value="REMOVE" onClick="removeEle();">
</br>
</br>
<b><u>Elements In Array:</u></b>
</form>
<div id="arrayEle"></div>
<div id="delEle"></div>
<div id="sortedEle"></div>
</body>
</html>

postheadericon 5. Write a JavaScript to generate two random numbers and find out maximum and minimum out of it.




<html>
<head>
<title>EX-5: WTAD : www.bipinrupadiya.com</title>

<script type="text/javascript">
function bsr()
{
var rnd1=Math.floor(Math.random()*100);
var rnd2=Math.floor(Math.random()*100);
document.write('<br>Random No.1 : ' + rnd1);
document.write('<br>Random No.2 : ' + rnd2);
if (rnd1 < rnd2)
{
document.write('<br><br>Minimum No.:' + rnd1);
document.write('<br>Maximum No.:' + rnd2);
}
else
{
document.write('<br><br>Minimum No.:' + rnd2);
document.write('<br>Maximum No.:' + rnd1);
}
}
</script>

</head>

<body onLoad='bsr();'>

</body>
</html>

postheadericon 4. Write a JavaScript that finds out multiples of 10 in 0 to 10000. On the click of button start the timer and stop the counter after 10 seconds. Display on the screen how many multiples of 10 are found out within stipulated time.




<html>
<head>
<title>EX-4: WTAD : www.bipinrupadiya.com</title>
<head>
<script type="text/javascript">
function StartTimer()
{
setTimeout ("doMyTask()", 10000);
}
function doMyTask()
{
var i = 1;
for(i=1;i<10000;i++)
if(i%10==0)
document.write(i + ' , ');
}
</script>
</head>
<body>
<form name=f1>
<input type="button" name="clickMe" value="Click Me...!" onclick="StartTimer()"/>
</form>
</body>
</html>

postheadericon 3. Create a Form in HTML with two fields, minimum and maximum, write JavaScript to validate that only numeric value is entered in both, and the value entered in minimum is less than the value entered in maximum.



<html>
<head>
<title>EX-3: WTAD : www.bipinrupadiya.com</title>
<script language="javascript">
function isValid()
{
m=parseInt(document.myform.txtMax.value);
n=parseInt(document.myform.txtMin.value);
if(m<n)
{
document.getElementById("error").innerHTML="Maximum Number can not be less then Minimum Number ";
}
else
{
document.getElementById("error").innerHTML="*";
}
}
function isNum(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if(! (charCode >= 48 &&charCode <= 57))
{
return false;
}
return true;
}
</script>
</head>
<body>
<form name="myform">
<br>Maximum No.:<input type="text" name="txtMax" onkeyup='isValid()' onkeypress='return isNum(event)'>
<br>Minimum No.:<input type="text" name="txtMin" onkeyup='isValid()' onkeypress='return isNum(event)'>
<div id="error"></div>
</form>
</body>
</html>

postheadericon 2. Write a JavaScript that demonstrates the use of +=,-=,*=,/= operators.





<html>
<head>
<title>EX-2: WTAD : www.bipinrupadiya.com</title>
</head>
<body>
<h1>
<script type="text/javascript">
var a=10;
document.write("<br>A: "+a);
a+=10;
document.write("<br>a+=10 : "+a);
a-=10;
document.write("<br>a-=10 : "+a);
a*=10;
document.write("<br>a*=10 : "+a);
a/=10;
document.write("<br>a/=10 : "+a);
</script>
</body>
</html>

postheadericon 1. Write a JavaScript that shows how a variable’s type can be changed on-the-fly.



<html>
<head>
<title>EX-1: WTAD : www.bipinrupadiya.com</title>
</head>
<body>
<h1>
<script type="text/javascript">

var myVar=10;
document.write("<br>myVar : ["+myVar +"] Type : ["+typeof(myVar)+"]<br>");
myVar=12.5;
document.write("<br>myVar : ["+myVar +"] Type : ["+typeof(myVar)+"]<br>");
myVar='c';
document.write("<br>myVar : ["+myVar +"] Type : ["+typeof(myVar)+"]<br>");
myVar=true;
document.write("<br>myVar : ["+myVar +"] Type : ["+typeof(myVar)+"]<br>");
myVar="Bipin Rupadiya";
document.write("<br>myVar : ["+myVar +"] Type : ["+typeof(myVar)+"]<br>");
</script>
</body>
</html>

postheadericon getting "android write to sdcard permission denied..?" do the following

Getting android write to sdcard permission denied or File not found permission denied error while creating file in sdcard. don't worry solution is here.

>> Create sdcard using command prompt

open command prompt
D:\...\android-sdk_r11-windows\android-sdk-windows\tools
type command [ mksdcard 1024M sdcard.iso ]
now open Android SDK and AVD manager from windows menu in Eclipse
Create or Edit AVD
From SD Card option Choose our sdcard.iso file using Browse from tools directory of sdk
set other required option for you then create or edit AVD

now you are able to put your file, database etc to sd card. Just keep a point in mind that when ever you want to write data on external storage / SD Card then you have to add a User Permission





to Android Manifest.xml file

Follow by Email

Blog Archive

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.