Friday 4 October 2013

How to make a servlet using eclipse ,apache tomcat [ J2EE PART 5 ]


A Servlet is a Java class in Java EE that conforms to the Java Servlet API, a protocol by which a Java class may respond to HTTP

Right click on myfirstservletproject→New→Other


Select Servlet→Next→


Type java package:com.mamstricks.myfirstservlet→Class name:Firstservlet→



→Select /FirstServlet from URL mapping→EDIT→


→Type"path" in addition to "/FirstServlet"→/FirstServletpath
Its just for understand the URL mapping ,its not necessary



Select Option rounded→Finish


Now we get this class
have a look at screenshot and focus on to yellow marking and red rectangle window



Now type this line as shown in picture
  
System.out.println("From doget method ");
        
    
i will tell you more about doget method  in following tutorials, Dont worry :-)
Run the servlet by right clicking myservlet.java→run as server



→Click→yes



Now this message     System.out.println("From doget method ");  printed in log
So how to print "hello" in browser?
Just write this this two line
       

PrintWriter pw=response.getWriter();
pw.println("Hello");
        
    
So what is this "PrintWriter" ,"getWriter" doing?


PrintWriter pw = response.getWriter();
getWriter() simply returns a "PrintWriter" object that can send character text to the client.The PrintWriter uses the character encoding returned by getCharacterEncoding(). If the response's character encoding has not been specified as described in getCharacterEncoding (i.e., the method just returns the default value ISO-8859-1), getWriter updates it to ISO-8859-1.


Now refresh Your page pointed by arrow


We have done !!!!!!!!!!!

   Please Dont forget to suggest & correct my language  :P
if you have any doubt please comment ...



0 Comments:

Post a Comment

Facebook Twitter Delicious Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | coupon codes