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>
Related
I'm new to web development with Java, so please excuse me if I'm butchering the terminology:
I'm building a web app with JSPs/servlets using the Java MVC model. I'm including a register/login option on the top menu that will of course need to communicate with the server (handle registering/logging in or retreiving the user's name).
I want to reuse both the JSP and controller code for the top menu as it should be on every page. I'm able to reuse the menu page using <c:import>. However, the menu will appear on pages that have their own functionality and therefore their own controllers. I can't figure out how to reuse the controller code for the menu on these pages as I can only map one servlet to a URL.
I don't have much code to show as an example at this point. What's the best practice for reusing common functionality like this without interfering with page specific functionality?
Not sure if I understand the problem correctly.
What about passing a hidden field in your form(which, from my understanding would point to the same mapping in your controller) specifying which jsp the request is coming from ? That way you know which jsp to call back from your controller.
I am attempting to learn Java EE. After struggling through trying to understand the Java stack, it appears that that a basic dynamic web application can be accomplished by focusing on JSP and Servlets so I want to start there.
In Net Beans I have a basic project set up with a tomcat server and when I hit run I can get the web page to generate and display the default index.jsp page. I have added a css file to the project and this jsp page looks like any standard html page now. I then create a new servlet. I was also able to create a link in the index.jsp page so when I click on it, it calls the servlet.
What I don't understand is why when I create the servlet in Net Beans, the processRequest method comes pre-populated with several html print statements. I could easily make this page look like the jsp page I started with, making me wonder why I even need a JSP page. It appears the entire application could be comprised of servlets.
Could someone please explain the proper interaction between the two? It appears the index page can contain links that call various servlets, perhaps to access a database of comments, then do I create the response table in the servlet, or would I pass that information to another JSP page?
Understanding these basic workflows would really help. Thanks
JSP also a kind of a servlet . Right after you run the jsp , go to your netbeans project directory and right click the jsp and select view Servlet then you get the servlet representation of your jsp file. Actually jsp file is there to perform front end design easily. Without jsp It is really difficult task to perform front-end developing stuff inside servlet.
And you should keep in mind there is no such a thing called proccessRequest in javaEE . It is automatically generated by Netbeans once you create a new servlet. So just stick to basics. Erase all the auto-generated stuff from the class which has extended HttpServlet. And then you can override the required HTTP method/methods. (doGet , doPost etc..) You should take a look at this for more infromation ...
And this page will also improve your knowledge on javaEE
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.
For my college project I have to build a custom JSP/Servlet MVC application so I can't use frameworks such as Struts or Spring. I already have FrontController, Command, Service, DAO, Business layers.
Let's say I want to create a website with a sidebar and in the sidebar has the following widgets: Members, Who's Online, Recent Comments. Each widget accesses the database via Command -> Service -> Dao.
I want information to be displayed in the sidebar constantly throughout the application. Problem is I don't know how to do this. I know how to display information by processing GET/POST requests but I don't know how to display information (from database) without GET/POST requests if that makes sense?
Couple of ways I tried that don't work:
1) Upon loading the homepage and invoking the HomeCommand calls ListUsers from UserDao and then stores them into a session. But if the user enters the site from a different URL ListUsers won't be stored into a session.
2) Creating a separate Command: MembersCommand, WhosOnlineCommand, RecentCommentsCommand. Then use JSTL include to include the FrontController and to get it to invoke the Command. But include wants a .jsp
<jsp:include page="FrontController/members" />
Fragment "FrontController/members" was not found at expected path /MyApplication/WebContent/WEB-INF/FrontController/memmbers
3) Create individual .jsp's for each widget (members.jsp, whosonline.jsp) with Java code to access the Dao. Then use JSTL include. But how would I get it to go through the FrontController and isn't Java in jsp's a big no-no?
4) Use <jsp:forward page="" /> but this gives me a blank page?
I'm out of ideas?
check this tutorials for MVC architecture for java web applications. hope you can get some
insight
http://www.javaranch.com/journal/200603/frontman.html
Design Patterns web based applications
http://balusc.blogspot.com/2008/07/dao-tutorial-data-layer.html
Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern
I didn't understand all of what you are trying to say, but perhaps a servlet design pattern can help. Typically with a front controller, you access your persistent storage, and then store what you need to store in "session" or "request" scoped areas. However, since you talked about "another web site" I think you may need to store your data in the "application" type area. If you are using a servlet as a front controller, this area is accessed by getServletContext().setAttribute() and later your JSP page can just access the variable the same way as it does to session-scoped variables. I couldn't tell if your front-controller was a servlet or a JSP, but it doesn't matter because both are able to store application-scoped variables.
In a real business environment, this is not sufficient, because servlets are often shared amongst multiple machines, and can be re-started and stopped many times; and therefore any persistent information would have to be retrieved directly from the database. However, for a project, storing data in application-scoped variables is sufficient.
I know I missed a good part of what you were explaining, but my point is a front controller accesses (several) persistent storages, loads up all the data needed for the returned jsp pages into session or request or application variables(in this case) and the jsp file can be written to make use of all the data that had been stored in these variables.
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>