Kashub's Code Barn - "Servlet z kalkulatorem z laborek"

podświetlone jako qbasic (dodał(a) Kashub @ 2008-08-01 21:47:21)

Twoja wyszukiwarka
Podświetl ten kod w:
Ostatnio dodane:
Losowe wpisy:
/*
 * calc.java
 *
 * Created ON March 17, 2007, 4:42 PM
 */
 
package calc;
 
 
import java.io.*;
import java.net.*;
import java.util.Enumeration;
 
import javax.servlet.*;
import javax.servlet.http.*;
import javax.swing.text.Element;
 
/**
 *
 * @author student
 * @version
 */
public class calc 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();
        OUT.println("<html>");
        OUT.println("<head>");
        OUT.println("<title>Servlet calc</title>");
        OUT.println("</head>");
        OUT.println("<body>");
        OUT.println("<h1>Servlet calc at " + request.getContextPath () + "</h1>");
        OUT.println("1st servlet<br />");
 
 
 
        OUT.println("</body>");
        OUT.println("</html>");
 
        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);
 
         response.setContentType("text/html;charset=UTF-8");
 
        PrintWriter OUT = response.getWriter();
        OUT.println("<html>");
        OUT.println("<head>");
        OUT.println("<title>Servlet calc</title>");
        OUT.println("</head>");
        OUT.println("<body>");
        OUT.println("<h1>Servlet calc at " + request.getContextPath () + "</h1>");
        OUT.println("1st servlet<br />");
 
        RequestDispatcher rd = request.getRequestDispatcher("/banner");
        rd.include(request, response);
 
        OUT.println("<form method=\"post\" action=\"calc\">" +
                "<input type=\"text\" name=\"a\" id=\"a\" />" +
                "+ <input type=\"text\" id=\"b\" name=\"b\" />" +
                "<input type=\"submit\" id=\"wynik\" />" +
                "</form>");
 
           Enumeration paramy = request.getParameterNames();
       WHILE(paramy.hasMoreElements()) {
          STRING ax = (STRING)paramy.nextElement();
          OUT.println(request.getParameter(ax));
       }
 
        OUT.println("</body>");
        OUT.println("</html>");
 
        OUT.CLOSE();
 
 
    }
 
    /** 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);
 
 
        PrintWriter OUT = response.getWriter();
        OUT.println("<html>");
        OUT.println("<head>");
        OUT.println("<title>Servlet calc</title>");
        OUT.println("</head>");
        OUT.println("<body>");
        OUT.println("<h1>Servlet calc at " + request.getContextPath () + "</h1>");
        OUT.println("1st servlet<br />");
 
        STRING a = request.getParameter("a");
        STRING b = request.getParameter("b");
 
        RequestDispatcher rd = request.getRequestDispatcher("/banner");
        rd.include(request, response);
 
        OUT.println("<form method=\"post\" action=\"calc\">" +
                "<input type=\"text\" name=\"a\" id=\"a\" value=\"" + a + "\" />" +
                "+ <input type=\"text\" id=\"b\" name=\"b\" VALUE=\"" + b + "\"/>" +
                "<input type=\"submit\" id=\"wynik\" />" +
                "</form>");
 
        try {
            INT wynik = INTEGER.parseInt(a) + INTEGER.parseInt(b);
            OUT.println("<br />Wynik: " + wynik);
        }
        catch(NumberFormatException e) {
            OUT.println("<h1>chyba cos ci sie pomerdalo. ktoras z podanych przez ciebie wartosci nie jest liczba :P</h1>");
        }
 
       Enumeration paramy = request.getParameterNames();
       WHILE(paramy.hasMoreElements()) {
          STRING ax = (STRING)paramy.nextElement();
          OUT.println(request.getParameter(ax));
       }
 
 
        OUT.println("</body>");
        OUT.println("</html>");
 
        OUT.CLOSE();        
 
 
 
    }
 
    /** Returns a short description of the servlet.
     */
  //  public STRING getServletInfo() {
  //      RETURN "Short description";
  //  }
    // </editor-fold>
}
| Sklepy internetowe | | Foteliki samochodowe | | Opony letnie | | Opony zimowe | | Opony specjalne | | Gdzie przenieść blog za darmo? | | Kody programów | | Wklejacz kodów | | Gnieżdżewo | | Opisy GG |