how to avoid creating new session using window.open()? - session

window.open(url) is creating a new session.
Is there any way to prevent window.open() from doing the same. I heard using servlet dispatcher instead of window.open(). we can avoid making new session but i have no idea how to use the dispatcher in jsp or js file. All i have right now is the url in my javascript file.

Related

How to access lazy hibernate collections from jsp foreach in spring?

I need to do forEach over lazy collection in jsp from spring controller. But this invocation crashes because session was closed in controller. I solve this by loding collection via:
Hibernate.initialize(obj.getCollection())
Is it possible to tune mvc to have hibernate session inside jsp?
Thanx
But this invocation crashes because session was closed in controller
I assume by crash you mean that you caused a LazyInitializationException by accessing a mapped collection after the session was closed.
Tuning mvc to have Hibernate session inside you JSP essentially means that you want to hold the session open for the duration of your HTTP request. This means that at render time you can still load the data you require.
You can achieve this using the open session in view pattern. In Spring this is implemented using the OpenSessionInViewFilter. See this answer for more detail on setting it up.
The alternative as you have already demonstrated it to load the data that you require within the transaction which loaded the parent. This is why Hibernate supports lazy loading. Sometimes you will need to load child entities and collections but sometimes you don't. By mapping it as lazy you can choose depending on the specific interaction.

can't get attribute from session

I have a web application, and I want to use an object, which I save in the seesion.
When I run the web app in eclipse it's work fine, but it dosen't work on the browser.
The object which I get from the session is null.
In the servlet I have this code:
request.getSession().setAttribute("wrapper", wrapper);
and in the jsp, where I need this attribute I have the code:
WrapperMLP wrapper = ((WrapperMLP) request.getSession().getAttribute("wrapper"));
thanks in advance
Try making a new session object in servlet as:-
HttpSession session=request.getSession(true);
session.setAttribute("wrapper",wrapper);
and in jsp write your code:
WrapperMLP wrapper = ((WrapperMLP) request.getSession().getAttribute("wrapper"));

Spring MVC Load page automatically

I have a quick question about how to use the Spring MVC.
I just started with it and i was used to have pages that always had their data loaded calling some controller that you setted ON THE PAGE.. calling some methods on controllers to load objects at the page at load time.
Is it the same in Spring MVC? Cuz what im seeing so far is that if you want to load a page with data, you always have to use a modelandview object which is loaded by a method that you must call before... and them you return the modelandview object with its destination to the view that you want.
What im trying to know is if there is a way that the page requests some data from the controller before it gets loaded... automatically...
Dont know if im making my self clear... thanks anyway for the help!
in spring mvc, using Controllers, it is always Request->Controller(populates model and chooses view)->Response.
you could use ajax to load data from any resource (even Controllers) or jsp:useBean to explicitly call some business logic or jsp:include to include a certain fragment or a custom jsp tag.
I also face the same situation.
check the below link for getting the data from the controller before view(JSP) load.
how to load the data into index page

JSF 'Tab' bean scope

I need create webapp with two pages and one backing bean for both pages. I tried #SessionScoped, but when I open other tab in a browser, I see the same data as in previous tab.
I tried to use MyFaces Orchestra, but backing bean is created on each AJAX request.
How to solve the problem?
Beforehand thankfull, Ilya
In a web browser if you open a new tab it will pass along the same session related cookies thus will be opend in the same session context. You need to open a new winodow to get a new session.

JSP Including A Private Servlet

I have a servlet that responds to a URL and then forwards to a JSP in a typical MVC pattern.
Many pages share the same page head so in the JSP there is an include to head.jsp
head.jsp is placed inside WEB-INF so that it cannot be accessed directly.
Now I find that I need to add some control to the head. Rather than forwarding to WEB-INF/head.jsp and putting scriptlets in I would like to forward to a servlet instead.
How can I forward from the JSP to a servlet without mapping that servlet to a URL as I do not want to give direct access to this servlet.
Or to put it another way is there a servlet equivalent of WEB-INF to hide it from direct access? So the servlet can only be called via an include?
Rather than forwarding to WEB-INF/head.jsp and putting scriptlets in I would like to forward to a servlet instead.
It's indeed possible to do this (using <jsp:include> or a small scriptlet that dispatches), but I'm not sure whether this is really the best approach. The Servlet would either write directly to the response or would put some data in the request scope that the JSP can pick up later.
Writing directly to the response is a bit debatable today and for the other approach you don't need a Servlet at all.
The idiomatic way is to use some helper bean that contains the logic. The original Servlet you mentioned can put this bean into request scope, or you can use the <jsp:usebean> tag. Reference the data the helper bean prepared via expression language or very simple scriptlets.
So the servlet can only be called via an include?
If you still want to go this route, there might be an option of securing the Servlet behind a role and then giving the head.jsp a run-as role in web.xml:
<servlet>
<servlet-name>headInclude</servlet-name>
<jsp-file>/WEB-INF/head.jsp</jsp-file>
<run-as>
<role-name>SYSTEM</role-name>
</run-as>
</servlet>
disclaimer: I have never tried this myself, just pointing in a possible direction.

Resources