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

podświetlone jako java (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>
}
| Katalog Sklepów internetowych | | Kamery IP sklep | | Sklep z artykułami dla zwierząt | | karma dla psa - sklep | | Blog o książkach | | Blogi za darmo | | Przenieś bloga z onetu | | Opisy na Facebooka | | Pionowe opisy |