/ Published in: Java
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"
Jars: servlet-api.jar (spec version: 3.0, Implementation-Vendor: Apache Software Foundation)
URL: http://localhost:8080/TestServlet3/Hello?user=nav
Output: "Hello nav"
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
package com.nav.sample.servlet3; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class HelloServlet3 */ @WebServlet("/Hello") public class HelloServlet3 extends HttpServlet { private static final long serialVersionUID = 1L; @Override public void init(ServletConfig config) throws ServletException { // TODO Auto-generated method stub super.init(config); } /** * Default constructor. */ public HelloServlet3() { // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { pWriter.write("Hello "+request.getParameter("user")); pWriter.flush(); pWriter.close(); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }