Pr.Pg Next Pg

Creating Servlet packages in Netbeans tutorials

  • Below is the simple steps to create a package in Netbeans.

  1. Right click on the Source Packages directory of webapp than click on new and than click on Java Package.

Creating packages in Servlets

  1. In new Java Package dialog window give package name and click Finish button.

Creating pagkage in servlets

  1. After adding the package web directory look like below:

creating packages in servlets

  1. Now lets add servlets inside MyPackage, Right click on MyPackage than click on new than click on Servlet.. as below:

creating package in servlets

  1. Now in a New Servlet dialog window write class name than click on Next > button.

creating package in servlets

  1. Now write a Servlet name than click on finish button

creating packages in servlets

  1. After adding servlet inside MyPackage, the webapp directory structure look like below:

creating packages in servlets

  1. Finally for doing package demo you need a webapp directory as below:

creating packages in servlets

 

 

Example: Servlet class containing user defined method which will be called by another servlet placed in other package.

Sam Sir

//Program name MyServletMesg.java

/* Servlet class containing user defined method which will be called by another servlet placed in other package.*/

 

package MyPackage;

 

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

public class MyServletMesg extends HttpServlet

{

public void Hello(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

PrintWriter out=response.getWriter();

out.println("Hello this is simple package example in servlet");

}

}

 

 

 

 

Example: Servlet program for calling another servlet method from another package.

Sam Sir

//Program name MyServlet.java

//Servlet program for calling another servlet method from another package.

 

package MyServletPack;

 

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import MyPackage.MyServletMesg;

 

public class MyServlet extends HttpServlet

{

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

MyServletMesg msg=new MyServletMesg();

msg.Hello(request, response);

}

}

Output

Package demo in Servlet

Pr.Pg border                                             Next Pg