I have written the following code for servlet in java. It is not compiling despite repeated attempts. and i can't figure out the cause of error. please help me!
It is showing the following errorimport javax.servlet.http.*; import javax.servlet.*; import java.io.*; public class CartServlet extends HttpServlet { public void service(ServletRequest req, ServletResponse res) throws IOException, ServletException { res.setContentType("text/html"); PrintWriter out=res.getWriter(); HttpSession session=req.getSession(false); if(session!=null) { String mobiles[]=(String[])session.getAttribute("mobiles"); String laptops[]=(String[])session.getAttribute("laptops"); String pendrives[]=(String[])session.getAttribute("pendrives"); String watches[]=(String[])session.getAttribute("watches"); String columnCount[]={"mobiles","laptops","pendrives","watches"}; out.println("<html><body>"); out.println("<table bgcolor='pink' border=1> width=400>"); if(mobiles!=null) { out.println("<tr>"); for(int m=0; m<mobiles.length; m++) { out.println("<td>"+mobiles[m]+"</td>"); } out.println("</tr>"); } if(laptops!=null) { out.println("<tr>"); for(int m=0; m<laptops.length; m++) { out.println("<td>"+laptops[m]+"</td>"); } out.println("</tr>"); } if(pendrives!=null) { out.println("<tr>"); for(int m=0; m<pendrives.length; m++) { out.println("<td>"+pendrives[m]+"</td>"); } out.println("</tr>"); } if(watches!=null) { out.println("<tr>"); for(int m=0; m<watches.length; m++) { out.println("<td>"+watches[m]+"</td>"); } out.println("</tr>"); } out.println("</table>"); out.println("</body></html>"); } else { RequestDispatcher rd=req.getRequestDispatcher("/index.html"); rd.forward(req,res); } } }
CartServlet.java:12: error: cannot find symbol
HttpSession session=req.getSession(false);
^
symbol: method getSession(boolean)
location: variable req of type ServletRequest
1 error