Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default I need to inicialize an ArrayList together the Servlet Container

    hey guys,

    I managed to put an object(ArrayList) in the ServletContext. In my servlet I did this way in order to create the ArrayList and put it in the ServletContext:

    Java Code:
    ArrayList<String> analistasLogados = new ArrayList<String>();
    analistasLogados.add(usuario.getNome());
    ServletContext context = request.getSession().getServletContext();
    context.setAttribute("logados", analistasLogados);
    In my jsp I need to get the ArrayList's content and I did like this:

    Java Code:
    ServletContext context = request.getSession().getServletContext();
    ArrayList<String> analistasLogados = (ArrayList<String>)context.getAttribute("logados");
    for(int i=0;i<analistasLogados.size();i++){
    out.println((String)analistasLogados.get(i));
    }
    My xml is like that:

    <context-param>
    <param-name>logados</param-name>
    <param-value>analistasLogados</param-value>
    </context-param>

    The problem is that each time that I call the Servlet it creates again the ArrayList. Is there any way of inicialize the ArrayList together the application instead of create the ArrayList inside the Servlet's code? Sorry about my bad english.

    Thanks

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,050
    Rep Power
    20

    Default Re: I need to inicialize an ArrayList together the Servlet Container

    Where is that first bit of code?

  3. #3
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default Re: I need to inicialize an ArrayList together the Servlet Container

    I have an index.jsp where the user put his login and his password and this page sends these information to a Servlet called Logar which has the first bit of code that I have posted. Here is the complete code of my servlet Logar:

    Java Code:
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.util.ArrayList;
    import java.util.Queue;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.mysql.jdbc.interceptors.SessionAssociationInterceptor;
    
    public class Logar extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    
    	public Logar() {
    		super();
    	}
    	
    	public void init(){
    		
    	}
    
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    	}
    
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		Connection conexao = null;
    		PreparedStatement stmt = null;
    		ResultSet res = null;
    
    		try{
    			String login = request.getParameter("login");
    			String senha = request.getParameter("senha");
    
    			Class.forName("com.mysql.jdbc.Driver");
    			conexao = DriverManager.getConnection("jdbc:mysql://189.126.99.100/pausa_trafego", "pausa_trafego", "fancong@123");
    
    			stmt = conexao.prepareStatement("SELECT * FROM tbl_usuarios WHERE usuario=? and senha=?");
    			stmt.setString(1, login);
    			stmt.setString(2, senha);
    			res = stmt.executeQuery();
    
    			UsuariosBean usuario = null;
    
    			if(res.first()){
    				usuario = new UsuariosBean();
    				usuario.setId_usuario(res.getInt("id_usuario"));
    				usuario.setNome(res.getString("nome"));
    				usuario.setUsuario(res.getString("usuario"));
    				usuario.setSenha(res.getString("senha"));
    			}
    
    			if(login=="" && senha==""){
    				request.getSession().setAttribute("erro", "Por favor preencha os campos usuário e senha");
    				response.sendRedirect("index.jsp");
    			}else if(senha=="" && login!=""){
    				request.getSession().setAttribute("erro", "Impossível logar sem informar a senha");
    				response.sendRedirect("index.jsp");
    			}else if(login=="" && senha!=""){
    				request.getSession().setAttribute("erro", "Impossível logar sem informar o usuário");
    				response.sendRedirect("index.jsp");
    			}else if(!res.first()){
    				request.getSession().setAttribute("erro", "Login ou senha incorretos!");
    				response.sendRedirect("index.jsp");
    			/*}else if(!usuario.getSenha().equals(senha) || !usuario.getUsuario().equals(login)){
    				request.getSession().setAttribute("erro", "Login ou senha incorretos!");
    				response.sendRedirect("index.jsp");
    			*/}else{
    				ArrayList<String> analistasLogados = new ArrayList<String>();
    				analistasLogados.add(usuario.getNome());
    				ServletContext context = request.getSession().getServletContext();
    				context.setAttribute("logados", analistasLogados);
    				
    				request.getSession().setAttribute("nome", usuario.getNome());
    				request.getSession().removeAttribute("erro");
    				response.sendRedirect("/pausa/sistema");
    			}
    			
    		}catch(Exception e){
    			throw new ServletException(e);
    		}finally{
    			try{
    				res.close();
    				stmt.close();
    				conexao.close();
    			}catch(Exception e){
    				e.printStackTrace();
    			}
    
    		}
    	}
    
    }

Similar Threads

  1. Online JSP/Servlet container
    By an24 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-17-2011, 06:24 AM
  2. Help with adding a Container within a Container
    By JoKeR313 in forum New To Java
    Replies: 4
    Last Post: 01-02-2011, 04:59 AM
  3. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  4. Replies: 2
    Last Post: 07-24-2009, 01:26 PM
  5. Replies: 3
    Last Post: 04-21-2009, 11:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •