Question

create Java Servlets and deploying them onto a Java Servlet Container. Part 1- Hello World Servlet...

create Java Servlets and deploying them onto a Java Servlet Container.

Part 1- Hello World Servlet For the first part of this assignment, you will deploy a simple “helloworld” servlet to the servlet container. The servlet simply displays “Hello World”.

Part 2– Counter For this part, you will create a servlet that displays a “counter” for the number of times the servlet has been accessed. You should also support resetting the counter back to 0. You should display a button (.e.g., “Reset Counter”) on the page displayed by URL above. When the button is pressed, your servlet should process the POST request and reset the counter to 0.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks.

Download Project:

https://drive.google.com/open?id=1qaSS0MVTLHHeQG6R2oWdE2Uht9x_Y4lB

Code:

HelloWorld.java

package com.HomeworkLib.www;

import java.io.IOException;
import java.io.PrintWriter;

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

/**
* Servlet implementation class HelloWorld
*/
@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
   private static final long serialVersionUID = 1L;
private static long counter=0;
/**
* @see HttpServlet#HttpServlet()
*/
public HelloWorld() {
super();
// TODO Auto-generated constructor stub
}

   /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       // TODO Auto-generated method stub
       //response.getWriter().append("Served at: ").append(request.getContextPath());
       counter++;
       response.setContentType("text/html");
PrintWriter printWriter = response.getWriter();
printWriter.println("<h1>Hello World!</h1><br>Counter :"+counter);
  
printWriter.println("<form name=\"loginForm\" method=\"post\" action=\"HelloWorld\"><input type=\"submit\" value=\"Reset Counter\" /></form>");
   }

   /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       // TODO Auto-generated method stub
       counter=0;
       String referer = request.getHeader("Referer");
       response.sendRedirect(referer);
       //doGet(request, response);
   }

}

Output:

Add a comment
Know the answer?
Add Answer to:
create Java Servlets and deploying them onto a Java Servlet Container. Part 1- Hello World Servlet...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Part I – Build a simple Servlet called MyServlet using NetBeans. Add this Servlet to you...

    Part I – Build a simple Servlet called MyServlet using NetBeans. Add this Servlet to you “ChattBank” Project. This MyServlet will display a message like “Go Braves” in a simple <h1> tag. Run this servlet from a Browser window by typing in the servlet name in the URL line. (ie. http://localhost:8080/ChattBank/MyServlet). Make sure that your Server is up and running before you test this Servlet. The best way to do this is just Run your “ChattBank” Project once before you...

  • QUESTION 6 When you use URL encoding for session tracking, you must 1. a and b...

    QUESTION 6 When you use URL encoding for session tracking, you must 1. a and b above 2. a, b, and c above 3. convert all HTML pages in the application to JSPs 4. use the encodeURL method to encode all URLs in the servlets 5. use the encodeURL method to encode all URLs in the JSPs QUESTION 7 The best way to develop Java web applications is to use an Integrated Development Environment such as: 1. Foxfire or Netscape...

  • JAVA SOLUTION This lab has four parts: Create a window. Create 5 buttons in that window....

    JAVA SOLUTION This lab has four parts: Create a window. Create 5 buttons in that window. Create an event when a button is pushed. Create an event listener that will respond to the button being pushed event. Task 1 – Create a Window For Java: Please ensure that the following import statements are used: import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.GridPane; import javafx.scene.text.Text; import javafx.event.EventHandler; import javafx.scene.input.MouseEvent; Next, ensure that there is a main method which is...

  • Project overview: Create a java graphics program that displays an order menu and bill from a...

    Project overview: Create a java graphics program that displays an order menu and bill from a Sandwich shop, or any other establishment you prefer. In this program the design is left up to the programmer however good object oriented design is required. Below are two images that should be used to assist in development of your program. Items are selected on the Order Calculator and the Message window that displays the Subtotal, Tax and Total is displayed when the Calculate...

  • Java: In this assignment, you will create an accumulator accumulator-calculator that displays a sad face “...

    Java: In this assignment, you will create an accumulator accumulator-calculator that displays a sad face “ :-( “ whenever the number displayed by the calculator is negative and a happy face “ :-) ” whenever the number displayed is positive. The calculator responds to the following commands: num + , num - , num * , num / and C ( Clear ). After initial run, if the user clicks 8 and then presses the “+” button for example, the...

  • There is an example below Now that everything is working you can try the following exercises. To complete them you wi...

    There is an example below Now that everything is working you can try the following exercises. To complete them you will need to refer to the documentation in Appendix A The MiteASM Assembler and Appendix B The MiteFPGA Processor. Write an assembly language program for an over counter for a cricket umpire. This should 1. display a count on the 7-segment display. The count should increase by 1 when button 0 is pressed. It should reset to 0 when button...

  • JAVA Hello I am trying to add a menu to my Java code if someone can...

    JAVA Hello I am trying to add a menu to my Java code if someone can help me I would really appreacite it thank you. I found a java menu code but I dont know how to incorporate it to my code this is the java menu code that i found. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; public class MenuExp extends JFrame { public MenuExp() { setTitle("Menu Example");...

  • in java Part 1 In this section, we relook at the main method by examining passing...

    in java Part 1 In this section, we relook at the main method by examining passing array as parameters. Often we include options/flags when running programs. On command line, for example, we may do “java MyProgram -a -v". Depending on options, the program may do things differently. For example, "a" may do all things while "-v" display messages verbosely. You can provide options in Eclipse instead of command line: "Run ... Run Configurations ... Arguments". Create a Java class (Main.java)....

  • QUESTION 1 What statement would you use to print the phrase “Hello, world” and then start...

    QUESTION 1 What statement would you use to print the phrase “Hello, world” and then start a new line? QUESTION 2 What statement would you use to assign the value 32 to the variable cheeses? QUESTION 3 What statement would you use to read a value from keyboard input into the variable cheeses? QUESTION 4 What statement would you use to print “We have X varieties of cheese,” where the current value of the cheeses variable replaces X? QUESTION 5...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT