Hi I'm trying to complete this assignment and I don't understand how this PHP thing works. I can't get the value of the check box to populate and I can't get the sales tax to calculate either. He is what I have so far:

<!DOCTYPE html>

<!-- testproblem.html 

     -->

<html lang = "en">

  <head>

<title> Light Bulb Exercise </title>

    <style type = "text/css">
      td, th, table {border: thin solid black;}
    </style>

</head>

  <body>


    <form action = "http://localhost:8888/mikeswork/testproblem.php" method = "post">



      <h2> Light Bulb Convenient Store </h2>


      <table>



<!-- Text widgets for the customer's name and address -->


    <tr>

      <td> Name: </td>

      <td> <input type = "text"  name = "name" 

              size = "30" />
          </td>

    </tr>



      </table>

      <p />


      <table>



<!-- First, the column headings -->


    <tr>

      <th> Product </th>

      <th> Price </th>

      <th> Select One </th>

    </tr>



<!-- Now, the table data entries -->


    <tr>

      <td> Four 25-Watt Light Bulbs</td>

      <td> $2.39 </td>

          <td><label>
            <div align="center">
              <input type="checkbox" name="checkbox1" value="$2.39"/> <br />
            </div>
          </label></td>

    </tr>

    <tr>

      <td>Eight 25-Watt Light Bulbs</td>

      <td> $4,29 </td>

          <td><label>
            <div align="center">
              <input type="checkbox" name="checkbox2" value="$4.29" /> <br />
            </div>
          </label></td>
    </tr>

    <tr>

      <td>Four 25-Watt Long Life Light Bulbs</td>

      <td> $3.95 </td>

      <td><label>
        <div align="center">
          <input type="checkbox" name="checkbox3" value="$3.95" /> <br />
        </div>
      </label></td>

    </tr>

    <tr>

          <td>Eight 25-Watt Long Life Light Bulbs</td>

      <td> $7.49 </td>

          <td><label>
            <div align="center">
              <input type="checkbox" name="checkbox4" value="$7.49" />   <br />
            </div>
          </label></td>

    </tr>

      </table>

      <p />



<!-- The radio buttons for the payment method -->


      <h3> Payment Method</h3>
      <input type = "radio"  name = "payment"  value = "Visa" 

               checked = "checked" /> 

        Visa <br />

        <input type = "radio"  name = "payment"  value = "Mastercard" />

        Master Card <br />

        <input type = "radio"  name = "payment"  value = "Discover" />

        Discover <br />

        <br />





        <!-- The submit and reset buttons -->


        <input type = "submit"  value = "Submit Order" />

        <input type = "reset"  value = "Clear Order Form" />
      </h3>
  </form>

  </body>
</html>


<!DOCTYPE html>
<!-- testproblem.php - Processes the form described in
     testproblem.html
     -->
<html lang = "en">
  <head>
    <title> Process the testproblem.html form </title>
    <meta charset = "utf-8" />
    <style type = "text/css">
      td, th, table {border: thin solid black;}
    </style>
  </head>
  <body>
    <?php

// Get form data values

      $checkbox1 = $_POST["checkbox1"];
      $checkbox2 = $_POST["checkbox2"];
      $checkbox3 = $_POST["checkbox3"];
      $checkbox4 = $_POST["checkbox4"];
      $name = $_POST["name"];
      $payment = $_POST["payment"];


// If any of the quantities are blank, set them to zero

      if ($checkbox1 == "") $checkbox1 = 0;
      if ($checkbox2 == "") $checkbox2 = 0;
      if ($checkbox3 == "") $checkbox3 = 0;
      if ($checkbox4 == "") $checkbox4 = 0;

// Compute the item costs and total cost

      $checkbox1_cost = 2.39 * $checkbox1;
      $checkbox2_cost = 4.29 * $checkbox2;
      $checkbox3_cost = 3.95 * $checkbox3;
      $checkbox4_cost = 7.49 * $checkbox4;
      $total_price = $checkbox1_cost + $checkbox2_cost + $checkbox3_cost + $checkbox4_cost;
      $sales_tax = 1.062 * $total_price;
      $total_items = $checkbox1 + $checkbox2 + $checkbox3 + $checkbox4;



// Return the results to the browser in a table
    ?>
    <h4> Customer: </h4>
    <?php
      print ("$name");
    ?>
    <p /> <p />

    <table>
      <caption> Order Information </caption>
      <tr>
        <th> Product </th>
        <th> Unit Price </th>
        <th> Quantity </th>
        <th> Item Cost </th>
      </tr>

      <tr align = "center">
        <td> Four 25-Watt Light Bulbs </td>
        <td> $2.49 </td>
        <td> <?php print ("$checkbox1"); ?> </td>
        <td> <?php printf ("$ %4.2f", $checkbox1_cost); ?>
        </td>
      </tr>

      <tr align = "center">
        <td> Eight 25-Watt Light Bulbs </td>
        <td> $4.29 </td>
        <td> <?php print ("$checkbox2"); ?> </td>
        <td> <?php printf ("$ %4.2f", $checkbox2_cost); ?>
        </td>
        </tr>

      <tr align = "center">
        <td> Four 25-Watt Long Life Light Bulbs </td>
        <td> $3.95 </td>
        <td> <?php print ("$checkbox3"); ?> </td>
        <td> <?php printf ("$ %4.2f", $checkbox3_cost); ?>
        </td>
      </tr>

      <tr align = "center">
        <td> Eight 25-Watt Long Life Light Bulbs </td>
        <td> $7.49 </td>
        <td> <?php print ("$checkbox4"); ?> </td>
        <td> <?php printf ("$ %4.2f", $checkbox4_cost); ?>
        </td>
      </tr>
    </table>
    <p /> <p />

    <?php
      print ("You ordered $total_items light bulbs items <br />");
      printf ("Your total bill with the 6.2% sales tax is: $ %5.2f <br />", $sales_tax);
      print ("Your chosen method of payment is:" $payment");
    ?>
  </body>
</html>

Recommended Answers

All 12 Replies

Hi,

It is my lazy night, tonight so I am not going to explain my answer any further than what the corrected codes can do. By the way, You will need to reasearch PHP reference.. I wrote a function in the form processor that uses the reference function.

In your form, you need to put name for the submit, and you don't need to add $ sign on the price, otherwise it will be parse as variable.

Another thing, is your usage of printf, you cannot printf <br>, It has to be an isolated event, because it is a function looking for arguments...like..

 print "this string"; printf(arg1,arg2);print"<br/>';

Here is your html file...

        <!DOCTYPE html>
<!-- testproblem.html
-->
<html lang = "en">
<head>
<title> Light Bulb Exercise </title>
<style type = "text/css">
td, th, table {border: thin solid black;}
</style>
</head>
<body>
<form action = "testproblem.php" method = "post">
<h2> Light Bulb Convenient Store </h2>
<table>
<!-- Text widgets for the customer name and address -->
<tr>
<td> Name: </td>
<td> <input type = "text" name = "name"
size = "30" />
</td>
</tr>
</table>
<p />
<table>
<!-- First, the column headings -->
<tr>
<th> Product </th>
<th> Price </th>
<th> Select One </th>
</tr>
<!-- Now, the table data entries -->
<tr>
<td> Four 25-Watt Light Bulbs</td>
<td> $2.39 </td>
<td><label>
<div align="center">
<input type="checkbox" name="checkbox1" value="2.39"/> <br />
</div>
</label></td>
</tr>
<tr>
<td>Eight 25-Watt Light Bulbs</td>
<td> $4,29 </td>
<td><label>
<div align="center">
<input type="checkbox" name="checkbox2" value="4.29" /> <br />
</div>
</label></td>
</tr>
<tr>
<td>Four 25-Watt Long Life Light Bulbs</td>
<td> $3.95 </td>
<td><label>
<div align="center">
<input type="checkbox" name="checkbox3" value="3.95" /> <br />
</div>
</label></td>
</tr>
<tr>
<td>Eight 25-Watt Long Life Light Bulbs</td>
<td> $7.49 </td>
<td><label>
<div align="center">
<input type="checkbox" name="checkbox4" value="7.49" /> <br />
</div>
</label></td>
</tr>
</table>
<p />
<!-- The radio buttons for the payment method -->
<h3> Payment Method</h3>
<input type = "radio" name = "payment" value = "Visa"
checked = "checked" />
Visa <br />
<input type = "radio" name = "payment" value = "Mastercard" />
Master Card <br />
<input type = "radio" name = "payment" value = "Discover" />
Discover <br />
<br />
<!-- The submit and reset buttons -->
<input type = "submit" name="submit" value = "Submit Order" />
<input type = "reset" value = "Clear Order Form" />
</h3>
</form>
</body>
</html>

and the form processor...

<?php
// Get form data values
function form_data(&$form_vars){
 $form_vars = array();
 if(isset($_POST['submit'])){
$form_vars['box1'] = (($_POST["checkbox1"]==="")? 0 : $_POST["checkbox1"]) ;
$form_vars['box2'] = (($_POST["checkbox2"]==="")? 0 : $_POST["checkbox2"]) ;
$form_vars['box3'] = (($_POST["checkbox3"]==="")? 0 : $_POST["checkbox3"]) ;
$form_vars['box4'] = (($_POST["checkbox4"]==="")? 0 : $_POST["checkbox4"]) ;

$form_vars['name'] = $_POST["name"];
$form_vars['payment'] = $_POST["payment"];
// If any of the quantities are blank, set them to zero

// Compute the item costs and total cost
   $form_vars['box1_cost'] = 2.39 * $form_vars['box1'];
    $form_vars['box2_cost'] = 4.29 * $form_vars['box2'];
    $form_vars['box3_cost'] = 3.95 * $form_vars['box3'];
    $form_vars['box4_cost'] = 7.49 * $form_vars['box3'];
    $form_vars['total_cost'] = $form_vars['box1_cost'] + $form_vars['box2_cost'] + $form_vars['box3_cost'] + $form_vars['box4_cost'];
    $form_vars['tax'] = 1.062 * $form_vars['total_cost'];
    $form_vars['total_items'] = $form_vars['box1'] + $form_vars['box2'] + $form_vars['box3'] + $form_vars['box4'];
    // Return the results to the browser in a table
    }
     return $form_vars;
    }
    ?>

 <!DOCTYPE html>
    <!-- testproblem.php - Processes the form described in
    testproblem.html
    -->
    <html lang = "en">
    <head>
    <title> Process the testproblem.html form </title>
    <meta charset = "utf-8" />
    <style type = "text/css">
    td, th, table {border: thin solid black;}
    </style>
    </head>
    <body>

<h4> Customer: </h4>
<?php
$form_vars = form_data($form_vars);
print "Customer Name: ". ($form_vars['name'])."<br/>";
?>
<p /> <p />
<table>
<caption> Order Information </caption>
<tr>
<th> Product </th>
<th> Unit Price </th>
<th> Quantity </th>
<th> Item Cost </th>
</tr>
<tr align = "center">
<td> Four 25-Watt Light Bulbs </td>
<td> $2.49 </td>
<td> <?php echo $form_vars['box1_cost']; ?> </td>
<td> <?php printf ("$ %4.2f", $form_vars['box1_cost']); ?>
</td>
</tr>
<tr align = "center">
<td> Eight 25-Watt Light Bulbs </td>
<td> $4.29 </td>
<td> <?php print ($form_vars['box2_cost']); ?> </td>
<td> <?php printf ("$ %4.2f", $form_vars['box2_cost']); ?>
</td>
</tr>
<tr align = "center">
<td> Four 25-Watt Long Life Light Bulbs </td>
<td> $3.95 </td>
<td> <?php print ($form_vars['box3_cost']); ?> </td>
<td> <?php printf ("$ %4.2f", $form_vars['box3_cost']); ?>
</td>
</tr>
<tr align = "center">
<td> Eight 25-Watt Long Life Light Bulbs </td>
<td> $7.49 </td>
<td> <?php print ($form_vars['box4_cost']); ?> </td>
<td> <?php printf ("$ %4.2f", $form_vars['box4_cost']); ?>
</td>
</tr>
</table>
<p /> <p />
<?php
print ("You ordered ". $form_vars['total_items']." light bulbs items <br />");
 print "Your total bill with the 6.2% sales tax is:"; printf("$ %5.2f", $form_vars['tax']);
 print "<br/>";
print ("Your chosen method of payment is:". $form_vars['payment'] );
?>
</body>
</html>

Please do me a big favor, examine the function that i wrote, and analyze every bit of the codes.. You should be able to answer What?, Why?, When? and Where? If not, ask your teacher :)..

There is one thing I left for you to do.. The printf function will throw an error if the form is submitted empty... I hope not, but if I am right, please kindly fix it. It should be an easy A++ for you on this project.

I just noticed the input values on your form are incorrect.. you need to change the values to quantity of the lightbulbs.

for example

 <td> Four 25-Watt Light Bulbs</td>
    <td> $2.39 </td>
    <td><label>
    <div align="center">
    <input type="checkbox" name="checkbox1" value="2.39"/> <br />
    </div>
    </label></td>

SHOULD be changed to

    <td> Four 25-Watt Light Bulbs</td>
    <td> $2.39 </td>
    <td><label>
    <div align="center">
    <input type="checkbox" name="checkbox1" value="4"/> <br />
    </div>
    </label></td>

Do the remainder on your html page. Do not change the pHP side.

Your script should output something like this, if all boxes are checked.

You ordered 24 light bulbs items
Your total bill with the 6.2% sales tax is:$ Amount here
Your chosen method of payment: card here 

Double check all of your Output a formatted string (printf)... I suspect the calculation could give wrong amount..

Thank you so much for your response!!!

Hey Veedeoo,

I just found out that I have to take this same assignment and write a servlet that makes the program do the same thing as it did when running it as a PHP form. Any guidance on how I can accomplish this???

Hi,

Where do you run your servlets? I am running it on apache tomcat, I write my application in eclipse. May I see your codes?

If you guys are using tomcat, there is an example pre-package with tomcat and it is called RequestParamExample.java.

You can use this and run it on your tomcat.. the form is being processed by this part of the codes..

String firstName = request.getParameter("firstname");
    String lastName = request.getParameter("lastname");
    String 
    out.println(RB.getString("requestparams.params-in-req") + "<br>");
    if (firstName != null || lastName != null) {
        out.println(RB.getString("requestparams.firstname"));
        out.println(" = " + HTMLFilter.filter(firstName) + "<br>");
        out.println(RB.getString("requestparams.lastname"));
        out.println(" = " + HTMLFilter.filter(lastName));
    } else {
        out.println(RB.getString("requestparams.no-params"));
    }

The above codes will print the submitted form as filled by the frontend user..below is a servlet classic form example.. modify it to make it look like the PHP form..

    out.println("<P>");
    out.print("<form action=\"");
    out.print("RequestParamExample\" ");
    out.println("method=POST>");
    out.println(RB.getString("requestparams.firstname"));
    out.println("<input type=text size=20 name=firstname>");
    out.println("<br>");
    out.println(RB.getString("requestparams.lastname"));
    out.println("<input type=text size=20 name=lastname>");
    out.println("<br>");
    out.println("<input type=submit>");
    out.println("</form>");

Notice, there isn't much difference between the PHP and JAVA, except

    (RB.getString("requestparams.InputAttribute"));

You need to add your checkboxes, using the format above, and then on the processing part..

     if (firstName != null || lastName != null)

You must add all items you don't want to be null, for example,

     if (firstName != null || lastName != null || checkbox1 != null) 

I ONLY USE this as an eXample

     || checkbox1 != null

But for your servlet it has to check on name, and then define the quantity, null is the closest thing to not being selected or checked... I think, I might be wrong here though.

    if(checkbox1 = null){
    checkbox1 = 0;
    }

For the rest use else if ..

Do the calculation, and then debug your app..

Veedeoo, I keep getting an HTTP 404 error when I run this. It's suppose torandomly return a different string every time the servlets ran.

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Random;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;





public class checkMate {


    protected void processRequest(HttpServletRequest request, HttpServletResponse
    response)
    throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    String[] myStrings = {"This is the first random message", "This better work when I run it", 
        "Welcome to the Terror Dome", "Dallas Cowboys Rock", "The class is almost over"};
    try {
    /* TODO output your page here. You may use following sample code. */
    out.println("<!DOCTYPE html>");
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Servlet RandomMessage</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Servlet RandomMessage</h1>");
    Random r = new Random( );
    out.println(myStrings[r.nextInt(myStrings.length)]);
    out.println("</body>");
    out.println("</html>");
    } finally {
    out.close();
    }}}



JSP File

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action ="checkMate.java"  method ="get">
            <p>
                Press the button to run the Servlet
                <input type ="submit"  value ="Run Servlet" />
            </p>
        </form>
    </body>
</html>

hi,

Did you manually created the servlet, I mean manually created the directories in the catalina/webapps directory?

If yes, is your web.xml properly setup?

One more thing, you don't have to put the .java extension on your form action, the router will take care of it. This is where the PHP framework got the idea. So, the form can and it is ok to just like this..

<form action ="checkMate"  method ="get">

or

<form action ="checkmate"  method ="get">

and the url of the servlet is accessible in your browser in this convention

http://localhost:8080/TheNameOfYourServlet/NameOfTheClass

So, youre servlet should be accesible in this url

http://localhost:8080/TheNameOfYourServlet/checkMate

For the servlet, if you are running it in tomcat and viewing it on your browser, you need to create your html file inside the servlet directory.. Or if you've created a Web pages directory, this is a great place to save the html files also.

Lastly, is the web.xml. Make sure that all of your web files or pages are included here.. here is an example of a web.xml for servlet...

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>checkMate</servlet-name>
        <servlet-class>checkMate</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>checkMate</servlet-name>
        <url-pattern>/checkMate</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>YourFormPage.html</welcome-file>
    </welcome-file-list>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

Notice how the xml file was constructed, entries are pretty self explanatory. The same concepts we can find in Zend and magento.. the Name of the application class takes the sevlet name

Again, in your case the web.xml file servlet-name will be checkMate. Make sure to define it as it shows on your class declaration. The welcome-file-list is the html file you created which is the html page where the form is located.

The idea is that on form submit, the browser should end up showing the result on the browser, which is given by this.

    out.println("<!DOCTYPE html>");
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Servlet RandomMessage</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Servlet RandomMessage</h1>");
    Random r = new Random( );
    out.println(myStrings[r.nextInt(myStrings.length)]);
    out.println("</body>");
    out.println("</html>");   

By the way, can you tell which IDE you guys are using at school? e.g. netbeans, eclipse, etc..

I'm using netbeans 7.0.1 with the glassfish 3.1 server. And yes I think I manually create a directory. I need to create this web.xml file you speak of because I didn't know this was needed. Thanks for all the help. I must admit this is all a bit o

This the new error I'm getting after I adjust the web.xml file

HTTP Status 500 -

type Exception report

message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: PWC1403: Class checkMate is not a Servlet
root cause

java.lang.ClassCastException: checkMate cannot be cast to javax.servlet.Servlet
note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.1.1 logs.

GlassFish Server Open Source Edition 3.1.1

checkMate.Java File    

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Random;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

public class checkMate {


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException 
    {

    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    String[] myStrings = {"This is the first random message", "A chicken crossed the road", 
        "Its cold outside", "The tree is green", "School is almost over"};
    try {
    /* TODO output your page here. You may use following sample code. */
    out.println("<!DOCTYPE html>");
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Servlet RandomMessage</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Servlet RandomMessage</h1>");
    Random r = new Random( );
    out.println(myStrings[r.nextInt(myStrings.length)]);
    out.println("</body>");
    out.println("</html>");
    } finally {
    out.close();
    }}}

index.jsp file



<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="checkMate" method="get"</form>
            <p>
                Press the button to run the Servlet
                <input type ="submit"  value ="Run Servlet" />
            </p>
        </form>
    </body>
</html>



web.xml File

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>checkMate</display-name>
    <servlet>
        <servlet-name>checkMate</servlet-name>
        <servlet-class>checkMate</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>checkMate</servlet-name>
        <url-pattern>/checkMate</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

Hi,

Here is a simple servlet that will function pretty much the same as what you are trying to achieved.

FileName: **TestServlet.java **

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

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 java.util.Random;
/**
 * Servlet provided without any warranty whatsoever.  For educational purposes only.
 * @author veedeoo
 */
public class TestServlet extends HttpServlet {

/**
 * Processes requests for both HTTP
 * <code>GET</code> and
 * <code>POST</code> 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();
     String[] myStrings = {"This is the first random message", "This better work when I run it",
"Welcome to the Terror Dome", "Dallas Cowboys Rock", "The class is almost over"};

        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet TestServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>");
            Random r = new Random( );
            out.println(myStrings[r.nextInt(myStrings.length)]);
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
 * Handles the HTTP
 * <code>GET</code> 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
 * <code>POST</code> 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";
}// </editor-fold>
}

Filename: index.html this is the html file where the form is located..

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
      <h4>Click the Button below to see my random thoughts, while taking JAVA class</h4>
 <form>
 <input type="button" value="Put Your Text Here" onClick="window.location.href='TestServlet'">      
 </form> 

</body>

</html>

Filename: web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>TestServlet</servlet-name>
        <servlet-class>TestServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>TestServlet</servlet-name>
        <url-pattern>/TestServlet</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

Filename: context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/WebApplication1"/>

File structure as seen on Netbeans project panel

    WebApplication1
        -Web Pages
          -META-INF
              web.xml
        -Source Packages
          -<default packages>
             TestServlet.java
        -Libraries

        -Configuration Files
           MANIFEST.MF
           context.xml
           web.xml

manifest.mf is automatically generated by the netbeans. Just in case you want to manually create it. Codes should look like this

Manifest-Version: 1.0

I tested the codes above based on the directory structures as shown above.

Although I test it in Apache Tomcat and not in glassFish Server, the result be the same in glassFish server.

I just don't want to install glassFish on my laptop, because NetBeans IDE is already sucking the life of it. NetBeans is highly memory demanding vs. Eclipse.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.