Tutorial Week 5
Tutorial Week 5
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);
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