Latest News

Showing posts with label netbeans. Show all posts
Showing posts with label netbeans. Show all posts

Tuesday, March 28, 2017

March 28, 2017

Servlet Example using apache server

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...



 


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.


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 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:



Download full example here..
March 28, 2017

Installation of Apache web server for JAVA Web Programming

1. Download apache server from here..
              https://www.apache.org/dyn/closer.cgi

2. Extract the folder and you will get following files in apache folder(in my case my folder name is apache-tomcat-6.0.37)

Extracted files



3. Now copy the Apache folder and paste it in the C drive(it must be your system drive).

     In my case PATH:C:\apache-tomcat-6.0.37.

4. Now in order to access server first of all you need to set path for JDK.
if you don't have JDK installed in your machine first install JDK from here..JDK

Now set path variable for JDK.

* Go to My computer and right click on it and select properties.



*On next screen select Advance System Setting.


*On next screen select Advance tab and choose Environment Variable.


* Then select new variable as follow.


* Then fill the field by following path.


For JDK path go through your C drive and look for the following path
C:\Program Files\Java\jdk1.8.0_101
if your machine jdk is located here than use this path as shown in above figure and click ok.

5. So if you already have JDK setup than after step 4 just move here...
   For apache server your machine should have CATALINA variable.
   To set path for CATALINA see following figure.


6. Now just try to run your JAVA web program using this server it could work.

See sample i created one web app:



WebPage of  Program