1.Create new web project in netbeans.
2. Here if you don't get Java Web option on left than follow below steps:
3.In plugins windows search Java Web and EE..
4. After creating Web Project you will have this type of Folder structure...
5. In index.jsp page we will make one simple form to take radius from user...
In folder structure right click on package and select servlet.
Note: Servlet name and name in form action tag should be exactly same.
7.In servlet write below code.
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;
/**
*
* @author Batman
*/
public class Area extends HttpServlet {
/**
* Processes requests for both HTTP
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
double r=Double.parseDouble(request.getParameter("txtrdus"));
double area=(double)(2*Math.PI*r);
out.println("Area of CIrcle is: "+area);
} finally {
out.close();
}
}
//
/**
* Handles the HTTP
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}//
2. Here if you don't get Java Web option on left than follow below steps:
3.In plugins windows search Java Web and EE..
4. After creating Web Project you will have this type of Folder structure...
5. In index.jsp page we will make one simple form to take radius from user...
6. Now in jsp file have a look at the
this means we are linking this form with Area class which we are going to create . Actually this Area is a servlet.
Note: Servlet name and name in form action tag should be exactly same.
7.In servlet write below code.
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;
/**
*
* @author Batman
*/
public class Area extends HttpServlet {
/**
* Processes requests for both HTTP
GET
and POST
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
double r=Double.parseDouble(request.getParameter("txtrdus"));
double area=(double)(2*Math.PI*r);
out.println("Area of CIrcle is: "+area);
} finally {
out.close();
}
}
//
/**
* Handles the HTTP
GET
method.*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
POST
method.*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}//
}
Output: