Servlets

Servlet API - HttpSession class
    HttpSession session = request.getSession();
   session.setAttribute("name","Fred Flintstone");  //   sets a string object to the attribute called "name"
   String name = session.getAttribute("name");
     session.setMaxInactiveInterval(600);   // timeout and destroy the session and its attributes after 10 minutes
JavaBeans and Servlets
beans.Customer customer = new beans.Customer();

      or

import beans.*;
...
Customer customer = new Customer();

session.setAttribute("Customer",customer);   //  add in the servlet that created the bean


Customer customer = (Customer)session.getAttribute("Customer");  
if(customer == null)   // session timed out or bean doesn't exist
{
 // handle it
}



Simple calculator index.jsp

<%--
    Document   : index
    Created on : Oct 27, 2008, 5:34:04 PM
    Author     : Mark Pendergast
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h2>Calculator</h2>
        <form name="calculator" method = "get" action = "Calculator">
        X : <input type = text name = "x" size = 10><br>
        Y : <input type = text name="y" size = 10><br>
        <br>
        <input type = submit name="operation" value = "+">
        <input type = submit name="operation" value = "-">
        <input type = submit name="operation" value = "*">
        <input type = submit name="operation" value = "/">
        <input type = reset>
    
        </form>
    </body>
</html>

simple calculator Calculator.java

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

package servlets;

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 Mark Pendergast
 */
public class Calculator extends HttpServlet {
  
    /**
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Answer</title>"); 
            out.println("</head>");
            out.println("<body>");
          
            String xStr = request.getParameter("x");
            String yStr = request.getParameter("y");
            String operation = request.getParameter("operation");
            double answer = 0;
            double x = Double.parseDouble(xStr);
            double y = Double.parseDouble(yStr);
            if(operation.equals("+"))
                answer = x + y;
            else if(operation.equals("-"))
                answer = x - y;
            else if(operation.equals("*"))
                answer = x * y;
            else if(operation.equals("/"))
                answer = x / y;
           
            out.println("x "+operation+" y = "+answer+"<br>");
            out.println("<input type='button' value='click here to try again' onclick='javascript:history.back()'>");
          
            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
    */
    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
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}