Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
5.2 kB
2
Indexable
Never
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Kuba
 */
@WebServlet(urlPatterns = {"/SerweryAplikacji2"})
public class SerweryAplikacji2 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");

      PrintWriter out = response.getWriter();
      String title = "Using GET Method to Read Form Data";
      String docType =
         "<!doctype html public \"-//w3c//dtd html 4.0 " +
         "transitional//en\">\n";
      
      
      //zadanie1
//      int result = 0;
//      int firstNumber = Integer.parseInt(request.getParameter("first_number"));
//      int secondNumber = Integer.parseInt(request.getParameter("second_number"));
//
//      String selectedEvent  =request.getParameter("event");
//      if (selectedEvent.equals("add")) {
//          result = firstNumber + secondNumber;
//      } else if (selectedEvent.equals("sub")) {
//          result = firstNumber - secondNumber;
//      } else if (selectedEvent.equals("mult")) {
//          result = firstNumber * secondNumber;
//      } else if (selectedEvent.equals("div")){
//          result = firstNumber / secondNumber;
//      }
      
      //zadanie4
      Locale locale = request.getLocale();
      String language = locale.getLanguage();
      String country = locale.getCountry();
      
      
      //zadanie5
//      response.setIntHeader("Refresh", 1);
//      
//      Calendar calendar = new GregorianCalendar();
//      
//      int hour = calendar.get(Calendar.HOUR);
//      int minute = calendar.get(Calendar.MINUTE);
//      int second = calendar.get(Calendar.SECOND);
//      
//      String time = hour+":"+ minute +":"+ second;
//      
      
      
      //zadanie6
      String first_name = request.getParameter("first_name");
      String url = "\"http://localhost:8080/SerweryAplikacji2/SerweryAplikacji2?first_name=" + first_name + "\"";
     // out.println("Witaj " + first_name + "<br /> <a href=" + url + ">Oto Twoja Strona</a>");
      out.println(docType +
         "<html>\n" +
            "<head><title>" + title + "</title></head>\n" +
            "<body bgcolor = \"#f0f0f0\">\n" +
//               "<ul>\n" +
//                  "  <li><b>First number</b>: "
//                  + firstNumber + "\n" +
//                  "  <li><b>Second number</b>: "
//                  + secondNumber + "\n" +
//                  "  <li><b>Result</b>: "
//                  + result + "\n" +
//                  "  <li><b>Se</b>: "
//                  + selectedEvent + "\n" +
//               "</ul>\n" + 
                      
               "<p>Locale = " + locale + "</p>\n" +
               "<p>Language = " + language + "</p>\n" +
               "<p>Country = " + country + "</p>\n" +       
               //"<p>Current Time is: " + time + "</p>\n" +
               "<p>Witaj " + first_name + "</p>\n" +
               "<p> <a href=" + url + ">Zobacz strone</a></p>\n" +

                       
            "</body>"
         "</html>"
      );
    }
    


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

}