JSP directory structure - java

I've been starting doing some Java and JSP and now, I'm stuck. I'm not happy with having all static HTML's in the root-folder. If I would pack those into a separate folder. At the moment, I see two options: configure it in the web.xml, but that only works for the servlets afaik. Configuring Tomcat on the other hand might be an option, but it would break the internal logic, even if it worked.
Is there a correct way for organizing the files and not having that reflected in the URL?

When you speak about static pages something like say header and footer.
You can place them under any subfolder say inside jsp/static/(subcontent) or depending on the type of your file you can group them even further.
Now the usage, since because you are with plain jsp and servlet the only option that I think makes sense for static content is that include them whereever required with include directive like:
<%#include file="/pages/static/abc.html" %>
or forward the request to them with
<jsp:forward page="/pages/static/staticdata.html" />
Understanding that this is a pain, if the navigation needs to be changed you will have to change your code, then more advanced web framework like Struts, JSF, e.t.c makes sense, as navigation rules can be changed very easily without changing code (unless the flow causes model to change or any different scenario).
Along with navigation rules these framework also specify page building configuration like tiles in Struts, templates in JSF e.t.c

You can actually map jsp files to urls in web.xml using
<jsp-file></jsp-file> tag
Example
<servlet>
<servlet-name>somName</servlet-name>
<jsp-file>newFolder/mypage.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>somName</servlet-name>
<url-pattern>/oldUrl.jsp</url-pattern>
</servlet-mapping>

Related

Does Spring MVC 3.x support partial views (html/jsp) like that of ASP.NET MVC3/4?

Q1: Does Spring or any opensource java UI framework support partial views like that in asp.net mvc?
For example in my main index.html (or _layout.cshtm per asp.net mvc3 spec)
I would have the folllowing code:
<span id="logindisplay">#Html.Partial("_LogOnPartial")</span>
where #Html is a helper to display a partial view for _LogonPartial.cshtml which just injected it's html view contents into the page?
Q2: If this is supposed If I want to display a bunch of partial views it would be helpful to display them concurrently in parallel to improve performance. Very similar to what linkedin is doing using dust and fizzy? http://engineering.linkedin.com/profile/engineering-new-linkedin-profile
Q3: Is fizzy available as open source like that of dust?
If you want to include content of a page into another page, by adding some code on the page itself, you should compare asp with jsp, not ASP.NET MVC* with JEE - Spring MVC
So, an equivalent to <span id="logindisplay">#Html.Partial("_LogOnPartial")</span> on a jsp would be one / all of the following
On your jsp, include content from another jsp using <%# include file="../includes/inner-content.jsp" %>. This is what is called a static include. The source of the included jsp is included and made part of the parent jsp, before the jsp is compiled. If you use an IDE, it will check to ensure the included jsp does infact exist at the path specified, relative to the location of the jsp in which you are adding the include. Technically this is a JSP Directive. The jsp being included could just be a fragment, and not addressable from the outside world (could be hidden inside WEB-INF)
You can also use what's called a Dynamic include <jsp:include page="someJSP.jsp" />. In this case, the included JSP should be addressable from the browser and should be capable of being rendered independently. When the server is executing the servlet to render the parent JSP, it stops when this tag is seen, and starts executing the servlet for the included jsp, the output obtained from the inner jsp execution is then merged to the output of the parent jsp, and processing of the parent jsp is resumed.
A third option would be to use Core JSTL taglib's <c:import url=""/>. This works just like option 2 above, except it also allows you to import a page / content from a url that lives outside your application. Basically you can mention a path to a jsp, or relative URI to a servlet mapping in your application, or a URL to an external page.
Now, I suspect this is not really what you want to do, if you are comparing with what Linkedin is doing. You want to mashup content from sources in your own application, and compose your page. You also want to do this in an asynch manner so as to keep load times in check. In this case, you HAVE to use JavaScript and Ajax. All the mechanisms described above are for server rendered pages (All HTML is created before the page is rendered in the browser). Just like #HTML. You need to create a simple framework / use an existing one, where once a page loads, it fires asynch ajax calls to the server to get content for specific areas on the page and renders the returned HTML in those specific areas.
Hope this helps.
Do let me know if I've misunderstood your question.

Java Servlets Convention

I have a question about Java servlets convention. In looking at any tutorial for servlets, whether it be Eclipse, NetBeans, etc., they always have you create an index.jsp page. Once the page is created, they have you create a form with a "submit" button that jumps you to the servlet for processing. My question is, in a servlets project, do you have to use the main index.jsp page, or can your project go immediately to the servlet?
I am working on my own little project to learn servlets, a project that connects to my local MySQL database, displays the list of schemas you can choose from, then displays the table data for each schema on the next page. In order to dynamically grab a list of schemas on the main page, I will need a servlet, not an index.jsp page. I know this can be done with JSP or JSF, but I would like to use servlets only.
This is where my original question comes in. Can my project go to an initial main servlet instead of an index.jsp page, or does convention, or technical matters, prohibit this?
Thank you for taking the time to read. Have a good day.
Not exactly needed.you can direclty call a servlet.There is no harm in that.
like
<welcome-file-list>
<welcome-file>/index</welcome-file>
</welcome-file-list>

How can I use JSP to generate content pages with a non-JSP extension?

I'm developing a web application to be deployed on the latest Glassfish server.
To make the application compatible with different context roots (like "/apps/myapp/"), I need the CSS files in it to be generated dynamically.
The problem is that the these pages aren't treated like JSP files so I can't use <%= contextRoot %>. I know I could use a JSP file with a Content-Type header to mimic a CSS file, but I would prefer to have a CSS extension on it.
Is it possible to have Glassfish treat a non-JSP file as a JSP file?
This is simple, I've done it before, works great.
Simply take the extension you want to map, and map it to the JSP servlet.
JSPs are processed by a servlet, like anything else. Nothing really special about them.
So, for Glassfish, this servlet happens to be named "jsp". I don't know whether that is portable (i.e. the name), but it likely runs in Glassfish and Tomcat, and probably anyplace that uses the Jasper JSP compiler.
In Glassfish, it's defined in $glassfish_domain_dir/config/default-web.xml.
So, add this to your web.xml
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
</web-app>
The nice thing that this will pretty much work for straight up CSS files if there's no markup in them, or with custom ones that you add markup too.
If you don't have too many CSS files to work with, you could add a servlet mapping for each CSS file which would redirect to a servlet and render the JSP.
I don't know that what you are trying to accomplish is really necessary. You could just use scriptlets or jstl to dynamically append the context root when linking to your CSS,JS,images,etc.
You can see a discussion about that here:
Any clever ways of handling the context in a web app?
You could use a jsp include directive.
<%# include file="something.css" %>
<%# include file="something.xyz" %>

How to publish javadoc instead of index.jsp page for a REST web service

I was wondering if it is possible for a client to hit my webservice and instead of it showing the normal index.jsp page (and list off what is available etc here) I can instead have it show the javadocs from methods available through the webservice. I am quite new to REST / webservices in general, but I am using myeclipse and I do know that currently it is accessing the index.jsp page through the web.xml file like so:
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
The main goal of this is to not have to spend lots of time creating a welcome page that shows each of the methods available through my webservice, but instead try to automate it / make it more readable.
Any help would be appreciated, and if more clarity / information is required I will be glad to add it. Thanks.
edit:
I ended up just exporting as javadoc and selecting only the files I needed / only the public methods. Then placed the created doc folder inside of the webroot and edited the web.xml to
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
and placed <FRAME src="doc/ in front of the normal path in the index.html as well as Link to<A HREF="doc/ so that it knew to search relative to the doc folder instead of the webroot.
If you generate a javadoc for all your pages (there are excellent tools out there to do that, even integrated into the maven build system) you can easily map those pages into your web app directly. So instead of displaying your index.jsp, you display the index.html from the generated javadocs.
Not sure about the Javadocs, but have you considered a WADL file to describe your services?
If you're using Jersey, this article may help:
http://wikis.sun.com/display/Jersey/HowToConfigureExtendedWADL

Spring 3 - Theme with separate JSP

I'm trying to rewrite some Spring 1.2 code to Spring 3.0 one. Currently I'm stuck with JSP resolved by URL problem. Application uses separate JSP files with different layouts for serving the same model from the same controller. The JSP is switched using interceptor, that intercepts the url and changes the view.
For example:
/design_one/mypage.htm -> MyPageController -> /design_one/mypage.jsp
/design_two/mypage.htm -> MyPageController -> /design_two/mypage.jsp
Is there a way to make same or similar functionality using something better than raw interceptors?
Well, I've found a way to do it, but not sure if it's the optimal way.
What I did, I defined a name for each theme in their resource bundle, like "name=design_one". And then using it in JSP to include some other JSP's inside.
Something like this:
<spring:theme code="theme.name" var="themeName"/>
<jsp:include page="${themeName}/head.jsp"/>
<jsp:include page="${themeName}/foot.jsp"/>

Categories

Resources