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.
Related
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
I'm just starting out with Spring MVC having been trying to pickup Java(Web) for the few months.
I'm sure what I'm trying to achieve is very simply, but I can't find a solution that really works.
I'd like to include another controller/view inside a view. This could be a menu, or some other dynamically generated content. For example, let's say in a side bar of an application I need to display a list of categories. I don't want to have to retrieve these categories in every controller/view that needs to use them. I'd like to include another controller/view into the main page view so that this content can be placed in multiple views but managed by a single piece of code.
ASP.NET MVC has something called PartialViews, and most PHP frameworks seem to offer this kind of functionality, but I can't find anything like this in spring.
The closest I've come to resolving this issue is to use the jsp:include tag, which does work, infact it's almost the solution, but it generates errors in Eclipse because obviously Eclipse can't locate the path, as it's a spring RequestMapping i.e. /include/categories rather than a direct link to a physical file.
The other solution is to use Javascript to dynamically load content into the sidebar, but I don't want to do that.
Is there a 'correct' way to do this, or is there a way to supress the errors generated by eclipse for the jsp:include tag?
If the content your are including is static and you don't have a lot of different pages, your approach with including jsps is ok. Otherewise, look at the templating frameworks like Tiles or Thymeleaf.
I recently developed a whole system in Java that connected to a database and exports and imports the table content to an excel sheet. I used SWING for the user interface. the user will interact with it for authentication and file management.
Apparently the client changed the requirements, He wants everything from a Web Interface. My team leader advised to look through JSP.
What does JSP actually do?
Will I have to rewrite the User Interface in Web if I used JSP?
is there an more effective and efficient solution to do this job?
I would Appreciate a specific answer
I'm not sure what you mean by "specific answer", but here goes:
JSP is a kind of template language, based on Java, and a technology for dynamically generating HTML. It's a server side technology. Look here.
Yes, if you're going for a pure web/HTML solution, you'll need to completely rewrite the UI.
There are other frameworks for creating webapps, such as Vaadin or Play! Framework that may be "better" than JSP, but then there's a whole new API/framework to learn...
What does JSP actually do?
Will I have to rewrite the User Interface in Web if I used JSP?
is there an more effective and efficient solution to do this job?
and
I used SWING for the user interface.
and
exports and imports the table content to an excel sheet.
not, have to look at JavaFX 2
You will certainly need to rewrite the user interface if you convert to JSPs.
JSPs are essentially just a method for dynamically generating HTML (with the option to embed Java code to produce parts of the page).
It is still possible to run Swing applications from a web browser: you might want to take a look at Java Web Start. This will save you from having to do a complete rewrite.
1.) JSP is pretty much like PHP. It is server side scripting. When ever a browser request for a page (JSP page), server (mostly Tomcat or any application server which you deploy your JSP project) will generate HTML content using the JSP code. Mainly JSP consist of part HTML, JavaScipt (if you want dynamic stuff), and Java.
2.) As far as I know if you are aked to do it in JSP then you need to o all the client side work again in JSP. There you will be generating HTML UIs using Java codes. But you can use all the back end codes you used.
You may can use SWING in a Applet.
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.
We have a website which is coded Java with Struts Framework. The WebSite's Urls are not seo friendly. All of them are like below
../buyerApplication.do&companyId=2323
Now We want to make these URLs SEO friendly and I searched and found these solutions:
tuckey.org/urlrewrite : but i don't rely on this system.
adding
title end of link after '&' such as
"../newsId=33233&does-art-in-the-city-equal-art-for-the-city"
: In this solution I am not sure it
works well.
I am waiting your sugestions to solve this problem best.
I actually used URLRewriter (http://tuckey.org/urlrewrite/), which you referenced in your original question. It was very easy to set up and filled my needs perfectly.
To the point, you need a Filter for this.
If you want to keep your existing application's architecture, you'll need to define and create a set of rules to convert unfriendly urls to friendly urls and let the filter convert it and forward the request to the unfriendly url.
If there is no means of modifying an existing application but you want to create a new application based on this idea, you could consider to having a single page controller which translates the HttpServletRequest#getPathInfo()/getRequestURI() to execute the appropriate action class (command pattern) and finally forward the request to the appropriate JSP page. Not sure how that would fit into Struts as I haven't worked with Struts previously.
For what it's worth, you can also look at the REST plugin http://struts.apache.org/2.x/docs/rest-plugin.html, which amongst other things will make your URLs more friendly