Hi
I m trying to pass the arraylist from Servelet to JSP page Which data is get t from My SQL data base.Plz anyone help me whats wrong in my code part
Login.java
package com; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; 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 { HttpSession session = request.getSession(true); response.setContentType("text/html"); ArrayList<String>items1 = new ArrayList<String>(); ArrayList <String>items2 = new ArrayList<String>(); try{ String url = "jdbc:mysql://localhost:3306/atopfinal"; String driver = "com.mysql.jdbc.Driver"; Class.forName(driver).newInstance(); Connection conn; conn = DriverManager.getConnection(url, "root","root"); Statement s = conn.createStatement (); s.executeQuery ("SELECT * from beml_112_gps"); ResultSet rs = s.getResultSet (); while (rs.next ()) { items1.add(rs.getString ("LAT")); items2.add(rs.getString ("LON")); } rs.close (); s.close (); } catch(Exception ee){ System.out.println(ee.toString()); } String destination = "/FirstJSP.jsp"; request.setAttribute("items1",items1); request.setAttribute("items2",items2); RequestDispatcher rd = getServletContext().getRequestDispatcher(destination); rd.forward(request, response); // request.getRequestDispatcher("FirstJSP.jsp").forward(request, response); } }
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> <form action="Login" method="post"> <input type="submit" value="submit"/><br> <% ArrayList <String> str1=(ArrayList <String>)request.getAttribute("items1"); ArrayList <String> str2=(ArrayList <String>)request.getAttribute("items2"); out.println(str1); out.println(str2); if(request.getAttribute("items1")!=("")) out.println(request.getAttribute("items1")); else out.println(""); if(request.getAttribute("items2")!=("")) out.println(request.getAttribute("items2")); else out.println(""); %> </form> </body> </html>