开发学院WEB开发Jsp JSP中Servlet获取表单数据示例(doGet()和doPost()... 阅读

JSP中Servlet获取表单数据示例(doGet()和doPost()共用接口)

 2009-10-25 16:35:19 来源:WEB开发网   
核心提示:仔细钻研下面两个代码绝对让你受益匪浅 1.jsp页(form表单): <%-- Document : index Created on : 2009-10-9, 10:37:30 Author : lucifer--%><%@page contentType="text/html&q

仔细钻研下面两个代码绝对让你受益匪浅

1.jsp页(form表单):

<%--
   Document  : index
   Created on : 2009-10-9, 10:37:30
   Author   : lucifer
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">

<html>
   <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>JSP Page</title>
   </head>
   <body>
     <form method="post" action="NewServlet">
        用户名:
        <input type="text" name="username"><br>
        密码:
        <input type="passWord" name="password"><br>
        多选:
        <p>
          <input type="checkbox" value="select1" name="check1">多选1
          <input type="checkbox" value="select2" name="check2">多选2
          <input type="checkbox" value="select3" name="check3">多选3
        </p>
        单选:
        <p>
          <input type="radio" name="Radio" value="Radio1" checked>单选1
          <input type="radio" name="Radio" value="Radio2">单选2
          <input type="radio" name="Radio" value="Radio3">单选3
        </p>
        <p>
          下拉选择:
          <select name="ComboBox" size="1">
             <option value="1">下拉选项1</option>
             <option value="2">下拉选项2</option>
             <option value="3">下拉选项3</option>
          </select>
        </p>
        <p>
          列表选择:
          <select name="ListBox" size="3" multiple>
             <option value="1">列表选择1</option>
             <option value="2">列表选择2</option>
             <option value="3">列表选择3</option>
          </select>
        </p>
        <input type="submit" value="Ok">
     </form>
   </body>
</html>


2.Servlet(名字是NewServlet)


/*
   Document  : NewServlet
   Created on : 2009-10-9, 10:37:30
   Author   : lucifer
*/


import java.io.IOException;
import java.io.PRintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

public class NewServlet extends HttpServlet {

   protected void processRequest(HttpServletRequest req, HttpServletResponse res)
   throws ServletException, IOException {
     Vector rows;
     String requestdata = "";
     res.setContentType("text/html;charset=UTF-8");
     PrintWriter out = res.getWriter();
     try {

     out.println("<html>");
       out.println("<head>");
       out.println("<title>Servlet NewServlet</title>");
       out.println("</head>");
       out.println("<body>");
       out.println("<h1>Servlet NewServlet at " + req.getContextPath () + "</h1>");

     rows = getRequestParametersData(req);
       for(int i = 0;i < rows.size();i++){
         Vector cols = (Vector)rows.elementAt(i);
         for(int j = 0;j <cols.size();j++){
            requestdata += cols.elementAt(j);
         }
         requestdata += "<br>";
       }
       out.println(requestdata);
       out.println("</body>");
       out.println("</html>");
     } finally {
       out.close();
     }
   }

   private Vector getRequestParametersData(HttpServletRequest req){
     Vector rows = new Vector();
     for(Enumeration e = req.getParameterNames();e.hasMoreElements();){
        String name = (String)e.nextElement();
        String[] values = req.getParameterValues(name);
        Vector cols = new Vector();
        cols.addElement(name);
        String value = "";
        for(int i = 0;i < values.length;i++){
          value += "{" + values[i] + "}";
        }
        cols.addElement(value);
        rows.addElement(cols);
     }
     return rows;


   }


  

   //注意他们共用一个接口
   @Override
   protected void doGet(HttpServletRequest req, HttpServletResponse res)
   throws ServletException, IOException {
     processRequest(req, res);
   }

   @Override
   protected void doPost(HttpServletRequest req, HttpServletResponse res)
   throws ServletException, IOException {
     processRequest(req, res);
   }

   @Override
   public String getServletInfo() {
     return "~~~";
   }

}

Tags:JSP Servlet 获取

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
更多精彩
    赞助商链接

    热点阅读
      焦点图片
        最新推荐
          精彩阅读