Results 1 to 3 of 3
- 12-22-2011, 01:22 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 2
- Rep Power
- 0
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:
In my jsp I need to get the ArrayList's content and I did like this:Java Code:ArrayList<String> analistasLogados = new ArrayList<String>(); analistasLogados.add(usuario.getNome()); ServletContext context = request.getSession().getServletContext(); context.setAttribute("logados", analistasLogados);
My xml is like that: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)); }
<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
- 12-22-2011, 09:40 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: I need to inicialize an ArrayList together the Servlet Container
Where is that first bit of code?
- 12-22-2011, 11:49 AM #3
Member
- Join Date
- Dec 2011
- Posts
- 2
- Rep Power
- 0
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
-
Online JSP/Servlet container
By an24 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 02-17-2011, 06:24 AM -
Help with adding a Container within a Container
By JoKeR313 in forum New To JavaReplies: 4Last Post: 01-02-2011, 04:59 AM -
how to add Arraylist filter for a jsp page showing results from a servlet-Arraylist
By alok_sharma in forum Java ServletReplies: 7Last Post: 11-22-2010, 01:26 PM -
Memory issue in servlet application in a tomcat container
By bedhinesh in forum Java ServletReplies: 2Last Post: 07-24-2009, 01:26 PM -
[SOLVED] Coordinates container --> in parent container.
By Cleite in forum AWT / SwingReplies: 3Last Post: 04-21-2009, 11:01 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks