Labs
In this lab you will create a Dynamic Web Project with a form to submit data, and a servlet to handle that data and return a response.
-
Create a new Dynamic Web Project called WebDataLabs. Make sure to have Eclipse generate the
web.xml. -
Convert the project to a Maven Project and add the servlet dependency to
pom.xml.
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
- In
src/main/webapp/, create an HTML pageuserdata.html. - Create a form that submits a
POSTrequest touserdata. - Add text fields for
firstNameandlastName. -
Add checkbox fields allowing the user to select days of the week for class. Name the checkbox input whatever you want (
day, for example).
(Solution: WebDataLabsSolution/src/main/webapp/userdata.html)
- Create a class in the package
com.example.webdata.servletscalledUserDataServlet. It must extendjavax.servlet.http.HttpServletand overridedoPost(). - Get the values for each field (or set of fields).
-
Use
PrintWriterto create an HTML response that displays the values back to the user. Style this however you choose.
(Solution: WebDataLabsSolution/com.example.webdata.servlets.UserDataServlet)
- In
web.xml - Change the
<welcome-file-list>to use youruserdata.htmlpage as the only<welcome-file>. - Add a
<servlet>for yourcom.example.webdata.servlets.UserDataServletclass. - Add a
<servlet-mapping>mapping for the url/userdataso that it uses your servlet.
(Solution: /WebDataLabsSolution/src/main/webapp/WEB-INF/web.xml)
-
Test your application. Be sure to test for the case where the user does not select any days.
-
(Optional) Test using the value below in one of your text fields:
<script>alert("Malice!");</script>
- (Optional) Add a field to your HTML form for
age. Have the servlet add a message to the response if the user is over 21. - Test with the value
A.