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 ");
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");
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