QuestionQuestion

In this assignment you will create a Web Application that will allow Users to opt-in to receiving e-mail updates regarding our course. To do so, a User must provide their full name and e-mail address. Upon successful submission, your application will display a welcome message informing your User that their sign-up was successful.
Your application will keep track of all successful sign-ups. To do so, you will create a class to model User information. Moreover, you will leverage the Application Scope (ServletContext) to store a collection of User information (objects).
Lastly, you will create an administrative view that will allow a site administrator the ability to view a complete list of all Users that opted-in to receive our newsletter.
Note: We are not focusing on authentication or permissions in this assignment. Therefore, any user who visits the admin view will be permitted to view the list of all Users on our mailing list.
NewsletterSignupServlet
In this assignment you will create a servlet named NewsletterSignupServlet.
Your servlet should be located at:
You can find the HTML (homework2.html) for this document under the examples section of our Class Webpage, or you can directly download it here.
Below is an example screenshot of the NewsletterSignupServlet.
Note: My expectation is that you will use the HTML provided to generate the output for your servlet. Specifically, the IDs and Names of form elements should match those in the sample source code.
Requirements
Your NewsletterSignupServlet servlet should present a form with two input fields named name and email, respectively. Upon submission, your form should submit all form data back to the NewsletterSignupServlet via a POST request. The HTML required to do this has already been provided in the homework2.html example.
All validation MUST be done on the Server. You are not to use any HTML attributes, or JavaScript, to validate any input on the Client. Client-Side validation will result in a severe loss of points.
Full Name
The first input field, name, will collect the Users first and last names. Notice that the names are to be entered into a single text input box. That means you will have to parse the first and last names out of the single input in your Server-Side Code.
You must validate the name by ensuring the following:
The User enters two names that are at least 2 characters in length.
You can assume that names are delimited by one or more whitespace characters.
You can also assume that, if more than two names are provided, the first word is the Users first name. All subsequent words should be considered the Users last name.
E-mail
The second input field, email, will collect the Users e-mail address.
You must validate the e-mail by ensuring the following:
The User must enter input that resembles an e-mail address. I recommend using a regular expression to validate the input.
Error Messages
If the User enters data that is invalid based on the criteria above, you should re-display the form with an error message identifying each issue.
Below is an example of what your form may look like if the User specifies an invalid name and email address:
You can find the HTML (homework2-error.html) under the examples section of our Class Webpage, or you can directly download it here.
Note: The HTML is almost identical to the homework2.html file. The differences are outlined below:
Lines 19 & 28: <div class="form-group has-error">
The addition of the .has-error class.
Line 26: <p class="text-danger">Please enter your first and last names.</p>
The addition of an error message wrapped with a paragragh tag.
Note the .text-danger class that is introduced in the paragraph tag.
Line 25: <p class="text-danger">Please enter a valid e-mail address.</p>
The addition of an error message wrapped with a paragragh tag.
Note the .text-danger class that is introduced in the paragraph tag.
Sticky Form Elements
If the User submits a mixture of valid and invalid input, you should not require the User to re-enter the valid data. In other words, your form should be sticky, in the sense that valid input is retained across form submissions.
Moreover, if a field is valid, you should not display an error message for said field.
Also, if a field is invalid, you should not make the erroneous input sticky.
Successful Submission
Below is an example of valid input:
Once the form is submitted, and all fields have been successfully validated, you should instantiate a new Homework2User object using the information submitted by the User.
Then, you should add the new Homework2User object to a collection of Homework2User objects stored in the Application Scope. This collection should be named MailingList.
Finally, you should display a message indicating a successful submission. Below is an example of the success message:
You can find the HTML (homework2-registered.html) under the examples section of our Class Webpage, or you can directly download it here.
Note: The HTML is almost identical to the homework2.html file. The differences are outlined below:
Line 14: The contents of <div class="well text-center"> were replaced. Refer to the HTML examples provided.
Line 20: Added <a href="NewsletterAdminServlet">View All E-mails</a>
This anchor tag links to your NewsletterAdminServlet.
NewsletterAdminServlet
You are to create a second servlet named NewsletterAdminServlet. This servlet will allow a visitor to view a list of all Users who are on the mailing list.
Below is an example of the Mailing List:
You can find the HTML (homework2-list.html) under the examples section of our Class Webpage, or you can directly download it here.
Note: The HTML in this document is significantly different from the HTML in your NewsletterSignupServlet. The differences are outlined below:
Line 8: We are now including main2.css instead of main.css
Line 13: We replaced the .bounceInDown CSS class with .fadeInUp.
Line 14: The div has changed to <div class="well">
The contents of this div have been changed drastically. Refer to the examples provided.
Line 19: All rows should be dynamically generated using the data stored in the MailingList collection stored in your Application Scope.
Line 46: A hyperlink <a href="NewsletterSignupServlet">Back to Sign-Up</a> is provided that links back to the NewsletterSignupServlet.

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.GregorianCalendar;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;




@WebServlet(urlPatterns = "/cs320stuXX/Homework2/NewsletterSignup", name = "NewsletterSignupServlet")
public class NewsletterSignupServlet extends HttpServlet {

ArrayList<Homework2User> hw2users;
       private String name, email;
      
       private String thisUrl = "/cs320stuXX/Homework2/NewsletterSignup";
       private String urlAdmin = "/cs320stuXX/Homework2/NewsletterAdmin";

public void init( ServletConfig config ) throws ServletException
    {
       super.init( config );
       hw2users = new ArrayList<Homework2User> ();
       name="";
       email="";
    }

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
   
    response.setContentType("text/html");
       PrintWriter out = response.getWriter();

   out.println(this.initForm(null));
   out.close();
}

       @Override
protected void doGet
(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
      
processRequest(request, response);

}

    /**
    *
    * @param request
    * @param response
    * @throws javax.servlet.ServletException
    * @throws java.io.IOException
    */
    @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
         
    int isOk = 0;
   
       PrintWriter out = response.getWriter();

       name = request.getParameter("name");
       email = request.getParameter("email");
   
       if(!isFullname(name)) isOk ++;

       if (!isValidEmailAddress(email)) isOk += 2;
      
       if(isOk != 0) {
       if(isOk == 1) out.println(this.initForm("name"));
       else if (isOk == 2) out.println(this.initForm("email"));
       else out.println(this.initForm("both"));
       }
      
       else
       {
      
       String firstName = "";
       String lastName = "";
      
       String[] fl = name.split(" ");
      
       firstName = fl[0];
      
       for(int i=1; i<fl.length; i++) {
       lastName += fl[i] + " ";
       }
      
       Homework2User newUser = new Homework2User (firstName, lastName, email, new GregorianCalendar());
      
       if(getServletContext().getAttribute("MailingList" ) != null) {
       hw2users = (ArrayList<Homework2User>) getServletContext().getAttribute( "MailingList" );
       }
      
       hw2users.add(newUser);
       getServletContext().setAttribute("MailingList", hw2users);

      
       out.println(this.initRegisteredForm());
       // response.sendRedirect("/HomeWork1/Login");...
$20.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Web Development (HTML, XML, PHP, JavaScript, Adobe, Flash, etc.) Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats