Likes

WCD-LAB @HOME 8-Ans 2

You need to enhance the Exotica Web application in such a ray that when the Inquire Nov/ hyperlink is clicked, the Inquire Nov/ Web page gets displayed. Therefore, you need to develop the Inquire Nov/ Web page, as shown in the following figure.
In addition, you need to perform the following validations when the form is submitted:
The values, such as name, address, contact number or email ID. and query, are filled by the user.
If the user has selected the option. Respond via phone, the contact number field must have a value: and if the user has selected the option. Respond via email, the email field must have a value.
If the respond via phone option is selected, you also need to ensure that the date and time is selected.
Further, if the user has submitted the form with appropriate values, the message. Thank you. Our executive will get back to you soon, should be displayed.



1. Open the Web application. ExoticaTravel.
2. Create the JSP page. InquireNowPage.jsp. and replace the cede with the following code:
<%@ taglib prefix="c"
uri="http://java.sun.com/jap/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<c:if test="S(not empty errorMessage)">
<P>
<£ont color="red">Please correct the following errors:
<ul>
<c:forEach var="message" items="${errorMessage}">
<li>${message}</li>
</c:forEach>
</ul>
</font>
</p>
</c:if>
<form action="InquireNowServlet">
<table width="100%">
<tr>
<td width="21%">Name</td>
<td width="79%"><input type="text" name="iName value="${iName}" /></td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" name="address" value="S{address}"/></td>
</tr>
<tr>
<td>Contact Numfcer</td>
<tdXinput type="text" name="phone" value="${phone}"/></td>
</tr>
<tr>
<td>Email</td>
<tdxinput type="text" name="email" value="${email}" /></td>
</tr>
<tr>
<td>Query</td>
<tdxtextarea name="query" rows="5">${query} </textareaX/td>
</tr>
<tr><td><input type="radio" value="viaPhone" name=,,re3pon3eType" ${re3ponseTypePhone}>Re3pond via phone</td>
<td><input type="radio" name="responseType" value="viaEmail" S{responseTypeEmail}>Respond via email</td>
<tr><td colspan="7"> If you opt to respond via phone, specify the date and time you want our executive to contact you</td>
<tr><td>Date
<select name="day">
<option
value="3elect">3elect</option>
<c:forEach begin="l" end="31" var="day"> <option value="S{day}">${day}
</option>
</c:forEach>
</select>
<select name="month">
<option
value="select">select</option>
<c: forTokene
iteme="2011,2012,2013,2014,2015,2016,2017,2018,2019,2020" delims="," var="year">
</option> <option value="${ year} ">${ year} </c: f orTokens» </select>
</td> <td>Time <select name=wtime"> <option
value="3elect">3elect</option>
<c:£orTokens items="between 6-7 am,between 7-8 am,between 8-9 am,between 9-10 am,between 10-11 am,between 11-12 am,between 12-1 pm, between 1-2 pm, between 2-3 pm,between 3-4 pm, between 4-5 pm, between 5-6 pm, between 6-7 pm, between 7-8 pm" delims=w," var="time">
</option> <option value="${time}">${time} </c: forTokens» </select>
</td>
<trXtdXinput type="submi t" value="Submit"x/td></tr>
</table>
</form>
</body>
</html>
3. Create a servlet. InquireNowServletjava. and replace the existing code with the following code:
import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
SWebServlet(name - "InquireNowServlet", urlPatterns —
{"/InquireNowServlet"})
public class InquireNowServlet extends HttpServlet {
String iName, address, phone, email, responseType, query, responseTypePhoneVal = "unchecked",
responseTypeEmailVal — "unchecked", day, month, year, time; ArrayList errorMessage = new ArrayList();
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter();
try {
errorMessage.clear();
address = request.getParameter("address"); query = request.getParameter("query"); phone = request.getParameter("phone"); email = request.getParameter("email"); responseType = request.getParameter ("responseType");
day = request.getParameter("day"); month = request.getParameter("month"); year = request.getParameter("year"); time = request.getParameter("time");
if (errorMessage.isEmpty()) {
iNarae = request.getParameter("iNamc");
if (iName.equals("")) {
errorMessage.add("Please enter your
name");
}
if (query.equals("")) {
errorMessage.add("Please specify your
query");
)
if (phone.equals("") && email-equals("")) { errorMessage.add("Please enter the contact number or email ID");
>
if(!phone.equals("")){
if (!(phone.matches("-?\\d+(\\.\\d
+)?"))> {
errorMessage.add("Please enter a
valid contact number");
>
)
if (responseType == null) {
errorMessage.add("Please select the
response type");
} else {
if(responseType.equals("viaPhone")){ if(phone.equals("")){
errorMessage.add("Please enter
the contact number");
responseTypePhoneVal =
"checked";
responseTypeEmailVal =
"unchecked";
}
else{
\.\\d+)?")>) {
if (!(phone.matches("-?\\d+(\
responseTypePhoneVal =
"checked";
responseTypeEroailVal =
"unchecked";
}
else {
responseTypePhoneVal =
"checked";
responseTypcEmailVal =
"unchecked";
if (day.equals("select") ||
month.equals("select") || year.equals("select") || time.equals("select")) {
errorMessage.add
("Please select the date and time");
}
}
}
}
else { if
your your email ID");
"checked";
"unchecked";
}
}
(email.equals("") ) { errorMessage.add("Please enter
responseTypeEmailVal =
responseTypePhoneVal =
}
}
if (! errorMessage. isEmptyO ) {
request.setAttribute("iName", iName); request.setAttribute("address",
address);
request.setAttribute (••query", query) ; request.setAttribute("phone", phone); request.setAttribute("email", email); request.setAttribute
("responseTypePhone", responseTypePhoneVal);
request.setAttribute
("responseTypeEmail", responseTypeEmailVal);
request -setAttribute("errorMessage",
errorMessage);
RequestDispatcher rd =
request.getRequcstDispetcher("InquireNowPage.jsp");
rd.forward(request, response);
> (
System.out.println ("InquireNowDisplayPagc.jsp");
RequestDispatcher rd =
request.getRequestDispatcher("InquireNowDisplayPage.j sp");
rd.forward(request, response);
}
}
catch (Exception e) {
System.out.println(e);
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
+
* 0param request servlet request
* 8param response servlet response
* 8throws ServletException if a servlet-specific error occurs
* ©throws IOException if an I/O error occurs */
©Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* 8poram request servlet request
* 0param response servlet response
* ©throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs */
QOverride
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { processRequest(request, response);
/**
* Returns a short description of the servlet.
*
* 0return a String containing servlet description */
0Override
public String getServletlnfo() { return "Short description";
}// </editor-£old>
}
4. Open the LinksPage.jsp page and replace the following statement:
<tr><td><a href="PageUnderConstruction.jsp" target="middle">Inquire Now</aXbrX/tdX/tr>
with the following statement:
<trXtdXa href="InquireNowPage .jsp" target="middle">Inquire Now</a><brX/tdX/tr>
5. Run the HomePage.jsp file.

1 comment:

Unknown said...

where is HomePage.jsp page?