Does an http request on a JSP page open a session?

0

I have a web application that contains some JSP pages that return a JSON.

This application has a large stream of access, and I noticed that there are many open sessions in the monitoring area of the GlassFish server.

See a piece of code that is in the JSP:

 <%
      RequestLive ao vivo = new RequestLive ();
      out.print (live.search ());
 %>

That's all I have in the JSP, the rest of the Java class does the service, and JSON is printed on the page.

The real question is, is a session open if I access the URL to retrieve the data?

  

Ex: www.mydomain.com/RequestLive.jsp

Should I call session.invalidade (); or is it correct as I am using it?

    
asked by anonymous 10.09.2015 / 00:50

1 answer

0

If it's a "RestService" that wants to do something like this:

import java.io.IOException;  
import java.io.PrintWriter;  
import java.util.ArrayList;  
import java.util.List;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import org.json.JSONArray;  
import org.json.JSONException;  
import org.json.JSONObject;  
import br.com.dominio.Pessoa;  
public class PessoaServlet extends HttpServlet {       
       private static final long serialVersionUID = 1L;  

       @Override  
       protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {  
            recuperarPessoas(request, response);  
       }  
       @Override  
       protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {  
            recuperarPessoas(request, response);  
       }  
       private void recuperarPessoas(final HttpServletRequest request, final HttpServletResponse response) throws IOException {  
            try {  
                 request.setCharacterEncoding("UTF-8");  
        response.setCharacterEncoding("UTF-8");  
        response.setContentType("application/json");   
        PrintWriter out = response.getWriter();   
                 List<Pessoa> procedimentos = getPessoas();  
                 JSONArray jsonArray = new JSONArray();  
                 for (Pessoa obj : procedimentos) {  
                      JSONObject jsonObject = new JSONObject();  
                      jsonObject.put("nome", obj.getNome());  
                      jsonObject.put("idade", obj.getIdade());  
                      jsonArray.put(jsonObject);  
                 }  
                 out.print(jsonArray);  
            } catch (JSONException e) {  
                 e.printStackTrace();  
            }  
       }  
       private List<Pessoa> getPessoas() {  
            List<Pessoa> pessoas = new ArrayList<Pessoa>();  
            Pessoa pessoaUm = new Pessoa();  
            pessoaUm.setNome("Gustavo Lopes de Oliveira");  
            pessoaUm.setIdade(25);  
            Pessoa pessoaDois = new Pessoa();  
            pessoaDois.setNome("Ralph Miranda");  
            pessoaDois.setIdade(28);  
            pessoas.add(pessoaUm);  
            pessoas.add(pessoaDois);  
            return pessoas;  
       }  
  }  

Source: JSON SERVLET

Or you can use Frameworks that help develop REST Service, Spring MVC etc.

    
11.09.2015 / 21:51