JSP questions

R

redpanda

have some questions about JavaServer Pages to ask

1. I made several input fields for user to input the height, width and
other stuff to create a table. how can I make the font in color red in
first row and first column automaticly and font in other rows and
columns remain the same color?

2. I want to add a title inside the table that need to colspan the
rest of the table. Becasue I don't know what # the user will input for
columns, so I can't set it before user input. so plz help me on this.

3. Randomly place an input field in one of the cells – a different
cell on each refresh. If the user enters the correct product there and
changes the focus (by clicking outside the field), a Javascript
provided by the jsp page checks the answer and makes appropriate alert

My jsp file looks like this:

<HTML>
<head></head>
<BODY>

<%
String s2 = request.getParameter("Bsize") ;
s2 = s2.trim();
out.print("<table bgcolor=silver border= ");
out.print(s2);
out.print(" ");
String s3 = request.getParameter("Bcolor") ;
s3 = s3.trim();
out.print("bordercolor= ");
out.print(s3);
out.print(">");

String m1 = request.getParameter("mName") ;
m1 = m1.trim();
int M = Integer.parseInt(m1);

String n1 = request.getParameter("nName") ;
n1 = n1.trim();
int N = Integer.parseInt(n1);

String s = request.getParameter("FullName") ;
s = s.trim();
out.println("<font color=red size=6>");
out.println(s);
out.println("'s table is: </font>");

out.println("<tr colspan= ");

out.print(m1);

out.println(">");
out.print("<th> Numbers </th> </tr>");


for (int i =1; i<= M; i++ )
{
out.println("<tr width= 10 >");

for (int j =1; j<= N; j++ )
{
out.println("<th >");
out.println(j * i);
out.println("</th>");
}

out.println("</tr>");

}
%>

</table>
 
J

John C. Bollinger

redpanda said:
have some questions about JavaServer Pages to ask

1. I made several input fields for user to input the height, width and
other stuff to create a table. how can I make the font in color red in
first row and first column automaticly and font in other rows and
columns remain the same color?

With JSP, always focus on the output. Most JSPs, including yours,
output HTML code. Determine how to code the visual effects you want in
HTML, then make your JSP produce that output. The HTML details are
off-topic for this newsgroup, but your main options are to use Cascading
Style Sheets (preferred) or to insert appropriate styling markup (e.g.
2. I want to add a title inside the table that need to colspan the
rest of the table. Becasue I don't know what # the user will input for
columns, so I can't set it before user input. so plz help me on this.

I don't see how you expect it to work at all, then. When the JSP is
processing a request it has only the information available from the
request, session, and application. The JSP cannot presciently determine
how the client might react to the response, and it cannot do anything to
change the response once it is sent.

However, if you can create a table with the correct number of columns in
the first place then you necessarily know some number of columns that
the client *already* requested. Either you're missing something here or
I am.
3. Randomly place an input field in one of the cells – a different
cell on each refresh. If the user enters the correct product there and
changes the focus (by clicking outside the field), a Javascript
provided by the jsp page checks the answer and makes appropriate alert

So what's the problem?

If you need help with the Javascript then this is the wrong group.
Javascript is a completely separate language from Java, with only
superficial similarities.

If you need help generating a random number, then look into
java.util.Random and / or Math.random().

If you need help creating an input field then see my above comments
about HTML.
My jsp file looks like this:

<HTML>
<head></head>
<BODY>

<%
String s2 = request.getParameter("Bsize") ;
s2 = s2.trim();
out.print("<table bgcolor=silver border= ");
out.print(s2);
out.print(" ");
String s3 = request.getParameter("Bcolor") ;
s3 = s3.trim();
out.print("bordercolor= ");
out.print(s3);
out.print(">");

String m1 = request.getParameter("mName") ;
m1 = m1.trim();
int M = Integer.parseInt(m1);

String n1 = request.getParameter("nName") ;
n1 = n1.trim();
int N = Integer.parseInt(n1);

String s = request.getParameter("FullName") ;
s = s.trim();
out.println("<font color=red size=6>");
out.println(s);
out.println("'s table is: </font>");

out.println("<tr colspan= ");

out.print(m1);

out.println(">");
out.print("<th> Numbers </th> </tr>");


for (int i =1; i<= M; i++ )
{
out.println("<tr width= 10 >");

for (int j =1; j<= N; j++ )
{
out.println("<th >");
out.println(j * i);
out.println("</th>");
}

out.println("</tr>");

}
%>

</table>

That is truly abominable JSP code. Sorry, but it is. See all those
out.print() and out.println() invocations? One of the original goals of
JSP was to not have to write servlet code that looks like that. Why are
you even bothering with JSP (instead of a pure servlet) if you're going
to write it like that? Also, most interested people agree that JSP
authors ought to focus on minimizing the use of scriptlets in their JSPs
[some even argue that scriptlets should be avoided altogether], but your
JSP is almost all one big scriptlet. Furthermore, unless you've
truncated it, your JSP doesn't even produce a valid HTML document. And
that's before we even get to not putting your HTML attribute values in
quotes, not using consistent coding conventions in your scriptlet code,
yada yada yada.


John Bollinger
(e-mail address removed)
 
S

Sudsy

John C. Bollinger wrote:
That is truly abominable JSP code. Sorry, but it is. See all those
out.print() and out.println() invocations? One of the original goals of
JSP was to not have to write servlet code that looks like that. Why are
you even bothering with JSP (instead of a pure servlet) if you're going
to write it like that? Also, most interested people agree that JSP
authors ought to focus on minimizing the use of scriptlets in their JSPs
[some even argue that scriptlets should be avoided altogether], but your
JSP is almost all one big scriptlet. Furthermore, unless you've
truncated it, your JSP doesn't even produce a valid HTML document. And
that's before we even get to not putting your HTML attribute values in
quotes, not using consistent coding conventions in your scriptlet code,
yada yada yada.

Count me among those who eschew scriptlets. Either use existing tag
libraries or roll your own. It's not that difficult and makes the JSP
so much easier to read. It's also easier on your graphic artists when
you explain that they just need to use a particular tag to obtain
desired functionality. This will be familiar since HTML is merely tag
markup. Embedded Java code just makes their heads spin... ;-)
 
H

Hal Rosser

<html>
<head>
<style>
.makeRed {color:red;}
</style>
</head>
<Body>
......in the body use if statement in the code
if the condition warrants red text then have the code make the <td> tag like
this
<td class='makeRed>
.... there's a free css tutorial on www.w3schools.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top