lundi 25 avril 2016

Web Application displaying issues after HTTP put request & formatting date. Java

In the web application, logged in users are saved to the session via session.setAttribute("loggedIn");.

I take in various user data into a servlet, validate and then perform a HTTP PUT request and then reset the person object by overwriting the session attribute, "loggedIn".

The issue I am having is that once the user edits their date of birth (dd-mm-yyyy input which is then converted into "dd-MMM-yyyy" for database input) after returning to their profile page, the date of birth field displays nothing.

However, if the user logsout (session.removeAttribute("loggedIn")) and then logs back in, the date displays appropriately. I have tried to auto logout and then login the user upon HTTP put, but it doesnt seem to work.

Thank you for any attempts at answering my question.

Date formatting code:

public String getDobAsFormatted(String input) throws ParseException
{
        //take in yyyy-MM-dd'T'HH:mm:ss
        SimpleDateFormat sdfDate = new SimpleDateFormat("dd-MM-yyyy");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        Date date = sdf.parse(input);
        String formattedDate = sdfDate.format(date);
        //output dd-mm-yyyy
        return formattedDate;
}

public String getDobForDbInput(String input) throws ParseException
{
    //take in dd-mm-yyyy
    SimpleDateFormat sdfDate = new SimpleDateFormat("dd-MM-yyyy");
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");

    Date date = sdfDate.parse(input);
    String formattedDate = sdf.format(date);
    //returns dd-mmm-yyyy
    return formattedDate.toUpperCase();
}

profile.jsp:

                             <% 
                                 Person username = new Person();
    try {
        username = (Person)request.getSession().getAttribute("loggedIn");
        if(username != null)
        {
    %>  <div style=" width:1018px; float:left; padding:35px 50px 15px 50px; background-image:linear-gradient(to bottom,#46C746,#3FB33F); margin-top:20px;">
                    <div style="text-align:center;"><span style="color:white; font-size:35px;"><%= username.getUSERNAME()%>'s Profile</span></div>
        </div>
        <div class="contentContainer" style="height:auto; background-color:white;">
                    <div style="margin:auto; width:700px;">



                        <div class="latestEvents" onMouseOver="show('latestEventsOnHover')" onMouseOut="hide('latestEventsOnHover') " style="height:200px;background-image: url(../images/defaultusericon101.png);background-size: cover;width:205px;border-radius:100px;background-color: #57b8e5;">

                        </div>
                        <div class="eventInfoWrapper" style="float: left;height: 200px;margin: 5px; position:relative; width:400px;">
                                <div class="eventInfo-Name">
                                        <p style="margin:0px; color:black;">First Name: <%= username.getFORENAME()%></p>
                                    </div>
                                    <div class="eventInfo-Venue" style="width:100%; float:left;">
                                        <p style="margin:0px; color:black;">Last Name: <%= username.getSURNAME()%></p>
                                    </div><br>
                                    <div class="eventInfo-Venue" style="width:100%; float:left;">
                                        <p style="margin:0px; color:black;">Email: <%= username.getEMAIL()%></p>
                                    </div><br>
                                    <div class="eventInfo-Date">
                                        <p style="margin:0px; color:black;">Date of Birth: <%= username.getDobAsFormatted(username.getDOB()) %></p>
                                    </div>
                                    <div class="eventInfo-Venue" style="width:100%; float:left;">
                                        <p style="margin:0px;color:black;">Address: <%= username.getADDRESS_LINE()%></p>
                                    </div><br>
                                    <div class="eventInfo-Venue" style="width:100%; float:left;">
                                        <p style="margin:0px; color:black;">Town: <%= username.getTOWN() %></p>
                                    </div><br>
                                    <div class="eventInfo-Venue" style="width:100%; float:left;">
                                        <p style="margin:0px; color:black;">Postcode: <%= username.getPOST_CODE() %></p>
                                    </div><br>
                                    <div class="eventInfo-Venue" style="width:100%; float:left;">
                                        <p style="margin:0px; color:black;">Telephone Number: <%= username.getTELEPHONE_NUMBER()%></p>
                                    </div><br>


                                    <div class="eventInfo-Description">
                                        <p style="margin:0px; color:black;" style="position:absolute;">Username: <%= username.getUSERNAME()%></p>
                                    </div>

                                <%
                                }
                            }
                            catch(Exception e)
                            {

                            }
                            %>

                        </div>
                            <div class="eventBookNowBtn">

                                            <input type="submit" class="editAccountDetailsBtn" value="Edit Account Details" class="btn" style="margin:0px;margin-top: 15px;width: 100%;">


                            </div>
                            <% try{ %>
                            <div class="editAccountDetails" id="NewsAndOffersContent" style="background-color:white; height:auto;">
                                <div style="color:white; text-align:left; margin-left:50px; margin-bottom:40px;">
                                    <form method="post" style="margin:auto; width:300px;" action="/PRDCD_Web_Application/editUser">
                                        <br>
                                        <input type="text" value="<%= username.getFORENAME()%>"  placeholder="First Name" name="firstName" maxlength="20" style="opacity:0.7; color:white;">
                                        <input type="text" value="<%= username.getSURNAME()%>" placeholder="Last Name" name="lastName" maxlength="20" style="opacity:0.7; color:white;">
                                        <input type="text" value="<%= username.getEMAIL()%>" placeholder="Email" name="email" maxlength="50" style="opacity:0.7; color:white;">
                                        <input type="text" value="<%= username.getDobAsFormatted(username.getDOB()) %>" placeholder="Date of Birth" name="dob" maxlength="11" style="opacity:0.7; color:white;">
                                        <input type="text" value="<%= username.getADDRESS_LINE()%>" placeholder="Address" name="address" maxlength="50" style="opacity:0.7; color:white;">
                                        <input type="text" value="<%= username.getTOWN() %>" placeholder="Town" name="town" maxlength="50" style="opacity:0.7; color:white;">
                                        <input type="text" value="<%= username.getPOST_CODE()%>" placeholder="Postcode" name="postcode" maxlength="7" style="opacity:0.7; color:white;">
                                        <input type="text" value="<%= username.getTELEPHONE_NUMBER()%>" placeholder="Telephone Number" maxlength="11" name="telephone" style="opacity:0.7; color:white;">
                                        <br>
                                        <input type="hidden" value="<%= username.getUSER_ID() %>" name="id" class="btn" value="Update">
                                        <input type="hidden" value="<%= username.getUSERNAME()%>" name="username" class="btn" value="Update">
                                        <input type="hidden" value="<%= username.getPASSWORD() %>" name="password" class="btn" value="Update">
                                        <input type="submit" class="btn" value="Update">
                                        <input type="submit" class="editAccountDetailsBtnCancel" value="Cancel" class="btn" style="margin:0px;width: 100%;"> 
                                    </form>
                                </div>
                            </div>
                                        <%}
                                        catch(Exception e){

                                        }%>

editUser servlet:

HttpSession session = request.getSession(true);
             //get user values
             int err = 0;
             String error = "";
             Person accessor = new Person();
             String firstName = request.getParameter("firstName"); //id of event date id
             String lastName = request.getParameter("lastName"); // student, senior, etc
             String email = request.getParameter("email"); // seating, standing
             String dob = request.getParameter("dob");
             String address = request.getParameter("address");
             String telephone = request.getParameter("telephone");
             String town = request.getParameter("town");
             String postcode = request.getParameter("postcode");
             String id = request.getParameter("id");
             String username = request.getParameter("username");
             String password = request.getParameter("password");
             String formattedForDbInput = "";
             int idNumber = Integer.parseInt(id);


         try 
         {
              dob = accessor.getDobForDbInput(dob);
         }
         catch(Exception e) 
         {
             error += "Invalid date format. Did you enter dd-mm-yyyy?.<br>";
             err = 1;  
         }

         //validation


    if(lastName.equals(""))
    {
        error += " Empty last name. ";
        err = 1;  
    }
    else if(lastName.length() < 2) 
       {
           error += "Invalid Last Name, must be at least 2 characters long.<br>";
           err = 1;  
       }
       else if (lastName.matches(".*\\d.*"))
       {
           error += "Invalid Last Name, cannot contain numbers.<br>";
           err = 1;  
       }
       else if(lastName.contains(" ")) 
       {
           error += "Invalid Last Name. Cannot contain whitespace.<br>";
           err = 1;  
       }

    if(postcode.equals(""))
       {
           error += "Invalid Postcode<br>";
           err = 1;   
       }
       else if(postcode.length() < 6)
       {
           error += "Invalid Postcode, must be at least 6 characters long.<br>";
           err = 1;
       }
       //regex doesnt work
       else if(!postcode.matches("[a-zA-Z0-9]*$"))
       {
           error += "Invalid Postcode. Must contain letters & numbers.<br>";
           err = 1;
       }

    if(town.equals(""))
       {
           error += "Invalid Town<br>";
           err = 1; 
       }

    if(email.equals(""))
    {
        error += " Empty email. ";
        err = 1;
    }
    else if(!email.contains("@"))
       {
           error += "Invalid Email<br>";
           err = 1;
       }
       else if(!email.contains("."))
       {
           error += "Invalid Email<br>";
           err = 1;
       }
       else if(email.length() < 6)
       {
           error += "Invalid Email. Must be at least 6 characters long.<br>";
           err = 1;
       }
       else if(email.startsWith("@"))
       {
           error += "Invalid Email.<br>";
           err = 1;  
       }
       else if(email.startsWith("."))
       {
           error += "Invalid Email.<br>";
           err = 1;  
       }

     if(dob.equals(""))
       {
           error += "Invalid Date of Birth.<br>";
           err = 1;
       }
       else if(dob.length() < 10)
       {
           error += "Invalid Date of Birth<br>";
           err = 1;
       }

    if(address.equals(""))
       {
           error += "Invalid Address<br>";
           err = 1; 
       }

    if(firstName.equals(""))
    {
        error += " Empty First Name. ";
        err = 1;
    }
    else if(firstName.length() < 2) 
       {
           error += "Invalid First Name, must be at least 2 characters long.<br>";
           err = 1;  
       }
       else if (firstName.matches(".*\\d.*"))
       {
           error += "Invalid First Name, cannot contain numbers.<br>";
           err = 1;  
       }
       else if(firstName.contains(" ")) 
       {
           error += "Invalid First Name. Cannot contain whitespace.<br>";
           err = 1;  
       }

       if(telephone.equals(""))
       {
           error += "Invalid Telephone Number<br>";
           err = 1;  
       }
       else if(!telephone.startsWith("0"))
       {
           error += "Invalid Telephone Number, must start with 0.<br>";
           err = 1;  
       }
       else if (telephone.length() < 11)
       {
           error += "Invalid Telephone Number, must have a length of 11 digits.<br>";
           err = 1; 
       }

         if(err == 0)
         {
         //update person
         Person update = new Person();
         update.setFORENAME(firstName);
         update.setSURNAME(lastName);
         update.setEMAIL(email);
         update.setDOB(dob);
         update.setADDRESS_LINE(address);
         update.setTELEPHONE_NUMBER(telephone);
         update.setTOWN(town);
         update.setPOST_CODE(postcode);
         update.setUSER_ID(idNumber);
         update.setUSERNAME(username);
         update.setPASSWORD(password);
         update.updatePerson(update);

         session.setAttribute("loggedIn", update);
         response.sendRedirect("/PRDCD_Web_Application/web/html/profile.jsp?suc=" + URLEncoder.encode("Update Successful.", "UTF-8")); 
         }
         else {
             response.sendRedirect("/PRDCD_Web_Application/web/html/profile.jsp?invalid=" + URLEncoder.encode(error, "UTF-8")); 
         }




Aucun commentaire:

Enregistrer un commentaire