When i click hlink in index.jsp , showing null..
and so the output shd be helloworld.
i use netbeans 6.9.
initservlet.java:
public class initservlet extends HttpServlet implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
ServletContext con=sce.getServletContext();
try{ System.out.println("Hello world");
con.setAttribute("p",con.getInitParameter("print"));// setting context attribute here...
}catch(Exception e)
{
con.log("Error occured",e);}// throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
// throw new UnsupportedOperationException("Not supported yet.");
}
}
simpleservlet.java:
public class simpleservlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String name="Maximum";
ServletContext sc=getServletContext();
name=(String) sc.getAttribute("p");// getting context attribute here..
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet simpleservlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>"+name+"</h1>");
out.println("</body>");
out.println("</html>");
} catch(Exception e)
{
out.println(e);}
finally {
out.close();
}
}
web.xml:
<context-param>
<param-name>print</param-name>
<param-value>helloworld</param-value>
</context-param>
<servlet>
<servlet-name>initservlet</servlet-name>
<servlet-class>initservlet</servlet-class>
</servlet>
index.jsp:
<body>
<p> <a href="simpleservlet">Execute</a>
</p>
</body>