Chapter 9: Problem 10
Write a Web application that consists of a servlet (DirectoryServlet) and several Web documents. Document index.html should be the first document the user sees. In that document, you should have a series of hyperlinks for other Web pages in your site. When clicked, each hyperlink should invoke the servlet with a get request that contains a page parameter. The servlet should obtain parameter page and redirect the request to the appropriate document.
Short Answer
Step by step solution
Create Index.html
Set Up DirectoryServlet
Handling GET Requests in DirectoryServlet
Redirect Based on Page Parameter
Create Additional HTML Documents
Unlock Step-by-Step Solutions & Ace Your Exams!
-
Full Textbook Solutions
Get detailed explanations and key concepts
-
Unlimited Al creation
Al flashcards, explanations, exams and more...
-
Ads-free access
To over 500 millions flashcards
-
Money-back guarantee
We refund you if you fail your exam.
Over 30 million students worldwide already upgrade their learning with Vaia!
Key Concepts
These are the key concepts you need to understand to accurately answer the question.
Web Application Development
This particular exercise demonstrates the development of a web app using Java Servlets, a popular server-side technology. Servlets allow programmers to extend the capabilities of web servers and are commonly used for processing form data, managing sessions, and redirecting requests.
- Java Servlets: They are Java programming language classes that process requests and create responses.
- Entry Point: In web applications, the entry point is often an HTML page where users begin their interaction.
- Hyperlinks: These are links embedded in the HTML that users click to navigate through the application.
HTTP GET Requests
In the context of this exercise, GET requests are essential because they enable users to navigate through different pages of the web application. Users interact with hyperlinks within `index.html`, which send GET requests to the servlet with a 'page' parameter.
- GET vs. POST: GET requests retrieve data; POST requests submit data.
- URL Parameters: GET requests can pass data to the server as part of the URL, such as `?page=home`.
- Stateless: HTTP is a stateless protocol, meaning each request is independent.
Servlet Redirection
In this exercise, the `DirectoryServlet` uses redirection extensively. When a link in `index.html` is clicked, it sends a GET request with a `page` parameter indicating the desired destination page. The servlet checks this `page` parameter and uses `response.sendRedirect("destination.html")` to forward the user to the correct HTML document.
- Parameter Evaluation: The servlet evaluates the `page` parameter to decide the next page.
- Redirection Logic: Different URLs are constructed using `sendRedirect`, making it flexible for various page flows.
- Seamless Transitions: Users experience a smooth transition between pages through redirection.
HTML Document Structure
In the context of this exercise, `index.html` serves as the homepage. It contains hyperlinks, each pointing to a servlet invocation with parameters. The organization of this document is key to the user experience, as it controls how users navigate the web app.
- HTML Tags: Key elements like ``, `
`, `
`, are used to build web pages.
- Hyperlinks: Use `Link` to connect pages and invoke servlets.
- Document Structure: Begins with ``, followed by ``, and includes `` and `` sections.