/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/Servlet.java to edit this template
*/
package com.mycompany.webdienform;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
/**
*
* @author Kiet
*/
@WebServlet(name = "form", urlPatterns = {"/form"})
public class form 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");
try ( PrintWriter out = response.getWriter()) {
/* 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 form</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet form at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</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);
request.getRequestDispatcher("index.html").forward(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 {
String name = request.getParameter("name");
String dob = request.getParameter("dob");
String gender = request.getParameter("gender");
String email = request.getParameter("email");
String address = request.getParameter("address");
String[] hobbies = request.getParameterValues("hobbies");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Form Result</title>");
out.println("<style>");
out.println("body { background-color: #FFFFFF; color: #FFFFFF; font-family: 'Arial', sans-serif; }");
out.println(".container { width: 80%; margin: 0 auto; padding: 20px; background-color: #0D416C; border-radius: 10px; }");
out.println("table { width: 100%; border-collapse: collapse; }");
out.println("th, td { padding: 10px; text-align: left; border-bottom: 1px solid #FFFFFF; }");
out.println("th { background-color: #f2f2f2; color: #000000; }");
out.println("</style>");
out.println("</head>");
out.println("<body>");
out.println("<div class=\"container\">");
out.println("<h1>Form Result</h1>");
out.println("<table>");
out.println("<tr><th>Name:</th><td>" + name + "</td></tr>");
out.println("<tr><th>Date of Birth:</th><td>" + dob + "</td></tr>");
out.println("<tr><th>Gender:</th><td>" + gender + "</td></tr>");
out.println("<tr><th>Email:</th><td>" + email + "</td></tr>");
out.println("<tr><th>Address:</th><td>" + address + "</td></tr>");
out.println("<tr><th>Hobbies:</th><td>");
int length = hobbies.length; // Assuming 'hobbies' is an array
for (int i = 0; i < length; i++) {
if (i == length - 1) {
out.println(hobbies[i]);
} else {
out.print(hobbies[i] + ", ");
}
}
out.println("</ul>");
out.println("</td></tr>");
out.println("</table>");
out.println("</div>");
out.println("</body>");
out.println("</html>");
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}