servlet表单处理使用GET()方法实例 及复选框

1、web目录下的hello.html
html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>菜鸟教程(runoob.comtitle>
head>
<body>
<form action="HelloForm" method="get">
  网址名:<input type="text" name="name">
    <br />
    网址:<input type="text" name="url">
    <input type="submit" value="提交">
form>
body>
html>
2、src下的HelloForm.java
public class HelloForm extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public HelloForm(){
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException,IOException{
        //设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();//准备输入
        String title ="使用get方法读取表单数据";
        //表单输入的数据中有中文则需要转码
        String name = new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8");
        String doType = " \n";
        out.println(doType +
                    "\n" +
                    "" </span>+ title +<span style="color:#008000;font-weight:bold;">"\n" +
                    "\"#f0f0f0\">\n" +
                    "

\"center\"> "+ title + "

\n" + "
    \n" + "
  • 站点名:" + name +"\n"+ "
  • 网址:" + request.getParameter("url")+"\n"+ "
      \n" + ""); } protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ doGet(request,response); } } 2、设置一下web-xml
<servlet>
    <servlet-name>HelloFormservlet-name>
    <servlet-class>HelloFormservlet-class>
servlet>
<servlet-mapping>
    <servlet-name>HelloFormservlet-name>
    <url-pattern>/HelloFormurl-pattern>
servlet-mapping>
 
3、试输入网址名和网址,然后点击"提交"按钮,Gif 演示如下:

读取所有的表单参数

1、ReadParams.java
public class ReadParams extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public ReadParams(){
        super();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException,IOException{
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String title = "读取所有的表单数据";
        String docType =
                "\"-//w3c//dtd html 4.0 " +
                        "transitional//en\">\n";
        out.println(docType +
        "\n" +
        "\"utf-8\">" </span>+title +<span style="color:#008000;font-weight:bold;">"\n"
        +"body bgcolor=\"#f0f0f0\">\n" +
        "

\"center\">" + title +"

"
+ "\"100%\" border=\"1\" align=\"center\">\n" + "\"#949494\">\n" + "\n"+ "\n");
        Enumeration paramNames = request.getParameterNames();
        while(paramNames.hasMoreElements()){//刚开始的时候Enumeration的游标指向-1,所以可以全部遍历
            String paramName = (String)paramNames.nextElement();
            out.println("
\n"); String[] paramValues = request.getParameterValues(paramName); //读取单个值的数据 if(paramValues.length == 1){ String paramValue = paramValues[0]; if(paramValue.length() == 0) out.println(""); else out.println(""); } else{//读取多个数据 out.println(""); } out.println(""); } out.println("\n
参数名称参数值
" + paramName + "没有值" + paramValue +"
    "); for(int i = 0;i < paramValues.length;i++){ out.println("
  • " +paramValues[i]); } out.println("
\n"); } public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ doGet(request,response); } }

2、test.html
html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>菜鸟教程(runoob.comtitle>
head>
<body>
<form action="ReadParams" method="post" target="_blank">
    <input type="checkbox" name="maths" checked="checked"/>数学
    <input type="checkbox" name="physics" />物理
    <input type="checkbox" name="chemistry" checked="checked"/>化学
    <input type="submit" value="选择科学" />
form>
body>
html>
3、web-xml
<servlet>
    <servlet-name>ReadParamsservlet-name>
    <servlet-class>ReadParamsservlet-class>
servlet>
<servlet-mapping>
    <servlet-name>ReadParamsservlet-name>
    <url-pattern>/ReadParamsurl-pattern>
servlet-mapping>

现在使用上面的表单调用 Servlet,将产生以下结果:

将复选框数据传递到 Servlet 程序

当需要选择一个以上的选项时,则使用复选框。

下面是一个 HTML 代码实例 checkbox.html,一个带有两个复选框的表单。




 charset="utf-8">
</span><span class="pln">菜鸟教程(runoob.com)</span><span class="tag">


 action="CheckBox" method="POST" target="_blank">
 type="checkbox" name="runoob" checked="checked" /> 菜鸟教程
 type="checkbox" name="google"  /> Google
 type="checkbox" name="taobao" checked="checked" /> 淘宝
 type="submit" value="选择站点" />



注意:如果doGet方法表单提交的数据中有中文数据则需要转码:

String name =new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8");

你可能感兴趣的:(servlet)