Spring MVC #RequestMapping opening a different jsp - java

I have a SpringMVC application, and I need it to open a completely different jsp. If I am validating a transaction (in "editing.jsp"), and the data passes all editing steps, and it gets written successfully to the database, I need to redirect to display "success.jsp". Both jsp files exist in the WEB-INF/jsp directory. Ideally I'd like to also return some data to display in that page. This seems like it should be simple to do, but so far, I have not been able to discover how to change my #RequestMapping method do do it.
Perhaps I need to somewhere set up a mapping between "success.jsp" and a new different url?
Thanks in advance.

Related

How to reuse a JSP with a controller?

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.

How to render .jsp from database without .jsp physical file

I am developing an application where I will have only loader.jsp which will load all other jsp files from the database. Like I am having all the coding for jsp file in database so that i can able to change the html content without restart the server in database itself.
Is there any framework already existing to implement this and what is the best way to achieve this?
If yes for render .jsp file dynamically, how to render css and js dependency also dynamically?
So my exact requirement is, I will have only loader.jsp which will load all other pages stored in database based on the path param.
http://localhost:8081/myApplication?page=mypage
TECHNOLOGIES USING: java spring boot
NOTE 1: I am not gonna render based of conditions or some parts of the file, I need to render entire page as it is from the database columns.
NOTE 2: if loading .jsp is not possible since jsp code need to be compiled, then can i load .html file? ex: loader.jsp will load a.html, b.html, c.html from database
NOTE 3: Am building application where user can able to drag and drop UI elements to create webpages and once if they save, their changes need to reflect on run time without restart. like they have one page in their own application itself to change their UI elements.

Java EE Clarification on Workflow for JSP and Servlets

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

Storing HTML file rendered by JSP

I need to create a report using Servlet and jsp. My problem is this, there is a page which list the previously generated report. One can click on any of this report from list and view that report again. For this I have to keep the static html file Rendered by the jsp on the server.
How can i obtain this. I don't want to render the jsp file twice as this is time consuming.
Thanks in advance.
You could cache the reports by adding a Filter. The filter can check if an already rendered version is available and return the contents of that file. If no file is present it can write the resulting response to the cache.
Basically, you should write the body of your HTTP response object to a file on your webserver and write some kind of service/servlet to retrieve these files.
I guess the easiest way to do this, is by writing a ServletFilter that intercepts the responses of your JSPs.
The current Oracle Java EE 6 tutorial is rather brief about Filters, but you can read all about them in the previous version : http://docs.oracle.com/javaee/5/tutorial/doc/bnagb.html

How do I display information from database to the view persistently in a JSP MVC application?

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.

Categories

Resources