From: pmz on
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
From: Chris Riesbeck on
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.


From: pmz on
On 28 Lip, 20:37, Chris Riesbeck <Chris.Riesb...(a)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)

>
>
>
>
>
> > 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...
>
> 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.

All the best,
Przemek M. Zawada
From: pmz on
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
From: markspace on
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/>