Servlet First Program

Advertisements

Prev Tutorial Next Tutorial

Servlet First Program

Servlet programming is very simple but you need some basic knowledge for example interface, abstract class, exception handling.

Steps to write servlet program

  • Define web directory structure
  • Define .jsp/.html pages
  • Define servlet program, compile into classes folder
  • Define web.xml, into WEB-INF folder
  • Deploy the webroot into web server deployment folder location.
  • Start the server
  • Open web browser and invoke servlet/.html/.jsp.

Write a web applicaton to send hello word as response to client using servlet.

FirstServlet.java

import java.io.*;
import javax.servlet.*;
  
public class FirstServlet extends HttpServlet {  
  
  public void service(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException
  {
   // get request parameter
   // business operation
   String resultvalue="<body bgcolor="cyan" text="red"> <h1> hello word</h1></body>";

   // prepare response
   resp.setContentType("text/html");
   printWriter out=resp.getWriter();

   // send response
   out.print(resultvalue);  
   out.close();  
   }  
} 

web.xml

<web-app>
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>FirstServlet</servlet-class>  
</servlet>  

<servlet-mapping>  
<servlet-name>s1</servlet-name>  
<url-pattern>/servlet1</url-pattern>  
</servlet-mapping>  

</web-app>  

Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements