'Servlet with HTTP' :-

Now, we are creating a Http Servlet by extending HttpServlet class. Right click on the src folder and create a new class file, name the file as ExampleHttpServlet. The file path should look like this: Java Resources/src/default package/ExampleHttpServlet.java


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Creating Http Servlet by Extending HttpServlet class

public class ExampleHttpServlet extends HttpServlet 
{    
private String mymsg;
public void init() throws ServletException

{      
mymsg = "Http Servlet Demo";   
}

public void doGet(HttpServletRequest request, 
HttpServletResponse response) throws ServletException, 
IOException 

{            
// Setting up the content type of web page      
response.setContentType("text/html");

// Writing the message on the web page      
PrintWriter out = response.getWriter(); 

out.println("<h1>" + mymsg + "</h1>");      
out.println("<p>" + "Hello Friends!" + "</p>");   
}
public void destroy() 
{      
// Leaving empty. Use this if you want to perform  
//something at the end of Servlet life cycle.   
}
}

			

Previous Next