Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Servlet not compiling-And i don't even have a hint, why?

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Servlet not compiling-And i don't even have a hint, why?

    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!


    import 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);
    		}
    	}
    }
    It is showing the following error
    CartServlet.java:12: error: cannot find symbol
    HttpSession session=req.getSession(false);
    ^
    symbol: method getSession(boolean)
    location: variable req of type ServletRequest
    1 error


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,310
    My Mood
    Mellow
    Thanks
    253
    Thanked 780 Times in 765 Posts

    Default Re: Servlet not compiling-And i don't even have a hint, why?

    The error message is quite clear and helpful. What don't you understand about what it is telling you? You might want to review the HttpSession API.

Similar Threads

  1. Need Hint for Triangle???
    By Ganeprog in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 17th, 2014, 03:20 AM
  2. send and receive byte array form servlet to another servlet via url
    By diga88 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 11th, 2013, 10:40 PM
  3. [SOLVED] For Loop - need a hint for what to do next
    By tufaylahmed in forum Loops & Control Statements
    Replies: 6
    Last Post: November 19th, 2012, 01:06 AM
  4. Java application Hint
    By Ahmedz in forum Java Theory & Questions
    Replies: 2
    Last Post: August 26th, 2012, 01:46 PM
  5. Homework help, don't understand teach's hint, need some direction
    By crazed8s in forum Java Theory & Questions
    Replies: 2
    Last Post: December 8th, 2010, 04:56 PM