While compiling the error is build failed;
I want to access the listofdvd.txt as getInitparameter using servletcontext and to setAttribute dvdlist in initservlet.
Meanwhile also i need to getattribute the dvdlist in listdvdservlet.java;
Somebody help me in this case..
initservlet.java:
public class initservlet implements ServletContextListener {
ServletContext sc;
public void contextInitialized(ServletContextEvent sce) {
ServletContext context=sce.getServletContext();
List l=new ArrayList();
String list=context.getInitParameter("dvdlist");
InputStream is=null;
BufferedReader reader=null;
try{
is=context.getResourceAsStream(list);
reader=new BufferedReader(new InputStreamReader(is));
String temprec;
while((temprec=reader.readLine())!=null)
{
String field[]=temprec.split("\t");
String tit=field[0];
String yr=field[1];
String gen=field[2];
dvditem lists=new dvditem(tit,yr,gen);
l.add(lists);
}
context.setAttribute("dvdl", l);// i set my list here..
context.log("The league list has been loaded.");
}
catch(Exception e)
{
context.log("Exception occured",e);
}
finally
{
if(is!=null)
{
try{
is.close();
}catch(Exception e)
{}
}
if(reader!=null)
{
try {
reader.close();
} catch (IOException ex) {
Logger.getLogger(initservlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
webxml:
<context-param>
<param-name>dvdlist</param-name>
<param-value>/WEB-INF/data/listofdvd.txt</param-value>
</context-param>
<listener>
<listener-class>view.initservlet</listener-class>
</listener>
listdvdservlet.java:
public class llibservlet extends HttpServlet {
List dvds=new ArrayList();
int a=0;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
ServletContext context=getServletContext();
dvds=(List)context.getAttribute("dvdl"); // i get my list here..
try {
int size=dvds.size();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet llibservlet</title>");
out.println("<h3> There are "+size+" Dvd's</h3>");
out.println("</head>");
out.println("<body>");
out.println("<h4>"+size+"</h4>");
out.println("<p>Make the best</p>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}