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

Asked

Viewed 136 times

0

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

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

See a piece of code that is on JSP:

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

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

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

Ex: www.mydomain.com/Requestlive.jsp

I should call the session.invalidade (); or is correct as I am using?

  • 1

    The question has been translated!

  • What live.seach() does? After printing what does one do? Will it read and copy or is it just to consume in other applications? If this is a restService, it would be interesting to do in a Servlet.

  • Do a search and return in a JSON, the page works as a REST yes...

1 answer

0

If it’s a "Restservice" who 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 else you can use Frameworks that help you develop REST Service, Spring MVC etc.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.