Question

Java servlet

Create a multiplication table using a servlet The first request to the page should get this response: # 2|13||4 123 4 The sec

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

Note:-  In place of com.june19 write your package name.

package com.june19;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.Servlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/table")
public class Table extends HttpServlet implements Servlet {
static final long serialVersionUID = 1L;

public Main() {
super();

}

//Here we want to display the table.So we will write our logic in doGet method which represents the get request.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// Response will be in text/html format.
// Other type we can send is XML,JSON etc..

response.setContentType("text/html");

// Outpr object will be used to send the reponse back to Html page from where the servlet is called.
PrintWriter outpr = response.getWriter();

String title = "Table";
outpr.println("<html>");
outpr.println("<head>");
outpr.println("<title>" + title + "</title>");
outpr.println("</head>");
  



try {

// Taking the request parameter from the http servlet request.

int number = Integer.parseInt(request.getParameter("number"));


int num = 1;

outpr.println("<body");
outpr.print("<table border=1>");

// Code for top row which holds # and 1 to 5.
outpr.print("<tr>");
outpr.print("<th>#</th>");

for (int i = 1; i <= 5; i++) {

outpr.print("<th>" + i + "</th>");
}
outpr.print("</tr>");
  
// Code from second row to number + 1 rows.
while(num<=number){
  
outpr.print("<tr>");
outpr.print("<td>" + num + "</td>");
for(int i = 1; i<=5; i++){
outpr.print("<td>" + num * i+ "</td>");

}

outpr.print("</tr>");   

}

outpr.print("</table>");
  
} catch (NumberFormatException e) {
outpr.println("<body><h1>Enter in number format only!!!! DONT ENTER TEXT !!!!</h1></body>");
}

outpr.println("</body>");
outpr.println("</html>");

}

}

Add a comment
Know the answer?
Add Answer to:
Java servlet Create a multiplication table using a servlet The first request to the page should...
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...

  • 484 Core Concepts of Accounting Information Systems a. Using Access, create an employee table sim- ilar...

    484 Core Concepts of Accounting Information Systems a. Using Access, create an employee table sim- ilar to the one shown in Figure 15-4. Create created. (Hint You can use the aforemen- tioned expression for the department code as a validation rule.) at least one record for this table b. Create validation rules for the listed restric tions and an accompanying validation text for each. Document your work by provid- ing a table outlining exactly what rules you c. Test each...

  • I need help creating a netbeans application and linking database with steps Part II – Next,...

    I need help creating a netbeans application and linking database with steps Part II – Next, build a simple Servlet called LoginServlet in your “Bank” Project. Now make it so that when the Customer logs in, the LoginServlet will get called and will validate the user id and password. At first, just make sure that the Servlet gets called correctly. So just print a simple message like “LoginServlet Running…”. Remember, to call the LoginServlet, you will need to modify the...

  • Using Python, Can someone please assist in the following: These are the hints: Summary This week's lab is to create a simple multiplication table using nested loops and if statements. Prompt the...

    Using Python, Can someone please assist in the following: These are the hints: Summary This week's lab is to create a simple multiplication table using nested loops and if statements. Prompt the user for the size of the multiplication table (from 2x2 to 10x10). Use a validation loop to display a warning if the number is less than 2 or greater than 10 and prompt the user to enter the data again until they enter a valid number Put a...

  • PHP Programming In this project, you will create a Web page that allows visitors to your...

    PHP Programming In this project, you will create a Web page that allows visitors to your site to sign a guest book that is saved to a database. Create a new document in your text editor and type the <!DOCTYPE> declaration, <html> element, document head, and <body> element. Use the strict DTD and “Guest Book” as the content of the <title> element. Add the following text and elements to the document body: <h2>Enter your name to sign our guest book</h2>...

  • Java Project In Brief... For this Java project, you will create a Java program for a...

    Java Project In Brief... For this Java project, you will create a Java program for a school. The purpose is to create a report containing one or more classrooms. For each classroom, the report will contain: I need a code that works, runs and the packages are working as well The room number of the classroom. The teacher and the subject assigned to the classroom. A list of students assigned to the classroom including their student id and final grade....

  • Rationale The focus of the project is to develop your database programming skills. This project will...

    Rationale The focus of the project is to develop your database programming skills. This project will help you get a fair idea of the sales and distribution system in any organization that has a chain of Carrying and Forwarding Agents (CFAs) or super stockists and stockists. You will be able to implement database programming concepts of ADO.NET in VB.NET and ASP.NET to create a real-life, web-based database application. (VB stands for Visual Basic.) Scenario Smooth Pen, Inc., a pen manufacturing...

  • Create a java class Customer.java that will represent a water company customer. It should contain the...

    Create a java class Customer.java that will represent a water company customer. It should contain the attributes, constructors, and methods listed below, and when finished should be able to allow the included file TestWaterBills.java to work correctly. Customer class Attributes firstName: String type, initial value null lastName: String type, initial value null streetAddress: String type, initial value null city: String type, initial value null state: String type, initial value null zip: String type, initial value null previousMeterReading: int type, initial...

  • Using Swift playground and / or the command line for macOS (open Xcode, create a new...

    Using Swift playground and / or the command line for macOS (open Xcode, create a new Xcode project, macOS, command line tool), practice the following exercises: Exercise: Swift Variables Declare 2 variables/constants with some random values and any name of your choice Considering these 2 variables, one as a value of PI and other as a radius of circle, find the area of circle Print the area of circle Declare a string variable explicitly with value “Northeastern”. Declare a string...

  • Programming Project 3 See Dropbox for due date Project Outcomes: Develop a Java program that uses:...

    Programming Project 3 See Dropbox for due date Project Outcomes: Develop a Java program that uses: Exception handling File Processing(text) Regular Expressions Prep Readings: Absolute Java, chapters 1 - 9 and Regular Expression in Java Project Overview: Create a Java program that allows a user to pick a cell phone and cell phone package and shows the cost. Inthis program the design is left up to the programmer however good object oriented design is required.    Project Requirements Develop a text...

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