Prev: Programming Java 3D API Book Recommendation
Next: Apache Tomcat https link goes down but http works
From: Arne Vajhøj on 28 Jul 2010 20:21 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
From: Arne Vajhøj on 28 Jul 2010 20:25 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
From: Lew on 28 Jul 2010 20:36 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
From: markspace on 28 Jul 2010 20:43 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?
From: Lew on 28 Jul 2010 20:47
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 |