Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to pass the ArrayList form servelet to jsp?

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to pass the ArrayList form servelet to jsp?

    Hi,

    I m new in Servlet side programming.i want to pass the ArrayList from Servlet to JSP page.how do i do this? below is what i tried.it allways pass Null value.My java file name is Login.java and my jsp file name is FirstJSP.jsp.

    FirstJSP.jsp
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>`
    <%@ page import="java.util.ArrayList" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org /TR/html4/loose.dtd">`
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>`
    </head>
    <body>


    <%
    ArrayList <String> str1=new ArrayList <String>();

    if (request.getSession().getAttribute("str1") != null) {
    str1 = (ArrayList ) request.getSession().getAttribute("str1");
    out.println(str1);
    }

    %>


    </body>
    </html>

    Login.java


    package com;

    import java.io.IOException;
    import java.util.ArrayList;

    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    public class Login extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Login() {
    super();
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    ArrayList <String> str1=new ArrayList <String>();
    HttpSession session = request.getSession(true);
    response.setContentType("text/html");

    try
    {
    str1.add("String1");
    str1.add("String2");

    request.setAttribute("str1", str1);

    String destination="/FirstJSP.jsp"; //If authentication fails///////////
    RequestDispatcher rd = getServletContext().getRequestDispatcher(destinati on);
    rd.forward(request, response);

    }

    catch (Exception e)
    {
    throw new ServletException(e.toString());
    }


    }
    }


  2. #2
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow How to pass the ArrayList form servelet to jsp?

    Hi Friends,
    I m new in servlet side.I want to pass the arraylist from servlet to jsp.how do i do this? below is what i tried.But i always shows null valuses.what is my mistake?

    Here is my code:
    Login.java

    package com;

    import java.io.IOException;
    import java.util.ArrayList;

    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    public class Login extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Login() {
    super();
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    ArrayList <String> str1=new ArrayList <String>();
    HttpSession session = request.getSession(true);
    response.setContentType("text/html");

    try
    {
    str1.add("String1");
    str1.add("S");

    request.setAttribute("str1", str1);

    String destination="/FirstJSP.jsp"; //If authentication fails///////////
    RequestDispatcher rd = getServletContext().getRequestDispatcher(destinati on);
    rd.forward(request, response);

    }

    catch (Exception e)
    {
    throw new ServletException(e.toString());
    }


    }
    }


    FirstJSP.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>`
    <%@ page import="java.util.ArrayList" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org /TR/html4/loose.dtd">`
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>`
    </head>
    <body>


    <%
    ArrayList <String> str1=new ArrayList <String>();

    if (request.getSession().getAttribute("str1") != null) {
    str1 = (ArrayList ) request.getSession().getAttribute("str1");
    out.println("str1");
    }

    %>d


    </body>
    </html>

  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: How to pass the ArrayList form servelet to jsp?

    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  4. #4
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: How to pass the ArrayList form servelet to jsp?

    There's a difference between request attributes and session attributes. Notice when you save your variable you are doing request.setAttribute() but when you get it back you call request.getSession().getAttribute(). You need to pick one or the other. The request object only lasts for this request while the session continues to exists as the user navigates through the website.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

Similar Threads

  1. pass parameters from jsp to another jsp
    By chathura in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: March 24th, 2012, 12:50 PM
  2. How can i pass the valus from a JSP to the action class???
    By sachin.negi04 in forum Web Frameworks
    Replies: 1
    Last Post: March 10th, 2012, 01:35 AM
  3. Arraylist pass/fail values problem/confused.
    By BITmixit in forum Collections and Generics
    Replies: 3
    Last Post: February 25th, 2012, 09:58 AM
  4. Replies: 1
    Last Post: January 17th, 2012, 10:25 AM
  5. Array List; How to pass objects into another arraylist
    By Melvrick in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 24th, 2011, 05:55 AM