Bỏ qua đến nội dung
Bỏ qua điều hướng
MT MT.Net.VNDigital Platform
Đăng nhập
MT.NET.VN

MT.Net.VN

Tìm kiếm
MT LEARNING · LESSON

Ví dụ đọc tất cả Parameters của Form trong Servlet

Ví dụ đọc tất cả Parameters của Form trong Servlet Trong ví dụ này chúng ta sử dụng phương thức […]

Ví dụ đọc tất cả Parameters của Form trong Servlet




Ví dụ đọc tất cả Parameters của Form trong Servlet

Trong ví dụ này chúng ta sử dụng phương thức getParameterNames() của HttpServletRequest để đọc tất cả các tham số của HTML Form. Phương thức này trả về một Enumeration chứa các tên tham số theo một thứ tự không xác định.

Đối với Enumeration, chúng ta có thể lặp Enumeration theo cách chuẩn bằng cách sử dụng phương thức hasMoreElements() để xác định khi nào dừng lại và sử dụng phương thức nextElement() để lấy từng tên tham số.

File: ReadParamsAction.java trong package vn.viettuts

package vn.viettuts;  import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration;  import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;  //Extend HttpServlet class public class ReadParamsAction extends HttpServlet {     // Method to handle GET method request.     public void doGet(HttpServletRequest request, HttpServletResponse response)             throws ServletException, IOException {          // Set response content type         response.setContentType("text/html");          PrintWriter out = response.getWriter();         String title = "Reading All Form Parameters";         String docType = "<!doctype html public "-//w3c//dtd html 4.0 "                  + "transitional//en">n";          out.println(docType +             "<html>n" +             "<head><title>" + title + "</title></head>n" +             "<body bgcolor = "#f0f0f0">n" +             "<h1 align = "center">" + title + "</h1>n" +             "<table width = "100%" border = "1" align = "center">n" +             "<tr bgcolor = "#949494">n" +                 "<th>Param Name</th>" +                 "<th>Param Value(s)</th>n"+             "</tr>n"         );          // get all parameters of form         Enumeration paramNames = request.getParameterNames();          // read parameters         while (paramNames.hasMoreElements()) {             String paramName = (String) paramNames.nextElement();             out.print("<tr><td>" + paramName + "</td>n<td>");             String[] paramValues = request.getParameterValues(paramName);              // Read single valued data             if (paramValues.length == 1) {                 String paramValue = paramValues[0];                 if (paramValue.length() == 0)                     out.println("<i>No Value</i>");                 else                     out.println(paramValue);             } else {                 // Read multiple valued data                 out.println("<ul>");                  for (int i = 0; i < paramValues.length; i++) {                     out.println("<li>" + paramValues[i]);                 }                 out.println("</ul>");             }        }        out.println("</tr>n</table>n</body></html>");     }          // Method to handle POST method request.     public void doPost(HttpServletRequest request, HttpServletResponse response)             throws ServletException, IOException {         doGet(request, response);     } } 

Cấu hình servlet trong file web.xml

<servlet>     <servlet-name>ReadParamsAction</servlet-name>     <servlet-class>vn.viettuts.ReadParamsAction</servlet-class> </servlet>  <servlet-mapping>     <servlet-name>ReadParamsAction</servlet-name>     <url-pattern>/ReadParamsAction</url-pattern> </servlet-mapping> 

Tạo trang index.html

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body>   <form action="ReadParamsAction" method="POST">     <input type="checkbox" name="toan" checked="checked" /> Toan      <input type="checkbox" name="ly" /> Vat Ly     <input type="checkbox" name="hoa" checked="checked" /> Hoa Hoc      <input type="submit" value="Chon Mon Hoc" />   </form> </body> </html> 

Demo

Click “Chon Mon Hoc”

Ví dụ đọc tất cả Parameters của Form trong Servlet







Source link

Zalo Messenger Gọi điện Hỗ trợ0
Home Dịch vụ Khóa học CPanel