Tuesday 1 December 2015

WCD -LAB @HOME 3

WCD -LAB @HOME 3
1. Enhance the ExoticaTravels Web application by implementing the following functionalities using the MVC architecture:
When the user clicks the submit button of the registration page, the details entered in the registration page should be stored in a file and the welcome page should be displayed similar to the following figure.
The welcome page should have hyperlinks for cab bookings, hotel bookings, tour package bookings, and plan travel.When the user submits the login page, the login credentials should be validated. If the credentials are valid then the welcome page should be displayed, else an error page should be displayed.Note: To perform this exercise, you need to use the solution created in Exercise 01 of Chapter 02. In addition, you need to use the Car.jpg, PlanTour.jpg, Hotel.jpg, and Tourpackage.jpg images.                                  

To solve this exercise, you reed to perform the following steps:
1. Open the Web application. ExoticaTravels.
2. Create a Java class. UserDetailsModel.java. and replace the existing cede with the following cede:
public class UserDetailsModel {
String uname, address, contactno, loginid, password;
public UserDetailsModel(){
public UserDetailsModel(String uname, String address. String contactno. String loginid. String password) { this.uname = uname; this.address = address; this.contactno = contactno; this.loginid = loginid; this.password = password;
}
public String getUname() { return uname;
}
public String getAddress() { return address;
}
public String getContactno() { return contactno;
}
public String getLoginid() { return loginid;
}
}
public String getPassword() { return password;
}
public void setUnaxne (String uname) { this . uname = uname;
)
public void setAddress(String address) { this.address = address;
>
public void setContactno(String contactno) { this.contactno = contactno;
}
public void setLoginid(String loginid) { this.loginid = loginid;
}
public void setPassword(String password) { this.password = password;
}
3. Create a servlet. LoginServlet.java. and replace the existing code with the following code:
import
import
import
import
import
import
import
import
import
import
java.io.BufferedReader ;
java.io.FileReader;
j ava.io.IOException;
java.io.PrintWriter;
javax.servlet.RequestDispatcher;
javax.servlet.ServletException;
javax.servlet.annotation.WebServlet;
j avax.servlet.http.HttpServlet;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = {"/LoginServlet"}) public class LoginServlet extends HttpServlet {
String loginld, password,uname;
UserDetailsModel udm=new UserDetailsModel(); protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8")
PrintWriter out = response.getWriter();
try {
loginId=request.getParameter("loginid"); password=request.getParameter("password");
boolean value=readFile();
if(value){
request.setAttribute("model", udm); RequestDispatcher
rd=request.getRequestDispatcher("WelcomePage.jsp");
rd.forward(request, response)
}
else {
RequestDispatcher
rd=request.getRequestDispatcher("ErrorPage.j sp");
rd.forward(request, response);
>
} finally {
out.close();
>
}
boolean readFile(){
boolean value=£alse; try{
FileReader fr=new FileReader ("ExoticaTravelLogin.txt"); BufferedReader br=new BufferedReader(fr);
String data;
while((data=br.readLine())!=null){
String text()=data.split ("-");
if(text[3].equals(loginld)&& text(4).equals
(password))
{
uname=text(0) ;
udm.setUname(uname);
value=true;
break;
)
else {
value=false;
}
else{
value=false;
}
}
fr.close();
}catch(lOException ioe){
System.out.println(ioe);
}
return value;
}
©Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, lOException { processRequest(request, response);
}
©Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, lOException { processRequest(request, response);
}
©Override
public String getServletlnfo() { return "Short description";
} }
4. Create a servlet. RegistrationServlet.java. and replace the existing cede with the following cede:
import
import
import
import
import
import
import
import
import
import
java.io.BufferedWriter;
java.io.FileWriter;
java.io.IOException;
java.io.PrintWriter;
javax.servlet.RequestDispatcher;
javax.servlet.ServletException;
j avax.servlet.annotation.WebServlet;
javax.servlet.http.HttpServlet;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = {"/RegistrationServlet"}) public class RegistrationServlet extends HttpServlet { String uname, city, contactno, loginid, password;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try {
uname=request.getParameter ("uname") ; city=request.getParameter("city"); contactno=request.getParameter("contactno"); loginid=request.getParameter("loginid"); password=request.getParameter("password"); writeFile () ;
UserDetailsModel udm=new UserDetailsModel (uname,city,contactno,loginid,password);
request.setAttribute("model", udm);
RequestDispatcher
rd=request.getRequestDispatcher("WelcomePage.j sp"); rd.forward(request,response);
) finally {
out.close();
    }
  }
void writeFile(){ try{
FileWriter fw=new FileWriter ("ExoticaTravelLogin.txt",true);
BufferedWriter bw=new BufferedWriter(fw) bw.write (uname+"-"+city+"-"+contactno +"-"+loginid+"^"+password);
bw.newLine(); bw.close(); fw.close();
}catch(IOException ioe){
System.out.println(ioe);
}
}
0Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { processRequest(request, response);
}
@Override
public String getServletlnfo() { return "Short description";
}
5. Add the Car.jpg Hotel.jpg. Tourpackage.jpg and PlanTour.jpg images to the Images fokSer.
6. Create a JSP page. WelcomePage.jsp, and replace the existing code with the following code:
<%8page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html charset=UTF-8">
<title>Welcome Page</title>
</head>
<body>
<hl>Welcome ${model.uname}</hl>
<table border="0" align="center">
<tr>
<td><a
href =' PageUnderConstruction. j sp' Xiang src="Images/Car .jpg" height="150" width="250"x/a></td>
<tdxa
href='PageUnderConstruction.j sp'><img
src="Images/Hotel.jpg" height="lSO" width="2S0"X/aX/td> <td><a
href='PageUnderConstruction.j sp'Xing
src="Images/Tourpackage.jpg" height="150" width="250"X/a> <tdXa
href =' PageUnderConstruction. j sp' Ximg
src="Images/PlanTour. jpg" height="150" width="250"X/a> </tr>
<tr>
<td align="center">Cab Bookings</td>
<td align="center">Hotel Bookings</td>
<td align="center">Tour Package
Bookings</td>
<td align="center">Plan Travel</td>
</tr>
</body>
</htral>
7. Create a JSP page. ErrorPage.jsp. and replace the existing code with the following code:
<%8page contcntType="text/htral" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Error Page</title>
</head>
<body>
<hl>Invalid login</hl>
</body>
</htral>
8. Open the LoginPage.jsp file and replace the statement. <for=>. with the statement. <for= sc?ior.=-L&jir.Ser-.-ier">.
9. Open the RegistrationPage.jsp file and replace the statement.<£or=>. with the statement. <£or= sc^ior.="?.egi:rrsrior.£ervle^">.
10. Run the HomePage.jsp file.


1 comment: