0% found this document useful (0 votes)
113 views

Tutorial Week 5

The document provides instructions for a lecture assignment on session tracking and database programming in Servlets. It includes 6 questions asking students to: 1) define session tracking and its techniques, 2) provide example code for using hidden values for session tracking, 3) write code to set cookies in a browser, 4) write code to display stored cookies, 5) write code to pass parameters via URL rewriting, and 6) write code using the Servlet API to track sessions and welcome returning users. Students are asked to answer in their own words and not copy from other sources.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views

Tutorial Week 5

The document provides instructions for a lecture assignment on session tracking and database programming in Servlets. It includes 6 questions asking students to: 1) define session tracking and its techniques, 2) provide example code for using hidden values for session tracking, 3) write code to set cookies in a browser, 4) write code to display stored cookies, 5) write code to pass parameters via URL rewriting, and 6) write code using the Servlet API to track sessions and welcome returning users. Students are asked to answer in their own words and not copy from other sources.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lecture Assignment

Tutorial Week 5
Our readings (lecture notes and video lecture) this week discuss the Servlet (session tracking
and database programming in Servlet). Write the answers for all questions in the table
according to your understanding. PLEASE USE YOUR OWN WORDS. DO NOT COPY FROM
THE WEB.

Questions Answers
1. What is session tracking? What are the three techniques for session tracking is series of interactions to
session tracking? track data among requests over a period

techniques:
- Using hidden values
- Using cookies
- Using URL rewriting

2. How do you use hidden values in HTML form for session public class FirstServlet extends
tracking? Write example code for session tracking using HttpServlet
hidden values based on the following figure. {
public void doPost(HttpServletRequest
request, HttpServletResponse response)
{
try
{

response.setContentType("text/html");
PrinterWriter out =
response.getWriter();

String
nm=request.getParameter("name");
String em =
request.getParameter("emailID");

MMR2020@FSKM
Lecture Assignment

out.print("Welcome"+n);

//creating form that have insible


textfield
out.print("<form action = 'servlet2'>");
out.print("Enter Country <input type =
'text' name = 'country'>");
out.print("<input type = 'hidden'name
= 'name'value = '"+nm+"'>");
out.print("<input type='hidden'
name='email' values!"+egsis°);
out.print("<input type=' submit'
values'subait's");
out.print("</form");
out.close();
}
catch (Exception e)
(System.out.printin(e);)
}
}
3. Write a servlet that stores the following cookies in a browser Cookie(“color”,”red”);
and set their max age for two days: Cookie cookie2 = new
Cookie 1: name is “color” and value is red. Cookie(“radius”,”5.5”);
Cookie 2: name is “radius” and value is 5.5. Cookie cookie3 = new
Cookie 3: name is “count” and value is 2. Cookie(“count”,”2”);
response.addCookie(cookie1);
cookie.setMaxAge(172800);
4. Write a servlet that display all the cookies on the client. The Cookie c[]=request.getCookies();
client types the URL of the servlet from the browser to display for(int i=0;i<c.length;i++){
all the cookies stored on the browser. out.print("Name: "+c[i].getName()+" &
Value: "+c[i].getValue());
}
5. Write a servlet URLtracker.java that retrieve values from a url response.setContentType("text/html");

MMR2020@FSKM
Lecture Assignment

html form and send parameter name and studentID using PrintWriter out =
URL rewriting technique to another servlet. Use response.getWriter();
getParameter() method to obtain a parameter value. String
n=request.getParameter("username
");
out.print("Welcome "+n+" ");
//appending the username in
the query string
out.print("<a
href='servlet2?uname="+n+"&StudId=”+s+

'>visit</a>");
out.close();
6. Create a servlet SessionTracker.java and use session HttpSession
tracking using the Servlet API to keep track the session and session=request.getSession();
display “Welcome to CSC584 class” to first-time visitors session.setAttribute("uname",N);
(within a browsing session) and “Welcome Back” to repeat if(session.isNew()) {
visitors. If you did this same task earlier with the Cookie API, out.print("Welcome to
was this servlet harder or easier than the equivalent version CSC584 class "+N);
using cookies explicitly? count = 0;
}
else {
out.print("Welcome Back "+N);
count++;
}
if(count >=10) {
session.invalidate();
}

MMR2020@FSKM

You might also like