4
  • I am working on Spring-MVC application in which I am setting and getting some session attributes which I need in backend. The problem is, Spring or the browser, someone out of the both is tying these session attributes to different browsers rather than different tabs.
    • So if I open a new tab in the same browser, then it is updating the session attribute for both tabs. How can I remedy this problem? I tried using session to scope, request, etc. But nothing works. here is my controller and servlet-context.xml

Controller :

@Controller
@Scope("request")
public class PersonController {


    @Secured("ROLE_USER")
    @RequestMapping(value = "/loadsection/{id}")
    public String loadNotePage(@PathVariable("id") Integer id, HttpSession session) {
// here i am setting the canvasid, which I would like to access in other methods
        session.setAttribute("canvasid",id);
        if (this.personService.returnCurrentOperationalMode()) {

            session.setAttribute("canvasid",id);
            return "redirect:/section/listing";
        } else {
            GroupCanvas mcanvas = this.groupCanvasService.getCanvasById(id);
            this.personService.setCurrentCanvas(mcanvas.getMcanvasid());

            return "redirect:/section/listing";
        }

    }


    @Secured("ROLE_USER")
    @RequestMapping(value = "/addbatchsections", method = RequestMethod.POST)
    public @ResponseBody String addBatchSections(HttpSession session, @RequestBody Section[] sections) {
        int canvasid = (Integer) session.getAttribute("canvasid");
        try {
            List<Section> sectionList = new ArrayList<>();
            for (Section section : sections) {
                sectionList.add(section);
            }
            this.sectionService.addBatchSections(sectionList,canvasid);
            return "success";
        } catch (Exception e) {
            return "failure";
        }
    }

Servlet-Context.xml

   <mvc:annotation-driven/>

    <mvc:default-servlet-handler/>

    <resources mapping="/resources/" location="/resources/" />

    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <beans:property name="driverClassName" value="org.postgresql.Driver"  />
        <beans:property name="url"
            value="jdbc:postgresql://localhost:5432/Person2"/>
        <beans:property name="username" value="postgres" />
        <beans:property name="password" value="asayhk2787" />
        <beans:property name="removeAbandoned" value="true"/>
        <beans:property name="removeAbandonedTimeout" value="20"/>
        <beans:property name="defaultAutoCommit" value="false"/>
    </beans:bean>


    <!-- Hibernate 4 SessionFactory Bean definition -->
    <beans:bean id="hibernate4AnnotatedSessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource"  />
        <beans:property name="annotatedClasses">
            <beans:list>
                <beans:value>com.journaldev.spring.model.Person</beans:value>
                <beans:value>com.journaldev.spring.model.Notes</beans:value>
                <beans:value>com.journaldev.spring.model.Canvas</beans:value>
                <beans:value>com.journaldev.spring.model.Section</beans:value>
                <beans:value>com.journaldev.spring.model.Attachment</beans:value>

                <beans:value>com.journaldev.spring.model.GroupAccount</beans:value>
                <beans:value>com.journaldev.spring.model.GroupMembers</beans:value>
                <beans:value>com.journaldev.spring.model.GroupCanvas</beans:value>
                <beans:value>com.journaldev.spring.model.GroupSection</beans:value>
                <beans:value>com.journaldev.spring.model.GroupNotes</beans:value>
                <beans:value>com.journaldev.spring.model.GroupAttachments</beans:value>

                <beans:value>com.journaldev.spring.model.Token</beans:value>
                <beans:value>com.journaldev.spring.model.WaitingMembers</beans:value>
                <beans:value>com.journaldev.spring.model.NoteHistory</beans:value>
                <beans:value>com.journaldev.spring.model.GroupNoteHistory</beans:value>

                <beans:value>com.journaldev.spring.model.Feedback</beans:value>
            </beans:list>
        </beans:property>
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL82Dialect
                </beans:prop>
                <beans:prop key="hibernate.show_sql">false</beans:prop>
                <beans:prop key="connection.pool_size">200</beans:prop>
                <beans:prop key="c3p0.max_size">200</beans:prop>
                <beans:prop key="c3p0.timeout">1000</beans:prop>
                <beans:prop key="hibernate.jdbc.batch_size">100</beans:prop>
                <beans:prop key="hibernate.order_updates">true</beans:prop>

            </beans:props>
        </beans:property>
    </beans:bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <beans:bean id="transactionManager"
                class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory"/>
    </beans:bean>

I hope my question was clear, if there is any doubt, please feel free to ask me. Thanks a lot. :-)

We are Borg
  • 5,117
  • 17
  • 102
  • 225
  • Perhaps the following thread could be of interest http://stackoverflow.com/questions/26757128/spring-mvc-sessionattributes-issue-in-multiple-browser-tabs/26758402#26758402 – Master Slave Mar 05 '15 at 14:39
  • @MasterSlave : My problem is the values I am saving are the same, and the thread doesn't explain how it is doing whatever it is doing. – We are Borg Mar 05 '15 at 14:56
  • Did you find solution? – jgr Jul 13 '15 at 15:27
  • 1
    @jgr : Yes, but we changed our architecture. Spring-security has a way, to define each tab a session. You can check out some docs. – We are Borg Jul 14 '15 at 07:08
  • If you found a solution to your question it would be good to put the exact approach and preferably some up here for the benefit of myself and others – Deepak May 11 '18 at 08:40

2 Answers2

1

Take a look at Spring Session and its associated examples.

manish
  • 19,695
  • 5
  • 67
  • 91
0

Browsers like Chrome on new tab or new window share the user session because use the same session cookies.

Check this or change your session approach.

Community
  • 1
  • 1
fantarama
  • 862
  • 6
  • 14
  • Thanks, I already found that HTML5 SessionStorage can be used. I am just searching how to use it in Java to set the canvasId attribute mentioned in the main post and retrieve it in the Controller. If you know how, kindly let me know. – We are Borg Mar 05 '15 at 14:48
  • So you need to share the same canvasId across different web sessions? – fantarama Mar 05 '15 at 15:11
  • Exactly the opposite, Each browser tab has its own canvasid. – We are Borg Mar 05 '15 at 15:14
  • 1
    As I wrote in the response you need an approach to "disable" the browser session sharing, try the one suggested by @manish, you need to bind different session to each tabs so every tab will need to log in. – fantarama Mar 05 '15 at 15:28
  • Is there any other way where I can maintain the user logged in, but can also get the updated canvasid which is set in each tab by the user himself. That is what I am more interested in. – We are Borg Mar 05 '15 at 16:10