简体   繁体   中英

create session in Servlet class

I want to create new session without request from browsers. with only one purpose to store data from a API of other system (i will create timer-scheduler to get data from API) and then process it.

I use tomcat server. I configed in web.xml like below:

<?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>TestSession</display-name>
<servlet>
  <servlet-name>initSmsSchedule</servlet-name>
  <servlet-class>test.TestSession</servlet-class>
   <load-on-startup>2</load-on-startup>
</servlet>

and my TestSession.java class:

public class TestSession extends HttpServlet {


   public void init(ServletConfig config) throws ServletException {
     super.init(config);

     //I want to create a HttpSession in here

  }

So with this config. when tomcat start, it will run to init() method in TestSession.

In init() method, I want to create a HttpSession in here. How i can do?

(Normally we usually get session in request when have request from browser. But in my case we can not because it is called from application for itself) Thank you!

A Session by definition is where a user's data is stored.

If you don't have a user who will make a request then it's not possible to create a session for them.

You say you want to get some data from another API and process it, if so why do you need to keep that data in a session?

If the data from the API is per user, then you can store it in that users session, if it's for the entire application you can store it at application level and refresh it as needed.

This is if the data need to be stored in memory before processing, for sometime after retrieval. Otherwise why not just process it from the scheduler?

You solution seem to be a little misguided for me, but may be I'm missing something obvious.

The problem in your question is that of multiplicity of correspondences and their interrelatedness as is usually understood as scoping within web applications.

Simply put, you can't access session from an application-wide bean, or servlet, as well as you can't access request-scoped data from a session. On the other hand, the reverse is true: you can access application data from a session and session data from a request scoped context (of course keeping in mind plausible concurrency issues).

If you think of it more closely, it makes much sense: how can you access session data from Servlet#init : there are many session scoped objects per one application-wide context. You could instead create an application scoped bean that would have a one-to-one correspondence with regards to data scopes.

Finally, in addition to Thihara's answer I would say that it is not the user per se that is needed to create HTTP session, but at least one distinct request, be it made by user authentication, anonymous guest's actions like creating shopping carts or computer bots crawling through your web application. In that respect, as stated elsewhere, the session will be associated with the subject that initiated that request (that had some operations with the session).

As to proceed to the possible ways of solving your problem, you have to carefully rethink scoping of data of your web application. There are many excellent answers on SO, including recent ones, addressing that I problem, that I'll leave for you to look for.

I think of the different scopes available in a web application is different boxes for storing things in.

It sounds to me that you want to store the data from the other API in session scope, although there is no session. I don't think storing it in session will do you any good at all - it can only be accessed from the same session, and when the session expires (soon after starting tomcat) there'll be no way to access the data at all. What's the point in that?

If you store is in Application scope, then the data will be accessible from any other (real) session.

From a servlet, the Application (Servlet) scope can be accessed via:

getServletContext().setAttribute("key", object);  
Object object = getServletContext().getAttribute("key"); 

Just like Session scope, it is still backed by a Map.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM