To implement a simple servlet, fetch information from HTML forms and parse the headers populated by the client's browser.
Exercise 1: Build and run HelloWorld Servlet Application
- Download the sample code.
- Open hello2 NetBeans project.
- Select File->Open Project (Ctrl+Shift+O). The Open Project dialog box appears.
- Browse down to <LAB_UNZIPPED_DIRECTORY>/servletbasics/samples directory.
- Select hello2.
- Click Open Project Folder. The hello2 project node appears under Projects tab window.
- Build (F11) and run (F6) hello2 project.
- Right-click hello2 project and select Run Project.
- Browser gets displayed. Type your name in the input form field and click Submit button.
- Note how the GreetingServlet includes the ResponseServlet
- Double click GreetingServlet.java under hello2->Source Packages->servlets to open in the source editor.
- Modify the code such that it asks for your hobby.
- Modify ResponseServlet.java under hello2->Source Packages->servlets such that it fetches the information sent from the client and displays on the screen
Exercise 2: Write an application that fetches the HTTP Headers sent by the browser
- Write a Servlet or modify the sample code such that it fetches the following headers from the client's browser and displays them in the form of an .html page.
- HTTP Method
- Request URL
- Client's IP Address
- Content type supported by the browser
- List of all headers sent by the browser
Exercise 3: Write an application that accesses the MySQL database server
- Write a Servlet or modify the sample code such that it fetches all the country names along with their population from the MySQL database server (as done in the previous lab) and lists them in a table within an .html page.
Exercise 4: Implement an application that utilizes session tracking
- Modify exersice 3 in such a way that the user has to login to access the list of all countries.
- Create a login servlet that asks the username for his/her password.
- Store that information in the session object.
- Direct the user to the servlet that lists the countries.
- If the user is logged in (i.e. the session objects maintain the correct password) then list the countries.
- Otherwise, redirect the user to the login page.
Instructions for submitting your work
- You are required to demonstrate your implementation during the lab.
- Create an archive (.zip) of your project folder and submit it online.
Last updated: 25th November, 2007