HelloServlet for Servlet3.0


/ Published in: Java
Save to your folder(s)

Description: A sample HelloServlet for Servlet3.0 specification. No web.xml file is required in this case.
Jars: servlet-api.jar (spec version: 3.0, Implementation-Vendor: Apache Software Foundation)
URL: http://localhost:8080/TestServlet3/Hello?user=nav
Output: "Hello nav"


Copy this code and paste it in your HTML
  1. package com.nav.sample.servlet3;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5.  
  6. import javax.servlet.ServletConfig;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.annotation.WebServlet;
  9. import javax.servlet.http.HttpServlet;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12.  
  13. /**
  14.  * Servlet implementation class HelloServlet3
  15.  */
  16. @WebServlet("/Hello")
  17. public class HelloServlet3 extends HttpServlet {
  18. private static final long serialVersionUID = 1L;
  19.  
  20. @Override
  21. public void init(ServletConfig config) throws ServletException {
  22. // TODO Auto-generated method stub
  23. super.init(config);
  24. }
  25.  
  26. /**
  27.   * Default constructor.
  28.   */
  29. public HelloServlet3() {
  30. // TODO Auto-generated constructor stub
  31. }
  32.  
  33. /**
  34. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  35. */
  36. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  37. PrintWriter pWriter = response.getWriter();
  38. pWriter.write("Hello "+request.getParameter("user"));
  39. pWriter.flush();
  40. pWriter.close();
  41. }
  42.  
  43. /**
  44. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  45. */
  46. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  47. // TODO Auto-generated method stub
  48. }
  49.  
  50. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.