JSP Methods

K

Ken

Hello group,

I'm new to JSP and was having trouble creating Methods in JSP to
output html, I managed to solve it before posting. So I'll share my
obtuseness and ask another question instead.

Now I realize that the javax.servlet.jsp.JspWriter object seems to be
local to _jspService... So to get at least printing with out.print() I
would need to pass in "out" the javax.servlet.jsp.JspWriter object.
However that requires print statements instead of the normal JSP
way... I would much rather have the following work:

<%!
public void printTableSubTotal(){
%>
<p>This is a test.</p>
<%
};
%>

See what I did wrong, and was fighting with for an hour?

The second <% also needs to be <%!... Arg, the simple things. Ok
so... if I need other objects such as request, JspWriter and response
I would need to pass those in too. Just out of curiosity can I
somehow declare these at the start of the JSP page and make them
available to all my methods or would I need to derive my own JSPBase
Class... Since I am using netbeans the specific class is:

public final class ViewBatchLines_jsp extends
org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent

So make my own base class. Is it easy to change which class is used
for the JSP's? Is this desirable for considerations which I may not
have thought of?

Any other advice on output with JSP that people found useful to them?

Oh one more thing but it is IDE specific: Netbeans HTML error checking
flags the HTML 5 tag <tfoot> as an error. Does anyone know how to
change this? I know I can turn off the error checking all together,
but I would rather know how to correct this.
 
K

Ken

<%!
public void printTableSubTotal(){
%>
<p>This is a test.</p>
<%};

%>

Ah! I thought this would work because it stopped giving me compiler
errors... What it does is not what I expected.

I will strip out the paragraph and print it where ever it occurs
ignoring the method all together, so I am back out needing JspWriter.
Very Ugly!
 
K

Ken

Why do you need JspWriter?  What are you actually trying to do?

Here is an example program:

======================= START EXAMPLE =======================
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%! public void printThree(javax.servlet.jsp.JspWriter out)
throws java.io.IOException{
out.print("<p>3</p>");
%>
<p>1'st method</p>
<%!
};/*End printThree*/
%>

<%! public void printTwo(javax.servlet.jsp.JspWriter out)
throws java.io.IOException{
out.print("<p>2</p>");
%>

<p>2'nd Method</p>

<%!
};/*End printTwo*/
%>

<%! public void printOne(javax.servlet.jsp.JspWriter out)
throws java.io.IOException{
out.print("<p>1</p>");
%>
<p>3'rd method</p>
<%!
};/*End printThree*/
%>

<!-- Call The methods - YES I SHOW UP IN THE HTML -->

<%
printOne(out);
printTwo(out);
printThree(out);
%>
</body>
</html>
======================= END EXAMPLE =======================

To me I would expect all the output from the methods (including the
paragraphs "1'st method", "2'nd method", etc) to show up after the
HTML Comment. Instead...

*********************** START Actual Output to Screen (Not raw html
for readability ) ***********************
1'st method

2'nd Method

3'rd method

1

2

3

*********************** START Actual Output ***********************

What I was expecting was:

1

1'st method

2

2'nd Method

3

3'rd method


What I am trying to do is output a table. The table is wide and very
long. The company has been using this report for a long time and is
quite used to it but they would like to see it on a web page instead
of printed. The report has a number of lines for a shift and then a
subtotal, then more lines and another subtotal... finally a grand
total. The table often contains a great number of lines but it may
also just contain 1 or none... So the table has a couple conditions in
it and the logic has created a good chunk of repeating html (which was
a pain to maintain and is the reason we invented methods/functions to
factor this stuff out...) I'm just having a really challenging time
trying to apply the fundamental idea of a method to a JSP.

My first approach was to use a JSP include for the common parts, but
the content is dynamic so that didn't work because the include needed
to modify the calling methods variables and passing the data made a
huge calling block on top of that. So I started to wonder about beans
and session variables but I see it as a mechanics for passing data to
other object, which I don't feel I need to do.

On a screen with syntax highlighting, the Java and HTML are kept
distinct and relaxing on my eyes. It would be nice if it is possible
to maintain that.
 
K

Ken

Wow... Correction...
The desired output should be:

1

3'rd method


2

2'nd Method

3

1'st method
 
M

markspace

Ken said:
<body>
<%! public void printThree(javax.servlet.jsp.JspWriter out)
throws java.io.IOException{
out.print("<p>3</p>");
%>
<p>1'st method</p>
<%!
};/*End printThree*/
%>

This is just weird. Why not:

<body>
<%!

public void printThree( Writer out ) {
out.print( "3");
}

public void printTwo( ...
}

public void printOne( ...
}

%>
..... etc.
</body>

?

Try it, you might like it. Let's clean up that code so folks can see
what is going on, I'm pretty lost to be honest. I can't follow it.

What I am trying to do is output a table. The table is wide and very
long. The company has been using this report for a long time and is


It would also be helpful to see the actual code you have written to
print your table. The JSP parts, I mean. You should also consider some
custom tag handlers, but let's try to get the JSP squared away first.
 
J

Jim

Here is an example program:

You're not quite getting what's happening when your JSP is translated
into a .java file. Take a look at the work file to see what happens,
it can be enlightening (in Tomcat, under Linux, the work (.java) file
s/b in someplace like /usr/local/tomcat/work/Catalina/...).

As you noticed, your page is essentially the following, which makes
the output a little less confusing:

<%!
public void printThree(javax.servlet.jsp.JspWriter out)
throws java.io.IOException {
out.print("<p>3</p>");
};/*End printThree*/

public void printTwo(javax.servlet.jsp.JspWriter out)
throws java.io.IOException{
out.print("<p>2</p>");
};/*End printTwo*/

public void printOne(javax.servlet.jsp.JspWriter out)
throws java.io.IOException{
out.print("<p>1</p>");
};/*End printThree*/
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<p>1'st method</p>
<p>2'nd Method</p>
<p>3'rd method</p>
<%
printOne(out);
printTwo(out);
printThree(out);
%>
</body>
</html>
 
L

Lew

You're not quite getting what's happening when your JSP is translated
into a .java file. Take a look at the work file to see what happens,
it can be enlightening (in Tomcat, under Linux, the work (.java) file
s/b in someplace like /usr/local/tomcat/work/Catalina/...).

As you noticed, your page is essentially the following, which makes
the output a little less confusing:

<%!
  public void printThree(javax.servlet.jsp.JspWriter out)
    throws java.io.IOException {
    out.print("<p>3</p>");
  };/*End printThree*/

  public void printTwo(javax.servlet.jsp.JspWriter out)
    throws java.io.IOException{
    out.print("<p>2</p>");
  };/*End printTwo*/

  public void printOne(javax.servlet.jsp.JspWriter out)
    throws java.io.IOException{
    out.print("<p>1</p>");
  };/*End printThree*/
%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
    <title>JSP Page</title>
  </head>
  <body>
      <p>1'st method</p>
      <p>2'nd Method</p>
      <p>3'rd method</p>
      <%
        printOne(out);
        printTwo(out);
        printThree(out);
      %>
  </body>
</html>

All of this is such fugly JSP code! Yecch.

JSPs are primarily for handling the view component of a web app. You
really shouldn't have any scriptlet in them, much less be messing
around with method definitions.

That you find yourself having to look at the precompiler .java output
is telling. I suggest either using JSTL and EL (they let you write
functions) or skipping JSP and writing the code as a .java-sourced
servlet.
 
K

Ken

All of this is such fugly JSP code!  Yecch.

JSPs are primarily for handling the view component of a web app.  You
really shouldn't have any scriptlet in them, much less be messing
around with method definitions.

That you find yourself having to look at the precompiler .java output
is telling.  I suggest either using JSTL and EL (they let you write
functions) or skipping JSP and writing the code as a .java-sourced
servlet.

Wow thanks guys... You see I was thinking that I could use
Declarations ( <%! ) in a similar way as Expressions ( <% ).

So you can use expressions this way...
<% if (someexpression) { %>
PRINT A BUNCH OF HTML
<% } /* End If */ %>

So I was trying to include HTML in Declarations the same way. If you
look back, you'll see why I expected the output to be completely
different and why my syntax was so strange, I was under the impression
that the out.print statements were not needed at all just the html
that was sandwiched between.

Lews suggestion of using JSTL or EL seems to be just the ticket. I
have not used either although I have used a little xslt for a couple
projects. I was looking at how to make what was in front of me work
and that is just what outsiders are for... expanding the box (and
quickly at that!). My orbit has been disturbed enough I am off on a
productive trip flying towards whatever my next fallacy will be.

If anyone has a favorite JSTL/EL learning site I'd like to hear it.
I'll probably start down what ever is offered by Sun. Thanks again.
 
R

Roedy Green

<%!
public void printTableSubTotal(){
%>

Don't do this. Keep your procedural code out of the .jsp files. Put
it in the code that implements your tags, e.g. the code that extends
SimpleTagSupport. I know it is legal to embed any sort of java in the
JSP, but lots of things are legal that are not a good idea, (like
eating a dozen donuts).


--
Roedy Green Canadian Mind Products
http://mindprod.com

On two occasions I have been asked [by members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
~ Charles Babbage (born: 1791-12-26 died: 1871-10-18 at age: 79)
 
M

markspace

Ken said:
So you can use expressions this way...
<% if (someexpression) { %>
PRINT A BUNCH OF HTML
<% } /* End If */ %>


Yes, but you can get shot for that.

First, these are not expressions, they're scriptlets. An expression
would be:

<%= Math.random() %>

Note the = at the start of that code. Also note the lack of a semicolon
at the end.

Second, you should consider something like:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html><body>
<table>

<c:if test="${some.expression()}" >
<tr><td> Optional table stuff </td></tr>
</c:if>

</table>
</body></html>

Which I think is the JSTL and EL that Lew is referring to. I still
think a custom tab is better, but you should get something working
first, then transform it to a custom object. Standard actions like c:if
are only a half measure, imo, because it's still conditional logic, just
not Java. Custom tags are the whole enchilada.
 
M

markspace

Stefan said:
I don't want to get shot, but... I've never quite understood why using
logic via taglibs is considered superior to logic via scriptlets. Just


Just the fact that you aren't embedding actual Java code, I think.

because the second example has the logic wrapped in XML tags doesn't
mean there's any conceptional difference. You're still mixing logic and
output - which is unavoidable in some cases. It may be hidden away, and


I agree with you. There isn't much difference. Supposedly, web
developers, the types of folks who only know HTML and Dreamweaver, find
standard actions like the c:if stuff easier to deal with.
I'm all for separating logic and presentation, but _something_ has to be
responsible for conditionally creating output.
IIUC, taglibs will also create slightly more complex intermediate Java code.


I agree, as soon as the logic goes beyond simple |if|s.


Too many nested if's can be an issue as well.

I was thinking the whole table could be defined as a custom taglib
object, with a few properties, and then all the conditional logic could
be wrapped up where the "view" wouldn't know, or care. This seems
better, at least to me.
 
A

Arved Sandstrom

markspace said:
Just the fact that you aren't embedding actual Java code, I think.




I agree with you. There isn't much difference. Supposedly, web
developers, the types of folks who only know HTML and Dreamweaver, find
standard actions like the c:if stuff easier to deal with.
[ SNIP ]

That's supposedly the argument, that they are comfortable with tags and
so that's all we give them. Of course, this doesn't explain all the
webheads who are entirely cool with arcane and complex JavaScript. :)

AHS
 
M

markspace

Arved said:
That's supposedly the argument, that they are comfortable with tags and
so that's all we give them. Of course, this doesn't explain all the
webheads who are entirely cool with arcane and complex JavaScript. :)


If I had to take Sun's side (and I'm not sure that I should, but...) I'd
say that JavaScrpt is a browser side tech that works the same if you are
using PHP, Rail, ASP, JEE, or flat HTML files. Whereas Java only works
if you are using Java on the back-end. So it's a front-end vs. back-end
thing.

I do think that standard actions bind you to a specific template engine,
which isn't great for the front-end folks. If Sun could have released a
template engine that works with PHP, Python, etc. then having the
standard actions work the same with JEE might have been more valuable.
 
A

Arne Vajhøj

That's supposedly the argument, that they are comfortable with tags and
so that's all we give them. Of course, this doesn't explain all the
webheads who are entirely cool with arcane and complex JavaScript. :)

They are also paying for the decision in maintenance costs and
small bugs annoying the users.

Arne
 
A

Arne Vajhøj

If I had to take Sun's side (and I'm not sure that I should, but...) I'd
say that JavaScrpt is a browser side tech that works the same if you are
using PHP, Rail, ASP, JEE, or flat HTML files. Whereas Java only works
if you are using Java on the back-end. So it's a front-end vs. back-end
thing.

Server side or client side - it is still code that needs to work and
be maintained.

So it is a problem.

My guess is that it sneaked in. First a simple inline onClick, then
a small 10 line function, then huge JavaScript library. And that
the one responsible for application & technology governance found
out too late.

Arne
 
A

Arne Vajhøj

Stefan said:
I don't want to get shot, but... I've never quite understood why using
logic via taglibs is considered superior to logic via scriptlets. Just
because the second example has the logic wrapped in XML tags doesn't
mean there's any conceptional difference. You're still mixing logic and
output - which is unavoidable in some cases. It may be hidden away, and
I'm all for separating logic and presentation, but _something_ has to be
responsible for conditionally creating output.
IIUC, taglibs will also create slightly more complex intermediate Java code.

As I see it then taglibs offer several advantages:
- tags are more HTML like
- tag libs are restricted in what they can do while Java code
can do anything
- tag libs encourage reuse

Arne
 

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,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top