0

I'm developing an application in JAVA where a servlet is taking the inputs from a JSP page. After inserting the values in db it will redirect to another servlet. Then the 2nd servlet will dispatch a JSP page with an ArrayList. But I can't redirect from the 2nd servlet to the JSP page. The ArrayList is going to the JSP page but the page is not showing anything. I'm using NetBeans 6.8. I'll be thankful if anyone can solve this problem.

Code for 1st Servlet:

RequestDispatcher dispatcher = request.getRequestDispatcher("/Servlet1?id="+id);
dispatcher.forward(request, response);

Code for 2nd Servlet:

request.setAttribute("list",list);
String url="test2.jsp";
RequestDispatcher v=request.getRequestDispatcher(""+url+"");
v.forward(request, response);
Sankha
  • 1
  • 4
  • Show some code preferably from your 2nd servlet. – me_digvijay Apr 18 '13 at 07:03
  • Add your code to understand the exact problem. – Rais Alam Apr 18 '13 at 07:05
  • @Sankha: In you question you area saying two different things which looks ambiguus to me: 1.)But I can't redirect from the 2nd servlet to the JSP page. 2.)The ArrayList is going to the JSP page but the page is not showing anything Please make it clear if the browser is going to the second jsp or not. – me_digvijay Apr 18 '13 at 07:16
  • what is the error you are getting? – Shurmajee Apr 18 '13 at 07:17
  • I'm redirecting from 1st servlet to the 2nd servlet with the "id" which is used to fetch some data from DB which is stored in an ArrayList nd the 2nd servlet will redirect to a JSP page with the ArrayList. – Sankha Apr 18 '13 at 07:39
  • I've put a System.out.println("list values::"+list.get(0)); in the final JSP page. It is working and print the value which I can see in the output console in Netbeans. But the page is not displayed. – Sankha Apr 18 '13 at 07:43
  • @MayankSharma I'm not getting any error – Sankha Apr 18 '13 at 07:51
  • put some sys out statements in your code. try to find out till what point the code is executing – Shurmajee Apr 18 '13 at 07:56
  • @MayankSharma the 2nd servlet is working properly when I'm accessing it directly. But when I'm accessing it through another servlet the problem is arises. – Sankha Apr 18 '13 at 08:02

4 Answers4

0

try this on 2nd servlet ..

request.setAttribute("list",list);
String url="test2.jsp";
RequestDispatcher v=request.getRequestDispatcher(url);
v.forward(request, response);

On jsp page ...

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title></title>
</head>
<body>
<c:forEach items="${list}" var="item">
        ${item}<br>
</c:forEach>
</body>
</html>
Mehul Kaklotar
  • 365
  • 1
  • 19
0
public class MySqlConnection {

    Connection c;

    public Connection getConnection() throws ClassNotFoundException, SQLException {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "ignite292";
        String user = "root";
        String password = "root";
        Class.forName(driver); // You don't need to call it EVERYTIME btw. Once during application's startup is more than enough.
        c = (Connection) DriverManager.getConnection(url + dbName, user, password);
        return c;
    }

    public void closeConnection() {
        try {
            if (!c.isClosed()) {
                c.close();
            }
        } catch (Exception e) {
        }
    }
}
Cairnarvon
  • 25,981
  • 9
  • 51
  • 65
0

First things, If you are using any redirect mechanism, It should not be in RequestScope. It must be in Session or Context Scope(Based on your Requirement). So , the resultant code for 2nd Servlet may as follow

request.getSession().setAttribute("list",list);
String url="/test2.jsp";
RequestDispatcher v=request.getRequestDispatcher(""+url+"");
v.forward(request, response);

Try with this Code.

SKC...
  • 223
  • 3
  • 7
  • 15
0

Sankha,

You can add those objects into session object. and you can use that session obj in any jsp and servlet.

suppose you have an arrayList Obj which have some data objects. and you are trying redirecting your servlet to jsp OR servlet to servlet. eg:-

RequestDispatcher dispatcher = request.getRequestDispatcher("/Servlet1?id="+id);
dispatcher.forward(request, response);
request.setAttribute("list",list); // **Insted of using request object use session implicit object**.
String url="test2.jsp";
RequestDispatcher v=request.getRequestDispatcher(""+url+"");
v.forward(request, response);

Please refer below code to sort out your problem.

RequestDispatcher dispatcher = request.getRequestDispatcher("/Servlet1?id="+id);
dispatcher.forward(request, response);
**session.setAttribute("list",list);** 
String url="test2.jsp";
RequestDispatcher v=request.getRequestDispatcher(""+url+"");
v.forward(request, response);

And get this list object by using

List dataList = session.getAttribute("list");

Hope this will help you.