Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JSP Web-Development [newbie]

3 views
Skip to first unread message

pmz

unread,
Jul 28, 2010, 1:31:01 PM7/28/10
to
Dear Group,

1. At the beginning I'd like to apologize you for any mistakes or
misunderstood's, which might occur in message below, but I'm a quite
beginner in JSP so maybe some of my problems aren't really problems ;)
My Java knowledge is intermediate, but the structures of JSP or JSF or
any other frameworks is pretty bad, but I do not have any idea how to
start.

2. I'm trying to build bit complex website based on JSP (which I've
done a lot in PHP, so the main idea of website engineering is quite
common for me), but I a bit confused about the architecture/structure
of a JSP webpage building. The problem is, I'm not able to imagine in
my mind, how the architecture (directory structure) should be found,
how do I divide the template files, the engine core, etc.

3. What I've done in PHP, is quite simple structural solution, which
looked like following:

mainFile {
switch( __mainArgument from _GET ) {
case "myPlugin":
include "myPlugin.inc.php";
break;
/// And so on, and so on
}
}

The problem is, that the switch() java statement is not supporting
Strings - okay - so I'm not able to such as above. That does not
matter at all, because I thought that I'll just divide my website into
subfolders, such as:

Web Pages \
\index.jsp
\news\index.jsp
\users\index.jsp
\requests\index.jsp

That sollution is quite okay for me - any other suggestions? - but I
was wondering what about dynamic elements, which are visible in any
index.jsp (and \*\index.jsp) file? Let me show it on an example:

/// index.jsp
<html>
<body>
<div>
[DEPEND ON \%s\index.jsp CONTENT HERE] [1]
</div>
<div>
[DEFAULT CONTENT of \%s\index.jsp] [2]
</div>
</body>
</html>

The [2] position is quite simple, because it might be defined directly
in my file, but what if I include in [1] position a file, which is
also included in base index.jsp (ROOT)? There should be a submenu
display, which should display menu items regarding to the module
selected.

Shall I use the mod_rewrite (ie. /any_here/index.jsp > /index.jsp?
module=any_here - then the request parameter fetching is much easier.

I do not need pure explanations for my problems, because I'm able to
learn it by my self. Unfortunately, while I've been googling for some
tutorials "How create a JSP complex website?", I found some stupid
documents, which just describe how to build a "Hello World"
application without any complex elements (such as site modules, etc.).
Okay, I'm able to browse manuals delivered by Sun or anyone else, but
also, it's pure javadoc, with no solution proposals.

Thank you VERY much for help!

All the best,
Przemek M. Zawada

Chris Riesbeck

unread,
Jul 28, 2010, 2:37:14 PM7/28/10
to
On 7/28/2010 12:31 PM, pmz wrote:
> Dear Group,
>
> 1. At the beginning I'd like to apologize you for any mistakes or
> misunderstood's, which might occur in message below, but I'm a quite
> beginner in JSP so maybe some of my problems aren't really problems ;)
> My Java knowledge is intermediate, but the structures of JSP or JSF or
> any other frameworks is pretty bad, but I do not have any idea how to
> start.

Personally, I prefer JSTL over straight JSP, to reduce jumping back and
forth between HTML and Java syntax. So iteration over a collection is

<ul>
<c:forEach var="item" items="${myObj.thingies}">
<li><c:out value="${item}" /></li>
</c:forEach>
</ul>

instead of

<ul>
<% for (Object item : myObj.getThingies()) { %>
<li><%= item %></li>
<% } <%>
</ul>

With more nesting, those separated braces get annoying fast.

>
> 2. I'm trying to build bit complex website based on JSP (which I've
> done a lot in PHP, so the main idea of website engineering is quite
> common for me), but I a bit confused about the architecture/structure
> of a JSP webpage building. The problem is, I'm not able to imagine in
> my mind, how the architecture (directory structure) should be found,
> how do I divide the template files, the engine core, etc.

You can do almost anything, but don't put pages and other user-viewable
items under WEB-INF. WEB-INF is for libraries, class files, and XML
configuration files. So you could have

webapp/
pages/
jsp files
styles/
css files
images/
...
WEB-INF/
classes/
...
...

and other flatter or more nested structures as you choose.

>
> 3. What I've done in PHP, is quite simple structural solution, which
> looked like following:
>
> mainFile {
> switch( __mainArgument from _GET ) {
> case "myPlugin":
> include "myPlugin.inc.php";
> break;
> /// And so on, and so on
> }
> }
>
> The problem is, that the switch() java statement is not supporting
> Strings - okay - so I'm not able to such as above.

I don't know PHP so I don't know what mainArgument is, but if there's a
URL parameter thing for selecting what to include, e.g.,
/myApp?thing=myPlugin, then you could do something like that with this JSTL:

<c:choose>
<c:when test='${param.thing == "myPlugin"}'>
<jsp:include page="myPlugin.jsp" />
</c:when>
...
<c:otherwise>
<jsp:include page="myDefault.jsp" />
</c:otherwise>
</c:choose>

Whether that's a good way to do this is not for me to say.


> I
> was wondering what about dynamic elements, which are visible in any
> index.jsp (and \*\index.jsp) file? Let me show it on an example:
>
> /// index.jsp
> <html>
> <body>
> <div>
> [DEPEND ON \%s\index.jsp CONTENT HERE] [1]
> </div>
> <div>
> [DEFAULT CONTENT of \%s\index.jsp] [2]
> </div>
> </body>
> </html>
>
> The [2] position is quite simple, because it might be defined directly
> in my file, but what if I include in [1] position a file, which is
> also included in base index.jsp (ROOT)? There should be a submenu
> display, which should display menu items regarding to the module
> selected.

This may be a paradigm difference with PHP. In JSP, you don't do a lot
of including of other JSP files. That's mostly for common blocks of
HTML/JSP code, like a navigation bar.

Most of the dynamic content is created by the use of c:forEach and
c:choose and so on, along with Java object containers, like this:

<p>Welcome, <c:out value="${user.fullName}" />!</p>

where user is a session attribute set up by a backend Java application.
JSP/JSTL/JSF are used for front-end display -- the view and controller
in model-view-controller. The more complex model logic is done in Java.

Google for Java JSTL best practices and you'll get sites like

http://www.oracle.com/technetwork/articles/javase/servlets-jsp-140445.html

Javaworld, IBM and Sun/Oracle are reliable sources.


pmz

unread,
Jul 28, 2010, 2:54:48 PM7/28/10
to
On 28 Lip, 20:37, Chris Riesbeck <Chris.Riesb...@gmail.com> wrote:
> On 7/28/2010 12:31 PM, pmz wrote:
>
> > Dear Group,
>
> > 1. At the beginning I'd like to apologize you for any mistakes or
> > misunderstood's, which might occur in message below, but I'm a quite
> > beginner in JSP so maybe some of my problems aren't really problems ;)
> > My Java knowledge is intermediate, but the structures of JSP or JSF or
> > any other frameworks is pretty bad, but I do not have any idea how to
> > start.
>
> Personally, I prefer JSTL over straight JSP, to reduce jumping back and
> forth between HTML and Java syntax. So iteration over a collection is
>
>    <ul>
>      <c:forEach var="item" items="${myObj.thingies}">
>         <li><c:out value="${item}" /></li>
>      </c:forEach>
>    </ul>
>
> instead of
>
>    <ul>
>    <% for (Object item : myObj.getThingies()) { %>
>       <li><%= item %></li>
>    <% } <%>
>    </ul>
>
> With more nesting, those separated braces get annoying fast.

That's true and I think I'll try using those, too. My question is,
shall I make any changes in environment of NetBeans to use JSTL? File
extensions? Additional libraries? But okay, if it's obvious and I'll
find it by googling for it, ignore my questions.

The base idea is: Let's say I have few packages which contain database
access, data management and some others, which are pure Java objects,
without any HTML stuff. What I want to achieve, is to work over the
data I fetch or I submit in JSP pages.

I'd like to have ONE default JSP page (which obviously contains a
webpage main layout - styles, scripts, tables, images, etc.). In few
places I have some dynamical stuff (such as parameter-regarded menu
display or also parameter-regarded body display).

Shall I use you <c:choose/> method for further inclusion of required
jsp-sub-pages?

>
>
>
> > 2. I'm trying to build bit complex website based on JSP (which I've
> > done a lot in PHP, so the main idea of website engineering is quite
> > common for me), but I a bit confused about the architecture/structure
> > of a JSP webpage building. The problem is, I'm not able to imagine in
> > my mind, how the architecture (directory structure) should be found,
> > how do I divide the template files, the engine core, etc.
>
> You can do almost anything, but don't put pages and other user-viewable
> items under WEB-INF. WEB-INF is for libraries, class files, and XML
> configuration files. So you could have
>
>    webapp/
>      pages/
>        jsp files
>      styles/
>        css files
>      images/
>         ...
>      WEB-INF/
>        classes/
>          ...
>        ...
>
> and other flatter or more nested structures as you choose.

As you said, my directory tree looks like the following:
\Project Name
\Web Pages
\META-INF
\WEB-INF
\jNLC
\users\add.jsp
\users\edit.jsp
\styles\(*.css)|(*.js)
\index.jsp
\Source Packages
\Libraries
\Configuration Files

That's the way NetBeans shows it to me. I suppose that's okay?

>
>
>
> > 3. What I've done in PHP, is quite simple structural solution, which
> > looked like following:
>
> > mainFile {
> >    switch( __mainArgument from _GET ) {
> >      case "myPlugin":
> >         include "myPlugin.inc.php";
> >      break;
> >      /// And so on, and so on
> >    }
> > }
>
> > The problem is, that the switch() java statement is not supporting
> > Strings - okay - so I'm not able to such as above.
>
> I don't know PHP so I don't know what mainArgument is, but if there's a
> URL parameter thing for selecting what to include, e.g.,
> /myApp?thing=myPlugin, then you could do something like that with this JSTL:
>
>    <c:choose>
>      <c:when test='${param.thing == "myPlugin"}'>
>        <jsp:include page="myPlugin.jsp" />
>      </c:when>
>      ...
>      <c:otherwise>
>        <jsp:include page="myDefault.jsp" />
>      </c:otherwise>
>    </c:choose>
>
> Whether that's a good way to do this is not for me to say.

Yes! That the way I'd like to work! (In fact, the mainArgument is
requestParameter, string valued)

> http://www.oracle.com/technetwork/articles/javase/servlets-jsp-140445...


>
> Javaworld, IBM and Sun/Oracle are reliable sources.

Okay! I'll do it. Now I know what should I look for directly. These
are base problems, I know, but I thank you for enlightening me the
bases.

pmz

unread,
Jul 28, 2010, 3:02:03 PM7/28/10
to
Few more knowledge leaks:

1. Let's say I have a sample jsp file, ie:

<anyJspTag>
<set parameter a="type1" />
<jsp:include page="mypage.jsp" />
</anyJspTag>

Question: Is it possible to access the "a" parameter (with defined or
dynamic) value in the mypage.jsp (which for example contains the
<c:choose /> related to "a" parameter ?) If yes, then I'd be able to
choose some sectors of mypage.jsp which should be visible regarding to
request parameters - true?

2. I'd like to build a menu with submenus from standalone XML file -
how shall I start? How ppl do it?

I need to change my php-thinking into jsp-thinking - that's quite
difficult, but I'm trying.

Thank you :)

Przemek M. Zawada

markspace

unread,
Jul 28, 2010, 4:00:55 PM7/28/10
to
The following contains a lot of opinions, as opposed to facts. Caveat
emptor.

pmz wrote:
> mainFile {
> switch( __mainArgument from _GET ) {
> case "myPlugin":

I'd regard this as somewhat suspect even in PHP. It requires URLs like
"http://example.com/?__mainArgument=myPlugin" and that looks kinda ugly
to the user. I'd rather supply the user with something like
"http://example.com/myPlugin" and use mod-rewrite to redirect that if
needed, thus eliminating any need to code it at all.

You can't use mod-rewrite in Java, because War files aren't visible to
Apache. All the Java files are contained within one archive which isn't
something Apache can read. So Java provides it's own way to do this,
again with out writing code, like what mod_rewrite provides.

In the WEB-INF/web.xml file, you'd bind a pattern to a name, like this:

<servlet-mapping>
<servlet-name>MainPlugIn</servlet-name>
<url-pattern>/myPlugin</url-pattern>
</servlet-mapping>

That replaces mod_rewrite, and maps the url pattern after your hostname
to the name MainPlugIn. Now you have to do one more thing to map the
name MainPlugIn to some code.

<servlet>
<servlet-name>MainPlugIn</servlet-name>
<servlet-class>myplugin/plugin.jsp</servlet-class>
</servlet>

You may have to play with the servlet-class a bit, I'm not 100% sure
that it works exactly like that.

This is a very powerful feature, and you should use it as much as
possible. In particular, it allows you to change any url so that it
calls any code in your app, and you don't have to search and modify your
code to do it. Just change a config file and it works.

The web.xml file itself can be a pain, but if you get a good IDE like
NetBeans or Eclipse, they'll do most of the work for you. That reminds
me: if you're looking for good tutorials, you could try the how-to's
for the IDEs. NetBeans has several that will show you how to build a
moderately complex web app.

<http://netbeans.org/kb/trails/java-ee.html>

You should probably avoid the EJB stuff (that's really complex) and JSF
- Java Server Faces - and the other stuff under Web Frameworks can be
skipped at first. Don't want to over load you with too much. Also
check out the Other Tutorials section and the Web Blogs might be a
useful source on info for you.


>
> Web Pages \
> \index.jsp
> \news\index.jsp
> \users\index.jsp
> \requests\index.jsp
>
> That sollution is quite okay for me - any other suggestions? - but I


Also, use the fact that WEB-INF is private to hide JSPs and servlets
that you don't want the user calling directly.

Web Pages \
index.jsp
other-public.jsp
WEB-INF \
private.jsp
myplugin \
plugin.jsp
etc.
css \
public.css
images \
public.jpg

Etc.

> was wondering what about dynamic elements, which are visible in any
> index.jsp (and \*\index.jsp) file? Let me show it on an example:
>
> /// index.jsp
> <html>
> <body>
> <div>
> [DEPEND ON \%s\index.jsp CONTENT HERE] [1]


Due to the servlet-mapping above, naming everything "index.jsp" is not
needed in a Java app. This would actually go to a servlet or JSP named
\%s, not \%s\index.jsp.

Anything that's extra on the path gets added as an attribute. <%
request.getPathInfo(); %> will return the extra path string. There's
probably an EL variable that returns the extra path string as well, but
I don't recall it offhand.

Anyway, use the request path and context path to figure out stuff that
depends on the path, but mostly you want to direct your URLs to a
servlet that already knows what to do with out a lot of "figuring out."

> Shall I use the mod_rewrite (ie. /any_here/index.jsp > /index.jsp?
> module=any_here - then the request parameter fetching is much easier.

Again, I don't think mod-rewrite is going to help you with JSPs and
servlets.

Also, try reading the servlet specifications, it's are surprisingly
readable.

<http://jcp.org/aboutJava/communityprocess/final/jsr154/index.html>

Java EE 6 Tutorial.

<http://download-llnw.oracle.com/javaee/6/tutorial/doc/>

Chris Riesbeck

unread,
Jul 28, 2010, 6:24:47 PM7/28/10
to
On 7/28/2010 2:02 PM, pmz wrote:
> Few more knowledge leaks:
>
> 1. Let's say I have a sample jsp file, ie:
>
> <anyJspTag>
> <set parameter a="type1" />
> <jsp:include page="mypage.jsp" />
> </anyJspTag>
>
> Question: Is it possible to access the "a" parameter (with defined or
> dynamic) value in the mypage.jsp (which for example contains the
> <c:choose /> related to "a" parameter ?) If yes, then I'd be able to
> choose some sectors of mypage.jsp which should be visible regarding to
> request parameters - true?

Either of the following works to pass info to an included page:

<c:set var="someVar" value="..." scope="request" />
<jsp:include page=... />

- inside the included page, use ${someVar} to access the value
- the scope is needed; by default, variables are page scope, i.e.,
not shared across pages

or

<jsp:include page=...>
<jsp:param name="someVar" value="..." />
</jsp:include>

- inside the included page, use ${param.someVar} to access the value

>
> 2. I'd like to build a menu with submenus from standalone XML file -
> how shall I start? How ppl do it?

JSTL has XML processing forms but I've not used them. A JSTL quick
reference I quite like (http://www.cs.uiowa.edu/~slonnegr/wpj/jqr.pdf)
gives this example:

<c:import var="rss"

url="http://servlet.java.sun.com/syndication/rss_java_highlights-PARTNER-20.xml"/>
<x:parse var="news" xml="${rss}"/>

which puts parsed XML data into "news".


>
> I need to change my php-thinking into jsp-thinking - that's quite
> difficult, but I'm trying.

Some of it is going to model-view-controller which is a more general
concept than Java. Even though JSTL has some nice SQL tags, I don't use
them because it puts too much business logic in the presentation layer.

Arne Vajhøj

unread,
Jul 28, 2010, 8:12:34 PM7/28/10
to
On 28-07-2010 13:31, pmz wrote:
> 2. I'm trying to build bit complex website based on JSP (which I've
> done a lot in PHP, so the main idea of website engineering is quite
> common for me), but I a bit confused about the architecture/structure
> of a JSP webpage building. The problem is, I'm not able to imagine in
> my mind, how the architecture (directory structure) should be found,
> how do I divide the template files, the engine core, etc.
>
> 3. What I've done in PHP, is quite simple structural solution, which
> looked like following:
>
> mainFile {
> switch( __mainArgument from _GET ) {
> case "myPlugin":
> include "myPlugin.inc.php";
> break;
> /// And so on, and so on
> }
> }
>
> The problem is, that the switch() java statement is not supporting
> Strings - okay - so I'm not able to such as above.

You could use if statements.

But you really should use a servlet instead of a JSP page
to do that.

Which could either be your own servlet or one of the many
MVC web frameworks.

> That does not
> matter at all, because I thought that I'll just divide my website into
> subfolders, such as:
>
> Web Pages \
> \index.jsp
> \news\index.jsp
> \users\index.jsp
> \requests\index.jsp
>
> That sollution is quite okay for me - any other suggestions?

I do not see the correlation with the previous section,
but it seems OK.

> - but I
> was wondering what about dynamic elements, which are visible in any
> index.jsp (and \*\index.jsp) file? Let me show it on an example:
>
> /// index.jsp
> <html>
> <body>
> <div>
> [DEPEND ON \%s\index.jsp CONTENT HERE] [1]
> </div>
> <div>
> [DEFAULT CONTENT of \%s\index.jsp] [2]
> </div>
> </body>
> </html>
>
> The [2] position is quite simple, because it might be defined directly
> in my file, but what if I include in [1] position a file, which is
> also included in base index.jsp (ROOT)? There should be a submenu
> display, which should display menu items regarding to the module
> selected.

If you want to do that then I think you should look at something
like Tiles.


> Shall I use the mod_rewrite (ie. /any_here/index.jsp> /index.jsp?
> module=any_here - then the request parameter fetching is much easier.

I do not see any need for mod_rewrite.

Arne

Wojtek

unread,
Jul 28, 2010, 8:11:57 PM7/28/10
to
pmz wrote :

You need to do a paradigm shift.

In PHP you include files selectively mostly for performance reasons, as
you do not want the interpreter parsing logic which will not be used
during that page hit.

Using Java, the JSP is a servlet which is compiled once into a class,
then used after that. In most cases the JSP servlet class remains in
server memory so load times are very fast.

Because the JSP is compiled once as a whole, it is impossible to
selectively include files for each page hit.

If the included files have differing processing logic, then you really
should move that logic into the application as classes and servlets. A
JSP should only contain enough logic to support display requirements
not processing requirements.

--
Wojtek :-)


Arne Vajhøj

unread,
Jul 28, 2010, 8:16:57 PM7/28/10
to

Maybe your servlet container comes with JSTL support.

Otherwise you can download an implementation like:
http://tomcat.apache.org/taglibs/standard/
and put the two jar files in WEB-INF/lib.

Then you need to put taglib definitions in top of your JSP pages.

Like:

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

> The base idea is: Let's say I have few packages which contain database
> access, data management and some others, which are pure Java objects,
> without any HTML stuff. What I want to achieve, is to work over the
> data I fetch or I submit in JSP pages.
>
> I'd like to have ONE default JSP page (which obviously contains a
> webpage main layout - styles, scripts, tables, images, etc.). In few
> places I have some dynamical stuff (such as parameter-regarded menu
> display or also parameter-regarded body display).
>
> Shall I use you<c:choose/> method for further inclusion of required
> jsp-sub-pages?

You should use a servlet for that.

JSP page = View
Servlet = Control

Arne

Arne Vajhøj

unread,
Jul 28, 2010, 8:18:41 PM7/28/10
to
On 28-07-2010 15:02, pmz wrote:
> Few more knowledge leaks:
>
> 1. Let's say I have a sample jsp file, ie:
>
> <anyJspTag>
> <set parameter a="type1" />
> <jsp:include page="mypage.jsp" />
> </anyJspTag>
>
> Question: Is it possible to access the "a" parameter (with defined or
> dynamic) value in the mypage.jsp (which for example contains the
> <c:choose /> related to "a" parameter ?) If yes, then I'd be able to
> choose some sectors of mypage.jsp which should be visible regarding to
> request parameters - true?

The syntax is a bit different.

http://java.sun.com/products/jsp/tags/11/syntaxref1112.html

Arne

Arne Vajhøj

unread,
Jul 28, 2010, 8:21:28 PM7/28/10
to
On 28-07-2010 16:00, markspace wrote:
> The following contains a lot of opinions, as opposed to facts. Caveat
> emptor.
>
> pmz wrote:
>> mainFile {
>> switch( __mainArgument from _GET ) {
>> case "myPlugin":
>
> I'd regard this as somewhat suspect even in PHP. It requires URLs like
> "http://example.com/?__mainArgument=myPlugin" and that looks kinda ugly
> to the user. I'd rather supply the user with something like
> "http://example.com/myPlugin" and use mod-rewrite to redirect that if
> needed, thus eliminating any need to code it at all.
>
> You can't use mod-rewrite in Java, because War files aren't visible to
> Apache. All the Java files are contained within one archive which isn't
> something Apache can read. So Java provides it's own way to do this,
> again with out writing code, like what mod_rewrite provides.

Why would:
Apache HTTPD with mod_rewrite -> Apache Tomcat
not work?

> In the WEB-INF/web.xml file, you'd bind a pattern to a name, like this:
>
> <servlet-mapping>
> <servlet-name>MainPlugIn</servlet-name>
> <url-pattern>/myPlugin</url-pattern>
> </servlet-mapping>
>
> That replaces mod_rewrite, and maps the url pattern after your hostname
> to the name MainPlugIn. Now you have to do one more thing to map the
> name MainPlugIn to some code.
>
> <servlet>
> <servlet-name>MainPlugIn</servlet-name>
> <servlet-class>myplugin/plugin.jsp</servlet-class>
> </servlet>
>
> You may have to play with the servlet-class a bit, I'm not 100% sure
> that it works exactly like that.
>
> This is a very powerful feature, and you should use it as much as
> possible. In particular, it allows you to change any url so that it
> calls any code in your app, and you don't have to search and modify your
> code to do it. Just change a config file and it works.

I don't think that provides nearly as much functionality as mod_rewrite
when it comes to smart rewrites.

Arne

Arne Vajhøj

unread,
Jul 28, 2010, 8:25:00 PM7/28/10
to
On 28-07-2010 14:37, Chris Riesbeck wrote:
> On 7/28/2010 12:31 PM, pmz wrote:
>> Dear Group,
>>
>> 1. At the beginning I'd like to apologize you for any mistakes or
>> misunderstood's, which might occur in message below, but I'm a quite
>> beginner in JSP so maybe some of my problems aren't really problems ;)
>> My Java knowledge is intermediate, but the structures of JSP or JSF or
>> any other frameworks is pretty bad, but I do not have any idea how to
>> start.
>
> Personally, I prefer JSTL over straight JSP, to reduce jumping back and
> forth between HTML and Java syntax. So iteration over a collection is
>
> <ul>
> <c:forEach var="item" items="${myObj.thingies}">
> <li><c:out value="${item}" /></li>
> </c:forEach>
> </ul>
>
> instead of
>
> <ul>
> <% for (Object item : myObj.getThingies()) { %>
> <li><%= item %></li>
> <% } <%>
> </ul>

Very good advice.

With newer versions:

<li><c:out value="${item}" /></li>

can even be written as:

<li>${item}</li>

>> 2. I'm trying to build bit complex website based on JSP (which I've
>> done a lot in PHP, so the main idea of website engineering is quite
>> common for me), but I a bit confused about the architecture/structure
>> of a JSP webpage building. The problem is, I'm not able to imagine in
>> my mind, how the architecture (directory structure) should be found,
>> how do I divide the template files, the engine core, etc.
>
> You can do almost anything, but don't put pages and other user-viewable
> items under WEB-INF. WEB-INF is for libraries, class files, and XML
> configuration files. So you could have
>
> webapp/
> pages/
> jsp files
> styles/
> css files
> images/
> ...
> WEB-INF/
> classes/
> ...
> ...
>
> and other flatter or more nested structures as you choose.

JSP pages in WEB-INF are not directly accessible, but they can
be used from servlet / action classes.

Some even consider that good practice.

I don't agree, but ...

Arne

Lew

unread,
Jul 28, 2010, 8:36:32 PM7/28/10
to
pmz wrote:
>>> Dear Group,

"Group" isn't exactly the right term here. It's a Usenet forum.

Chris Riesbeck wrote:
>> Personally, I prefer JSTL over straight JSP, to reduce jumping back and

Excellent advice.

>> forth between HTML and Java syntax. So iteration over a collection is
>>
>> <ul>
>> <c:forEach var="item" items="${myObj.thingies}">
>> <li><c:out value="${item}" /></li>
>> </c:forEach>
>> </ul>
>>
>> instead of
>>
>> <ul>
>> <% for (Object item : myObj.getThingies()) { %>
>> <li><%= item %></li>
>> <% }<%>
>> </ul>
>>
>> With more nesting, those separated braces get annoying fast.

Besides, you aren't supposed to put Java code ("scriptlet") directly in JSPs,
as a matter of practice.

> That's true and I think I'll try using those, too. My question is,
> shall I make any changes in environment of NetBeans to use JSTL? File
> extensions? Additional libraries? But okay, if it's obvious and I'll
> find it by googling for it, ignore my questions.

NetBeans Web project "Properties" has an entry for "Libraries", and a button
to "Add Library", and one built-in choice there is JSTL. NB then
automagically adds the jstl.jar JAR to your application.

> The base idea is: Let's say I have few packages which contain database
> access, data management and some others, which are pure Java objects,
> without any HTML stuff. What I want to achieve, is to work over the
> data I fetch or I submit in JSP pages.

Read about "Expression Language" (EL) in the Java EE tutorial and other sources.
<http://download.oracle.com/javaee/6/tutorial/doc/gjddd.html>
There's a decent chapter in the Java EE 5 tutorial also,
<http://download.oracle.com/javaee/5/tutorial/doc/bnahq.html>
plus its "Web Tier" chapter covers servlets and JSP.
<http://download.oracle.com/javaee/5/tutorial/doc/bnadp.html>

> I'd like to have ONE default JSP page (which obviously contains a
> webpage main layout - styles, scripts, tables, images, etc.). In few
> places I have some dynamical stuff (such as parameter-regarded menu
> display or also parameter-regarded body display).
>
> Shall I use you<c:choose/> method for further inclusion of required
> jsp-sub-pages?

It's not his, or mine, but JSTL's. That's one way but not the only way,
perhaps not even the best way.

There's a <jsp:include>
<http://download.oracle.com/javaee/5/tutorial/doc/bnajb.html>
and a <jsp:forward> tag.
<http://download.oracle.com/javaee/5/tutorial/doc/bnajc.html>

>>> 2. I'm trying to build bit complex website based on JSP (which I've
>>> done a lot in PHP, so the main idea of website engineering is quite
>>> common for me), but I a bit confused about the architecture/structure
>>> of a JSP webpage building. The problem is, I'm not able to imagine in
>>> my mind, how the architecture (directory structure) should be found,
>>> how do I divide the template files, the engine core, etc.

JSP isn't exactly a "template" framework as you might think of it, but close
enough for now.

Usually you have backing Java logic implemented as straight-up Java objects
accessed from page/session/application context using <jsp:useBean> and,
primarily, EL. The backing logic handles the "model" aspect - business logic,
interaction with back-end data stores and such, feeding the "view" aspect that
the JSPs embody.

Chris Riesbeck wrote:
>> You can do almost anything, but don't put pages and other user-viewable
>> items under WEB-INF. WEB-INF is for libraries, class files, and XML
>> configuration files.

And JSP fragments (.jspf files) and other resources that the user should not
access directly but only under control (e.g., <jsp:include>) of your logic.

pmz wrote:
>>> I was wondering what about dynamic elements, which are visible in any
>>> index.jsp (and \*\index.jsp) file? Let me show it on an example:
>>
>>> /// index.jsp
>>> <html>
>>> <body>
>>> <div>
>>> [DEPEND ON \%s\index.jsp CONTENT HERE] [1]
>>> </div>
>>> <div>
>>> [DEFAULT CONTENT of \%s\index.jsp] [2]
>>> </div>
>>> </body>
>>> </html>
>>
>>> The [2] position is quite simple, because it might be defined directly
>>> in my file, but what if I include in [1] position a file, which is
>>> also included in base index.jsp (ROOT)? There should be a submenu
>>> display, which should display menu items regarding to the module
>>> selected.

Chris Riesbeck wrote:
>> This may be a paradigm difference with PHP. In JSP, you don't do a lot
>> of including of other JSP files. That's mostly for common blocks of
>> HTML/JSP code, like a navigation bar.

We-e-e-ll, that all depends.

>> Most of the dynamic content is created by the use of c:forEach and
>> c:choose and so on, along with Java object containers, like this:
>>
>> <p>Welcome,<c:out value="${user.fullName}" />!</p>
>>
>> where user is a session attribute set up by a backend Java application.
>> JSP/JSTL/JSF are used for front-end display -- the view and controller
>> in model-view-controller.

a.k.a. "MVC". Wikipedia is a decent source for an introduction to this concept.

>> The more complex model logic is done in Java.
>> Google for Java JSTL best practices and you'll get sites like
>>

>> <http://www.oracle.com/technetwork/articles/javase/servlets-jsp-140445.html>


>>
>> Javaworld, IBM and Sun/Oracle are reliable sources.

IBM Developerworks is very worthy, too.

Google for Java's "Model 2" architecture for a simplified MVC architecture.
It's mentioned in the link Chris Riesbeck provided. Also here:
<http://en.wikipedia.org/wiki/Model_2>
<http://java.sun.com/blueprints/guidelines/designing_enterprise_applications_2e/web-tier/web-tier5.html>

--
Lew

markspace

unread,
Jul 28, 2010, 8:43:42 PM7/28/10
to
Arne Vajhøj wrote:

> Why would:
> Apache HTTPD with mod_rewrite -> Apache Tomcat
> not work?
>

Did you try it? Did it work? I'm honestly interested to know, it never
occurred to me that it might work.


>
> I don't think that provides nearly as much functionality as mod_rewrite
> when it comes to smart rewrites.

What if Apache isn't your web server?

Lew

unread,
Jul 28, 2010, 8:47:42 PM7/28/10
to
Arne Vajhøj wrote:
>> Why would:
>> Apache HTTPD with mod_rewrite -> Apache Tomcat
>> not work?
...

markspace wrote:
> What if Apache isn't your web server?

In general a good question. In the OP's particular case it's pretty clear
they're in control of the environment and can choose it to be.

--
Lew

markspace

unread,
Jul 28, 2010, 8:52:12 PM7/28/10
to
Lew wrote:
> Arne Vajhøj wrote:
>>> Why would:
>>> Apache HTTPD with mod_rewrite -> Apache Tomcat
>>> not work?
> ....

>
> markspace wrote:
>> What if Apache isn't your web server?
>
> In general a good question. In the OP's particular case it's pretty
> clear they're in control of the environment and can choose it to be.
>


Seems like a good way to get bit if you ever export the web app to a
different environment. Sure, Apache is very popular and you can just
spec the app that way to customers, but it's also just one more thing to
deal with manually (the mod_rewrite stuff) that doesn't need to be, I
suppose.

Arne Vajhøj

unread,
Jul 28, 2010, 9:10:12 PM7/28/10
to
On 28-07-2010 20:43, markspace wrote:
> Arne Vajhøj wrote:
>> Why would:
>> Apache HTTPD with mod_rewrite -> Apache Tomcat
>> not work?
>
> Did you try it? Did it work? I'm honestly interested to know, it never
> occurred to me that it might work.

I have not tried it, but I would expect it to work.

If used with PHP it is:

---(original request)-->mod_rewrite--(modified
request)-->mod_php---->PHP scipt

With Tomcat it should be:

---(original request)-->mod_rewrite--(modified request)-->mod_jk--(AJP
protocol)-->Tomcat

Given the functionality of mod_rewrite it must be executed before
the real mod's and be rather independent of what those mod's do.

At least that is my expectation.

>> I don't think that provides nearly as much functionality as mod_rewrite
>> when it comes to smart rewrites.
>
> What if Apache isn't your web server?

The using an Apache module like mod_rewrite is obviously not an option.

Arne

Lew

unread,
Jul 29, 2010, 12:51:48 AM7/29/10
to
markspace wrote:
>>> What if Apache isn't your web server?

Lew wrote:
>> In general a good question. In the OP's particular case it's pretty
>> clear they're in control of the environment and can choose it to be.

markspace wrote:
> Seems like a good way to get bit if you ever export the web app to a
> different environment. Sure, Apache is very popular and you can just
> spec the app that way to customers, but it's also just one more thing to
> deal with manually (the mod_rewrite stuff) that doesn't need to be, I
> suppose.

It is not uncommon to spec requirements for an app to run. And some kinds of
portability are overrated. I'm not necessarily speaking in favor of
mod_rewrite, but I do think httpd is an awesome product and one should not be
ashamed or fearful of basing an application on it.

And there are hundreds of products on the market that already spec it as part
of their product. Ever hear of LAMP?

--
Lew

Tom Anderson

unread,
Jul 29, 2010, 9:27:26 AM7/29/10
to
On Wed, 28 Jul 2010, markspace wrote:

> Also, use the fact that WEB-INF is private to hide JSPs and servlets
> that you don't want the user calling directly.
>
> Web Pages \
> index.jsp
> other-public.jsp
> WEB-INF \
> private.jsp
> myplugin \
> plugin.jsp
> etc.
> css \
> public.css
> images \
> public.jpg
>
> Etc.

Nice trick! I didn't know that one. You could hide config files and so on
like this too.

tom

--
The art of medicine consists in amusing the patient while nature cures
the disease. -- Voltaire

Tom Anderson

unread,
Jul 29, 2010, 9:57:06 AM7/29/10
to
On Wed, 28 Jul 2010, pmz wrote:

> I'd like to have ONE default JSP page (which obviously contains a
> webpage main layout - styles, scripts, tables, images, etc.). In few
> places I have some dynamical stuff (such as parameter-regarded menu
> display or also parameter-regarded body display).
>
> Shall I use you <c:choose/> method for further inclusion of required
> jsp-sub-pages?

No. You're doing this all wrong. Or rather, you're doing it all PHP, which
in JSP means wrong.

Use separate pages for the different pages, and a tag file to define the
common structure:

http://www.oracle.com/technology/sample_code/tutorials/jsp20/tagfiles.html

A tag file is the analog of a framework method in code; it can write a
load of HTML, but can also invoke fragments passed by the original
invoker.

For example, given a tag file like this (call it main.tag):

<%@ attribute name="content" required="true" fragment="true" %>
<html>
<body>
<h1>PMZ's SUPER SITE</h1>
<jsp:invoke fragment="content"/>
</body>
</html>

You could write a JSP like this:

<%@ taglib prefix="mytags" tagdir="/WEB-INF/tags" %>
<mytags:main>
<jsp:attribute name="content">
<p>Some content.</p>
</jsp:attribute>
</mytags:main>

This would invoke the main.tag to build the page structure, which would
then call down to the fragment defined in the JSP. You can have multiple
fragments, and also use normal content of the tag via <jsp:doBody/>.

Chris Riesbeck

unread,
Jul 29, 2010, 10:32:08 AM7/29/10
to
On 7/28/2010 7:25 PM, Arne Vajhøj wrote:
>
> With newer versions:
>
> <li><c:out value="${item}" /></li>
>
> can even be written as:
>
> <li>${item}</li>

c:out will convert HTML special characters to entity code. ${item} by
itself does not. I've been bit by that several times when ${item} was a
string with < in it.

Chris Riesbeck

unread,
Jul 29, 2010, 10:39:24 AM7/29/10
to
On 7/28/2010 7:36 PM, Lew wrote:
>
> JSP isn't exactly a "template" framework as you might think of it, but
> close enough for now.

And once you understand JSP, it's not hard to move on to the zillion
template frameworks built on top of it.


> Chris Riesbeck wrote:
>>>
>>> Javaworld, IBM and Sun/Oracle are reliable sources.
>
> IBM Developerworks is very worthy, too.

Yes, that's the part of IBM I meant. A lot of neat technology and some
very well-written articles have come out of there.

pmz

unread,
Jul 29, 2010, 10:52:12 AM7/29/10
to
Dear Friends (ain't Group ;),

I'm really surprised about your huge response for my problems. That's
really kind. I'll analyze suggestions you've mentioned above and I'll
try to fetch the best solution for my application.

I've discovered that such enthusiasm was not found by me in the C/C++
(or plenty more devlangs) sections of google groups, that's weird,
isn't it?
Now I'm pretty sure that I may share any of my problems here and they
won't stay without an answer. While I'm exploring the Java world,
which in fact gives me a lot of fun, that's a big bonus. There is no
hurry in my project, so I'll try to do my best and use your advices to
build the perfect (mostly) one.

Once again, I'd like to thank all of you for help - if any problems
occur, I won't hesitate writing'em here.

pmz

unread,
Jul 29, 2010, 11:18:21 AM7/29/10
to
Dear Friends,

As I mentioned before, I have a simple modular structure of my
website. Let me describe it as:

- Main page, which contains a menu bar with few page-links, ie.
- News
- Users
- Requests
- Options
- Anything more

Each page-link has sub-links, as following: “New”, “Edit”,
“Browse” (typical).
(In PHP, I’d href them like: “/news/add” or “/users/browse”)

What I’ve been doing lately in PHP, I’ve used a mod_rewrite powerful
tool, which rewrites me request string like so:

if(!index.php)
rewrite /any/friendly/url to /index.php?p=any/friendly/url

Inside the index.php I’ve parsed the “p” argument into ARGV/ARGC-style
based Array – ARGV[0] -> any, ARGV[1] -> friendly, ARGV[2] -> url,
etc. Cool.

When it was done, I’ve build big switch statement, which direct the
HTML-output regarding to ARGV[0-n] parameters, as following:

$PageObject = HTML_TEMPLATE(“index.tmpl”);

switch(ARGV[0]) {
case “news”:
switch(ARGV[1]) {
case “add”:
// Do something
$PageContentElement = HTML_TEMPLATE(“subpage.tmpl);
$PageContentElement->Add(“add some data here”);
$PageObject->Add(“PageSection_Menu”,
$PageContentElement->GetObjectHTML());
break;
case “edit”:
// Do something else
break;
}
break;
case “users”:
// analogical as in “news” case
break;
// other cases
}

$PageObject->DisplayHtml();
/*
print out <html>*</html> main template including the replaced
variable elements
*/

The PageObject is a simple template engine, which I’ve wrote to parse
pure-HTML files, which contain {Variable} elements, which might be
changed by RegExp with defined value (depending on switch statement
and ARGV variables).

At the end, I just printout the PageObject data, with changed content
– done.
That was my idea of PHP webpage engine and as I see that logic is
quite impossible in JSP. Here the problems occur.

I wish you understand how my php-logic seems to work, now tell me
please, how do I rewrite it into JSP style?

As I understood, I may create a index.jsp file, which contains the
default page template (including menus, footers, all that crap), with
links to my modules – ie. /news or /users.
I’ve been told, that I may connect the /news url with some java
servlets (via web.xml). Sounds cool, but is it possible to connect the
main index.jsp file with servlet output? On the other hand, is it
possible to get from index.jsp a page template layout and copy/move it
to my servlet element?

I don’t have any problems with dividing my pages into plenty jsp
pages, but what I want to achieve is NOT make changes in each jsp page
in the static sectors (like menubars, styles, etc.) When there are few
files, n/p, but when I build like 20 sections, that would be annoying.

Oghr, don’t be mad on me, I’m trying to understand everything. Now
when I’ve clarify my application structure, I presume it might be
easier for you to understand what I really want :)

Thank you,
Przemek M. Zawada

Lew

unread,
Jul 29, 2010, 11:51:57 AM7/29/10
to
pmz wrote:
> I've discovered that such enthusiasm was not found by me in the C/C++
> (or plenty more devlangs) sections of google groups, that's weird,
> isn't it?
>

This isn't a "section of Google Groups" (despite that I'm using GG to
post this response right now). Google Groups merely echoes the Usenet
forum, and really isn't even the best way to access the forum. GG
echoes tons of spam and other garbage that other newsgroup servers
filter, and the browser has no capability to filter the spam that does
get through. Google for "newsgroup reader", or use Mozilla
Thunderbird as a minimally acceptable client. You will need to
connect to a newsgroup server - like a mail server but specifically
for news feeds - to get the good experience.

> Now I'm pretty sure that I may share any of my problems here and they
> won't stay without an answer.
>

Of course you will have to deal with posters like me who reply
enthusiastically but not always kindly, humbly or diplomatically.
Bear in mind that sarcasm or rhetorical tricks don't necessarily
indicate unhelpful responses; sometimes one is cruel to be kind. To
pick one of the most popular examples, the initialism "GIYF", or
"Google Is Your Friend", is dismissive in tone but embodies the
extremely empowering advice to use the search tools available to find
answers rather than ask stupid questions. Build a man a fire and you
warm him for an hour. Set a man on fire and you warm him for the rest
of his life.

Kidding aside, most of us most of the time mean to help whether or not
we add attitude as a bonus feature. Also, Usenet is a discussion
medium, not a help desk. You usually can get good advice, but much of
the joy comes from the spirited discussion and diversity of opinions
people express.

--
Lew

Tim Slattery

unread,
Jul 29, 2010, 12:17:55 PM7/29/10
to
Chris Riesbeck <Chris.R...@gmail.com> wrote:

>On 7/28/2010 7:25 PM, Arne Vajhøj wrote:
>>
>> With newer versions:
>>
>> <li><c:out value="${item}" /></li>
>>
>> can even be written as:
>>
>> <li>${item}</li>
>
>c:out will convert HTML special characters to entity code.

It will if you use escapeXML:

<c:out value="${item}" escapeXml="true" />

>${item} by itself does not.

No, but using the JSTL escapeXml function does:

${fn:escapeXml(item)}

--
Tim Slattery
Slatt...@bls.gov
http://members.cox.net/slatteryt

pmz

unread,
Jul 29, 2010, 12:23:36 PM7/29/10
to
On 29 Lip, 17:51, Lew <l...@lewscanon.com> wrote:
> pmz wrote:
> > I've discovered that such enthusiasm was not found by me in the C/C++
> > (or plenty more devlangs) sections of google groups, that's weird,
> > isn't it?
>
> This isn't a "section of Google Groups" (despite that I'm using GG to
> post this response right now).  Google Groups merely echoes the Usenet
> forum, and really isn't even the best way to access the forum.  GG
> echoes tons of spam and other garbage that other newsgroup servers
> filter, and the browser has no capability to filter the spam that does
> get through.  Google for "newsgroup reader", or use Mozilla
> Thunderbird as a minimally acceptable client.  You will need to
> connect to a newsgroup server - like a mail server but specifically
> for news feeds - to get the good experience.

Don't be so precise, but I'll think about it - thank you. Anyway, the
Google gives me ability to "read and share wherever I am" the usenet.

>
> > Now I'm pretty sure that I may share any of my problems here and they
> > won't stay without an answer.
>
> Of course you will have to deal with posters like me who reply
> enthusiastically but not always kindly, humbly or diplomatically.
> Bear in mind that sarcasm or rhetorical tricks don't necessarily
> indicate unhelpful responses; sometimes one is cruel to be kind.  To
> pick one of the most popular examples, the initialism "GIYF", or
> "Google Is Your Friend", is dismissive in tone but embodies the
> extremely empowering advice to use the search tools available to find
> answers rather than ask stupid questions.  Build a man a fire and you
> warm him for an hour.  Set a man on fire and you warm him for the rest
> of his life.
>
> Kidding aside, most of us most of the time mean to help whether or not
> we add attitude as a bonus feature.  Also, Usenet is a discussion
> medium, not a help desk.  You usually can get good advice, but much of
> the joy comes from the spirited discussion and diversity of opinions
> people express.
>
> --
> Lew

True, true and true. I'm not afraid of Google power so at first I look
for answers there. Unfortunately, some things are not found, because
they are not just syntax or function questions - the ideas and
techniques rarely are posted. Nevertheless, I did find some
interesting topics, Q&A, FAQ or tutorials, which I read carefully and
tried to follow them. As you see, I do not ask for "how do I declare a
Object variable?", because such answers are found in each manual
website.

The sarcasm you've mentioned - believe me, I know what you are talking
about and I'm kind of immune for it.

pmz

unread,
Jul 29, 2010, 1:18:22 PM7/29/10
to
On 29 Lip, 15:57, Tom Anderson <t...@urchin.earth.li> wrote:
> On Wed, 28 Jul 2010, pmz wrote:
> > I'd like to have ONE default JSP page (which obviously contains a
> > webpage main layout - styles, scripts, tables, images, etc.). In few
> > places I have some dynamical stuff (such as parameter-regarded menu
> > display or also parameter-regarded body display).
>
> > Shall I use you <c:choose/> method for further inclusion of required
> > jsp-sub-pages?
>
> No. You're doing this all wrong. Or rather, you're doing it all PHP, which
> in JSP means wrong.
>
> Use separate pages for the different pages, and a tag file to define the
> common structure:
>
> http://www.oracle.com/technology/sample_code/tutorials/jsp20/tagfiles...

I think I'm getting it now.

P.

Przemek M. Zawada

unread,
Jul 29, 2010, 3:10:15 PM7/29/10
to
W dniu 2010-07-28 22:00, markspace pisze:

>
> In the WEB-INF/web.xml file, you'd bind a pattern to a name, like this:
>
> <servlet-mapping>
> <servlet-name>MainPlugIn</servlet-name>
> <url-pattern>/myPlugin</url-pattern>
> </servlet-mapping>
>
> That replaces mod_rewrite, and maps the url pattern after your hostname
> to the name MainPlugIn. Now you have to do one more thing to map the
> name MainPlugIn to some code.
>
> <servlet>
> <servlet-name>MainPlugIn</servlet-name>
> <servlet-class>myplugin/plugin.jsp</servlet-class>
> </servlet>
>
...

>
> Also, try reading the servlet specifications, it's are surprisingly
> readable.
>
> <http://jcp.org/aboutJava/communityprocess/final/jsr154/index.html>
>
> Java EE 6 Tutorial.
>
> <http://download-llnw.oracle.com/javaee/6/tutorial/doc/>

Well, I've tried your method above (this mapping) - seems to work, but I
still have some difficulties with web layout. I'm browsing the netbeans
tutorials - interesting.

P.

Przemek M. Zawada

unread,
Jul 29, 2010, 3:27:53 PM7/29/10
to
Dear Friends,

I've finally found what I've been looking for - great thanks to all of you.

For future readers, I recommend the NetBeans tutorial, placed here:
http://netbeans.org/kb/docs/javaee/ecommerce/page-views-controller.html

I assume this topic might be closed for now.

Thank you for helping me entering the Java world.

Arne Vajhøj

unread,
Aug 1, 2010, 8:11:27 PM8/1/10
to

True.

But I would expect that to only be needed in relative
few cases due to input validation rejecting input with
<> in.

Arne

Arne Vajhøj

unread,
Aug 1, 2010, 8:12:15 PM8/1/10
to
On 29-07-2010 12:17, Tim Slattery wrote:
> Chris Riesbeck<Chris.R...@gmail.com> wrote:
>> On 7/28/2010 7:25 PM, Arne Vajhøj wrote:
>>>
>>> With newer versions:
>>>
>>> <li><c:out value="${item}" /></li>
>>>
>>> can even be written as:
>>>
>>> <li>${item}</li>
>>
>> c:out will convert HTML special characters to entity code.
>
> It will if you use escapeXML:
>
> <c:out value="${item}" escapeXml="true" />

Isn't that attribute true by default?

>> ${item} by itself does not.
>
> No, but using the JSTL escapeXml function does:
>
> ${fn:escapeXml(item)}

All roads lead to Rime ...

:-)

Arne


Arne Vajhøj

unread,
Aug 1, 2010, 8:15:30 PM8/1/10
to

But Java web apps are not supposed to be tied to a specific
web server.

But I don't think that is the case here.

Certainly mod_rewrite is Apache specific, but the functionality
can be implemented with other software.

Arne

0 new messages