Servlet First Program

Google 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 Advertisements

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

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

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

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user. Cloud Computing Tutorial

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects. Download Java C C++ Projects

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: hitesh.xc@gmail.com or 8076671483 Try this Keyword C++ Programs