Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Suppose web form 1 , web form 2, Servlet1 (show below) , Servlet2 and Servlet3 (not shown) are all filed on a web server in

Suppose web form 1, web form 2, Servlet1 (show below), Servlet2 and Servlet3 (not shown) are all filed on a web server in the same directory. Note that form 1 invokes Servlet1 and form 2 invokes Servlet2.

This is form1

What is thy quest:

Press

to submit your query.


This is form 2

What is thy favorite color:

Press

to submit your query.


import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.util.*;

public class Servlet1 extends HttpServlet

{

public void doPost(HttpServletRequest req,

HttpServletResponse res)

throws ServletException, IOException

{

res.setContentType("text/html");

PrintWriter out = res.getWriter();

out.println("");

out.println("Servlet Response" +</p> <p> "");

out.println("");

Cookie c;

String name, value;

name = name;

value = req.getParameter(name);

c = new Cookie(name, value);

res.addCookie(c);

name = quest;

value = req.getParameter(name);

c = new Cookie(name, value);

c.setMaxAge(24*60*60);

res.addCookie(c);

out.println("");

} //end doPost

} //end class

During one browser session, a user browses form 1, then form 2. On form 1, the user enters Mary to the first question, and happiness to the second question. On form 2, the user chooses red.

Assume that there are no existing cookies on the users system.

1) During this session, how are the data entered in form 1 forwarded to Servlet1? Describe the data transferred among the browser, the HTTP server, and Servlet1 in terms of HTTP request, HTTP response, environment variables, and/or cookies.

2) During this session: When Servlet2 is initiated, what session state data items (name-value pairs) does it receive? Complete the following table to answer the question (you may not need to use all the rows):

name

value

3) During this session: For each session data item (name-value pair) that Servlet2 receives, explain how the data originated and how it was transmitted to Servlet2. Describe the data transferred among the browser, the HTTP server, Servlet1, and Servlet2 in terms of HTTP request, HTTP response, environment variables, and/or cookies.

4) Complete the code (including the method header) for Servlet2 so that it creates a session object to contain the state data (the name-value pairs from part 3) that it has received. Your code should work for any value associated with each state data item.

// import statements omitted

public class Servlet2 extends HttpServlet

{

public void (HttpServletRequest req,

HttpServletResponse res) throws ServletException, IOException

{

res.setContentType("text/html");

PrintWriter out = res.getWriter();

out.println(""); out.println("");

out.println("");

} //end doPost

} //end class

5) Suppose the browser windows that ran form1 and form2 have been closed. One hour later, another browser session is started by a user on the same computer, and a third servlet, Servlet3, filed in the same directory as Servlet1 and Servlet2, is invoked directly through the browser. Which, if any, name-value pair(s) from the first session will be available to Servlet3? Explain your answer.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Access Patterns Database Interactions In Object Oriented Applications

Authors: Clifton Nock

1st Edition

0321555627, 978-0321555625

More Books

Students also viewed these Databases questions