RSS

Monthly Archives: April 2015

Empty HTML Elements

HTML elements with no content are called empty elements.
•<br> is an empty element without a closing tag (the <br> tag defines a line break).
•Empty elements can be “closed” in the opening tag like this: <br/>.
•HTML5 does not require empty elements to be closed. But if you want stricter validation, or you need to make your document readable by XML parsers, you should close all HTML elements.

 
Leave a comment

Posted by on April 23, 2015 in Example

 

What is a mutable data structure? Show how ArrayList can be used to store session?

Mutable Data Structure :

• Mutable Objects are the objects which can be modified after it is created.
• Example of Mutable data structure are an array, List, Map, or application-specific data structure that has writable fields (instance variables).

We can use ArrayList to Store the Session Data in the following way :

HttpSession s=req.getSession();
 ArrayList al;
             
 al=(ArrayList)s.getAttribute("productlist");
 if(al==null)
        {
            al=new ArrayList();
            s.setAttribute("productlist",al);
        }
        if(pnm !=null)
        {
            al.add(“item”); //here add the item to the ArrayList
        }
        if (al.size()==0)
        {
             out.println ("no products in cart");
        }
         else
        {
            for (int i=0;i<al.size();i++)
            {
                out.println (al.get(i)+"");
            }
        }

 
Leave a comment

Posted by on April 23, 2015 in Example

 

Create the User Define Object using the Object() Constructor

Object() Constructor

• A constructor is a function that creates and initializes an object.
• JavaScript provides a special constructor function called Object() to build the object.
• The return value of the Object() constructor is assigned to a variable.
• The variable contains a reference to the new object.
• The properties assigned to the object are not variables and are not defined with the var keyword.

Test1.html

<script>
var book = new Object();   // Create the object
book.subject = "HTML"; // Assign properties to the object
book.author  = "Ivan Bayrose";

document.write(book.subject+ "<br>");
document.write(book.author + "<br>");	
	
</script>
 
Leave a comment

Posted by on April 23, 2015 in Example

 

How a constructor can be used to initialize user defined objects in JavaScript

USER DEFINE OBJECTS
• javascript permits the creation of user defined objects.
• All user-defined objects and built-in objects are descendants of an object called Object.
• This object will also be associated with properties and methods.
• After creation of such an object, any number of instances of this object can be created and used.
• The new operator is used to create an instance of an object. To create an object, the new operator is followed by the constructor method.
• A constructor is a function that creates and initializes an object.

Test.html

<script>
function student(name,age,marks) //constructor 
{
	this.name=name;
	this.age=age;
	this.marks= marks;
}

student1=new student("anil",10,75);
//for student1
document.write(student1.name + "<br>");
document.write(student1.age+ "<br>");
document.write(student1.marks+ "<br>");
</script>
 
Leave a comment

Posted by on April 23, 2015 in Example

 

Block The Response Through Filter

Test.html

<html>
<body>
	<form action="Print">
	Enter parameter Value : 
	<input type="text" name="myParam">
	<input type="submit" value="OK"> 
	</form>
</body>
</html>

Print.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
 
public class Print extends HttpServlet
 {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {
		String param=request.getParameter("myParam");
		PrintWriter p=response.getWriter();
		p.println("Parameter = " + param);
	}
}

BlockResponse.java

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class BlockResponse implements Filter 
{
public void init(FilterConfig config) throws ServletException{}
public void doFilter(ServletRequest request,ServletResponse response,FilterChain filterChain)
throws IOException, ServletException 
{

    String myParam = request.getParameter("myParam");
	if("facebook".equals(myParam))
	{
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    httpResponse.getWriter().write("a different response... Access denied");
    }
	else
	{
	filterChain.doFilter(request,response);
	}
}
public void destroy(){}
}

web.xml

<servlet>
	<servlet-name>Print</servlet-name>
	<servlet-class>Print</servlet-class>
</servlet>
<servlet-mapping>
	<servlet-name>Print</servlet-name>
	<url-pattern>/Print</url-pattern>
</servlet-mapping>
<filter>
   <filter-name>BFilter</filter-name>
   <filter-class>BlockResponse</filter-class>
</filter>
<filter-mapping>
   <filter-name>BFilter</filter-name>
   <url-pattern>/Print</url-pattern>
</filter-mapping>
 
Leave a comment

Posted by on April 20, 2015 in Example

 

Tags: ,

Session Management : Simple Shopping Cart

Product.html

<html>
<body>
<form action="esc">
	Enter Product name : 
	<input type="text" name="txtpnm" >
	<br>
	<input type="submit" value="ok">
</form>
</body>
</html>

esc.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
 
public class esc extends HttpServlet
{
    public void doGet(HttpServletRequest req,HttpServletResponse res)
    throws ServletException,IOException
    {
		res.setContentType("text/html");
		PrintWriter out =res.getWriter();
		
		String pnm=req.getParameter("txtpnm");
		HttpSession s=req.getSession();
		ArrayList al;
			
		al=(ArrayList)s.getAttribute("productlist");
		if(al==null)
		{
			al=new ArrayList();
			s.setAttribute("productlist",al);
		}
		if(pnm !=null)
		{
			al.add(pnm);
		}
		if (al.size()==0)
		{
			 out.println ("no products in cart");
		}
		 else
        {
            for (int i=0;i<al.size();i++)
            {
                out.println (al.get(i)+"");
            }
        }
	}
}
 
Leave a comment

Posted by on April 15, 2015 in Example

 

Using Hidden Form Field for Session Management

First.html

<form action="validate" method="post">
Name : 
<input type="text" name="unm"> <br>
Password : 
<input type="password" name="pwd"> <br>
<input type="submit" value="ok">
</form>

MyServlet.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet {

  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException ,IOException
  {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		 
		String unm=request.getParameter("unm");
		String pwd=request.getParameter("pwd");
		
		if(pwd.equals("1234"))
		{
			out.println("<form action=First>");
			out.println("<input type=hidden name=user value="+ unm + ">");
			out.println("<input type=submit value=submit>");
			out.println("</form>");
			
		}
   }
}

First.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class First extends HttpServlet 
{
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException ,IOException
  {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		String unm=request.getParameter("user");
		out.println("welcome  " + unm);
  }
}
 
Leave a comment

Posted by on April 15, 2015 in Example

 

Using URL rewriting for Session Management

If the client has disabled cookie in the browser then session management using cookie will not work. In that case URL rewriting can be used as a backup. URL rewriting will always work.

In URL rewriting, a token(parameter) is added at the end of the URL. The token consist of name/value pair seperated by an equal(=) sign.

Example
url-rewriting

Advantage of URL Rewriting

1)It will always work whether cookie is disabled or not (browser independent).
2)Extra form submission is not required on each pages.

Disadvantage of URL Rewriting

1)It will work only with links.
2)It can send Only textual information.

login.html

<html>
<body>
<form action="MyServlet">
Enter name : 
<input type="text" name="unm">
<br>
Enter Password : 
<input type="password" name="pwd">
<input type="submit" value="ok">
</form>
</body>
</html>

MyServlet.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
 
public class MyServlet extends HttpServlet
 {
	public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException ,IOException 
	{
		PrintWriter out=res.getWriter();
		String unm=req.getParameter("unm");
		String pwd=req.getParameter("pwd");
		
		if(pwd.equals("1234"))
		{
			res.sendRedirect("First?username="+unm);
		}
	}
}

First.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
 
 public class First extends HttpServlet
 {
	public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException ,IOException 
	{
		PrintWriter out=res.getWriter();
		String unm=req.getParameter("username");
                out.println("Welcome "+unm);
	}
}

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
<servlet>
	<servlet-name>First</servlet-name>
	<servlet-class>First</servlet-class>
</servlet>
<servlet-mapping>
	<servlet-name>First</servlet-name>
	<url-pattern>/First</url-pattern>
</servlet-mapping>
<servlet>
	<servlet-name>MyServlet</servlet-name>
	<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
	<servlet-name>MyServlet</servlet-name>
	<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
</web-app>
 
Leave a comment

Posted by on April 1, 2015 in Example

 

Using Cookies for Session Management

cookies-example

login.html

<html>
<body>
<form action="MyServlet">
Enter name : 
<input type="text" name="unm">
<br>
Enter Password : 
<input type="password" name="pwd">
<input type="submit" value="ok">
</form>
</body>
</html>

MyServlet.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
 
public class MyServlet extends HttpServlet
 {
	public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException ,IOException 
	{
		PrintWriter out=res.getWriter();
		String unm=req.getParameter("unm");
		String pwd=req.getParameter("pwd");
		
		if(pwd.equals("1234"))
		{
			Cookie c = new Cookie("username",name);
                        response.addCookie(c);
			res.sendRedirect("First");
		}
	}
}

First.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
 
 public class First extends HttpServlet
 {
	public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException ,IOException 
	{
		PrintWriter out=res.getWriter();
		Cookie[] cks = request.getCookies();
                out.println("Welcome "+cks[0].getValue());
	}
}

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
<servlet>
	<servlet-name>First</servlet-name>
	<servlet-class>First</servlet-class>
</servlet>
<servlet-mapping>
	<servlet-name>First</servlet-name>
	<url-pattern>/First</url-pattern>
</servlet-mapping>
<servlet>
	<servlet-name>MyServlet</servlet-name>
	<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
	<servlet-name>MyServlet</servlet-name>
	<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
</web-app>

Disadvantage: – The users can opt to disable cookies using their browser preferences. In such case, the browser will not save the cookie at client computer and session tracking fails.

 
Leave a comment

Posted by on April 1, 2015 in Example