0

Alright well im trying to code a multiplication table for a webpage im making. This needs to be in a separate java Servlet as opposed to an inline javascript.

Basically the code should output embedded html code something like

<table>
<tr><td>4*0=0</td><td>4*1=4</td><td>4*2=8</td></tr>
<tr><td>5*0=0</td><td>5*1=5</td><td>5*2=10</td></tr>
<tr><td>6*0=0</td><td>6*1=6</td><td>6*2=12</td></tr>
</table>

There is an HTML Form with a beginning number prompt and ending number prompt and a limit denoting the number of multiplications per row.


i think i have arrived at the logical code for the parameter obtaining, but am having some trouble with the actual implementation of how to carry out the input and result in javascript.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

Public class Table extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse res)
		throws ServletException, IOException {	

public class Counter extends HttpServlet {
  private int v;
  private List<String> starts;
  private List<String> ends;
  private List<String> mults;

 public void init() {
    v = 0;
    starts = new ArrayList<String>();
    ends = new ArrayList<String>();
	mults = new ArrayList<String>();
  }
  
  public void doGet(HttpServletRequest req, HttpServletResponse res) 
    throws ServletException, IOException {
    
    String start = req.getParameter("start");
    String end = req.getParameter("end");
	String mult = req.getParameter("mult");
	
	 if (!start.isEmpty()) {
      v += Integer.parseInt(start);
      starts.start(start);
    }
	
	 if (!end.isEmpty()) {
      v += Integer.parseInt(end);
      ends.end(end);
    }
	
	 if (!mult.isEmpty()) {
      v += Integer.parseInt(mult);
      mults.mult(mults);
    }
	
	PrintWriter o = res.getWriter();
    String r =

NOTE: i dont know if that logically sound...thats why im looking for help. Thanks in advance to anyone who takes the time.

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by Razultull
0

You could use your servlet to display the pagevia your PrintWriter. Just write a string of HTML head and body to it. Try it by writing some html string to the writer first, so you would at least see what would be displayed on the web.

0

You could use your servlet to display the pagevia your PrintWriter. Just write a string of HTML head and body to it. Try it by writing some html string to the writer first, so you would at least see what would be displayed on the web.

That is absolutely wrong. Don't write html inside servlets. Even if you can do it, you shouldn't.

And if you want dynamic html, you can use this code in a jsp:

<table>

<%
for (int i=1;i<=10;i++) {
%>

  <tr>
    <td>
       <%=i%>*0 = <%=i*0%>
    </td>
    <td>
       <%=i%>*1 = <%=i*1%>
    </td>
    <td>
       <%=i%>*2 = <%=i*2%>
    </td>
    ....
  </tr>

<%
}
%>

</table>

Try executing that code as a test and see what happens. You can have the <td> tags in a for loop as well:

<%
for (int j=0;j<=10;j++) {
%>

    <td>
       <%=i%>*<%=j%> = <%=i*j%>
    </td>

<%
}
%>

Instead of having multiple <td> . Just submit the first page to a JSP and take the parameters that you need and use them to determine the upper limits of the for loops.
I don't believe that a servlet is necessary since all you do display data

0

Hey thank you for your detailed reply. However i would like to achieve that dynamic XHTML generation in the servlet itself, based off input values by the user (start, end, limit being the number of multiplications per row.)


The output im looking for is something like this:

<table>
<tr><td>4*0=0</td><td>4*1=4</td><td>4*2=8</td></tr>
<tr><td>5*0=0</td><td>5*1=5</td><td>5*2=10</td></tr>
<tr><td>6*0=0</td><td>6*1=6</td><td>6*2=12</td></tr>
</table>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.