. What are the several ways of tracking users?
Ans.
The several ways in which you can track users are:
n Using hidden controls
n Using cookie
n Using sessions
n Using applications
2. Explain in brief how to use hidden controls in a web page? Give an example.
Ans.
Using HTML, hidden controls are an easy way to store data in a Web page. You can store data in a hidden control and then refer to the value of control wherever required. Hidden controls are often used when you submit a form to itself. That is if your form’s ACTION attribute is set to the same page which contains the form. In this case, every time the page loads you have to determine whether it is being loaded afresh or after a submission. For this, you can store a text value such as “DONE” in a hidden control SUBMIT_STATUS, and check for it whenever the page loads. If request.getParameter(SUBMIT_STATUS) returns a NULL value, it means that the form is being loaded afresh.
This logic is demonstrated in the following code:
<html>
<head></head>
<body>
<%
if(request.getParameter(“DONE”)==null)
{
out.println(“You entered your name as:” + getParameter(“name”));
}
else
{
%>
<form action = “this.jsp” method = “POST”>
Enter your name:<input type =”text” name = “name”></input
<input type = “HIDDEN” name = “DONE” value =”submitted”>
<input type = “SUBMIT” value = “SUBMIT”>
</form>
<%
}
%>
</body>
</html>
3. What are the disadvantages of hidden controls?
Ans.
The disadvantages of hidden controls are that they are not secure as any user can access the HTML source and view their value. Consequently, you cannot store crucial information such as passwords in them. In addition, they complicate the code many times.
4. What are cookies? Explain in brief.
Ans.
Cookies are small text strings you store on a user’s computer. Cookies store information for tracking users in name-value pairs. When you first visit a website it may store a cookie on your computer. Next, when you again visit the site it may read or manipulate the value of the cookie and perform an action based on it. For example, if a site allows you to customize the color of the user interface, it may set the color selected by you in a cookie on your computer and read the color value when next time you request the page of the website. The page can them be displayed in the color stored in the cookie.
5. List down any 6 methods of javax.servlet.http.Cookie class and their functionality.
Ans.
Following are the methods of the Cookie class:
n java.lang.String getName(): Returns the name of the cookie
n java.lang.String getValue(): Returns the value of the cookie
n void setValue(java.lang.String value): Assigns a new value to a cookie after it has been created
n void setPath(java.lang.String uri): Sets the path for the cookie by which the browser will send the cookie
n void setMaxAge(int expiry): Set the maximum age of the cookie in seconds
n int getMaxAge(): Returns the maximum age of the cookie in second
6. List down any 6 methods of HttpServletResponse class and their functionality.
Ans.
Following are the methods of the HttpServletResponse class:
n int SC_ACCEPTED: Indicates that a request was accepted for processing but was not completed
n int SC_BAD_GATEWAY: Indicates that the HTTP server received an invalid response from a server, when acting as a proxy or gateway
n int SC_BAD_REQUEST: Indicates that the request sent by the client was incorrect
n int SC_CONFLICT: Indicates that the request cannot be completed because of a conflict with the current state of the resource
n int SC_CONTINUE: Indicates that the client can continue
7. Explain in brief how to create a cookie with an example.
Ans.
You can create a cookie by instantiating an object of the Cookie class. For example, to create a cookie name ‘mycookie’, with the value ‘this is the cookie value’, you can use the following code snippet:
Cookie mycookie = new Cookie(“mycookie”, “this is the cookie value”);
mycookie.setMaxAge(24*60*60);
respone.addCookie(mycookie)//install cookie on browser
The setMaxAge() method sets the time after which the cookie will expire. The response.addCooki() method will install the cookie on the browser.
8. Explain in brief how to read a cookie with an example.
Ans.
You can read a cookie as:
Cookie[] cookies = request.getCookies();
for(int i=0;i<cookie.length;i++)
{
if(cookies[i].getName().equals(“mycookie”))
out.println(“The value is” + cookie[i].getValue());
}
9. What is a session?
Ans.
Sessions let you preserve data between accesses to a Web page by the same user. Unlike cookie, sessions are stored on the server itself in files or databases.
10. Explain in brief how to track a user using Sessions with an example.
Ans.
You can track user using sessions by storing the details of his/her session with the Web site. For example, you store the number of times that a user has visited a page and display it to him on every visit. To track the user using sessions perform the following steps:
- Include page directive with the session attribute set to true, at the top of the page, to indicate that a new session has to be started(if one does not already exist).
- Check whether a count has already been set before in a session attribute using the getAttribute() method of the Session class.
- If the count has not been set, the getAttribute() method will return null. In that case, you can create a new count value. If a value already exists, you can increment it and store the new value in the session object.
The above steps are implemented in the following code:
<%
Integer count = (Integer)session.getAttribute(“visitcount”);
If(count==null)
{count = new Integer(1);
}
else
{
count = new Integer(visitcount.intValue()+1);
}
session.setAttribute(“visitcount”, count);
%>
<body>
You have visited this page <%=count%> times.
</body>
11. What is the difference between Session and Application?
Ans.
A single session allows you to track one user at a time whereas an application enables you to track all JSPs in the same site, regardless of the number of users accessing them.
12. Explain how to track users using SESSIONS, APPLICATIONS, AND JAVABEANS.
Ans.
You can instruct Tomcat to save the JavaBeans in a session object as well as in attributes. You can store JavaBeans in applications as well. You do this with <jsp:useBean> element’s scope attribute, which you can set to one of these value: scope=”page|request|session|application”. The term scope indicates where a data item is visible in your code. The default scope for a bean is page scope, which means the bean exists only for the page scope. However, if you set the scope of a bean to a session, it is stored with the rest of session’s data