Java Servlet HttpServletRequest参数显示为null

时间:2012-01-03 10:02:57

标签: java servlets

我正在尝试从GET请求中获取表单参数。 html代码如下。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>DesignMatch. Matching the best clients and best designers.</title>
        <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
        <script type="text/javascript" src="jquery.backstretch.js"></script>
        <script type="text/javascript" src="design_match.js"></script>
        <link rel="stylesheet" type="text/css" media="all" href="design_match.css" />
    </head>
    <body>
        <div id="matte_bg"></div>
        <div id="entire_wrapper">
            <div class="divModule" id="section_head_1">
                <p class="primaryPageText">
                    Clients
                </p>
                <div class="url-bar">
                    <form name="clientUrls" action="SaveDatabase2" target="_blank" method="get">
                        <input type="text" class="url-box" id="url-box1" name="name1" size="20" value="Paste a link to a website that has the design qualities you want.">
                        <a id="add-url" href="#">Add</a>
                        <a id="del-url" href="#">Remove</a>
                    </form>
                </div>
            </div>
            <div class="divModule" id="section_head_2">
                <p class="primaryPageText">
                    Designers
                </p>
                <div class="portfolio-bar">
                    <form name="designerUrls" action="SaveDatabase2" target="_blank" method="get">
                        <input type="text" class="portfolio-box" id="portfolio-box1" name="name1" size="20" value="Paste a link to a website you designed.">
                        <a id="add-portfolio" href="#">Add</a>
                        <a id="del-portfolio" href="#">Remove</a>
                    </form>
                </div>
            </div>
            <div class="divModule">
                <div class="filterSentence">
                    <ul class="primaryPageText">
                        <li>
                            &nbsp; To me, &nbsp;
                        </li>
                        <li class="budgetTypeFilter">
                            <a href="#" id="budgetToggle">[what price]</a>
                            <div id='budgetFilter' style="display:none" class="budgetTypeMenu">
                                <a href="/500-under">$500 and under</a>
                                <a href="/500-to-1000">$500-$1,000</a>
                                <a href="/1000-to-2500">$1,000-$2,500</a>
                                <a href="/2500-to-5000">$2,500-$5,000</a>
                                <a href="/5000-to-7500">$5,000-$7,500</a>
                                <a href="/7500-to-10000">$7,500-$10,000</a>
                                <a href="/10000-above">Over $10,000</a>
                            </div>
                        </li>
                        <li>
                            &nbsp; seems fair to pay for &nbsp;
                        </li>
                        <li class="budgetTypeFilter">
                            <a href="#" id="typeToggle">[what type of]</a>
                            <div id='typeFilter' style="display:none" class="budgetTypeMenu">
                                <a href="/basic">a basic (e.g., front-end only)</a>
                                <a href="/dynamic">a dynamic (e.g., a little back-end)</a>
                                <a href="/sophisticated">a sophisticated (e.g., lots of back-end)</a>
                            </div>
                        </li>
                        <li>
                            &nbsp; website. &nbsp;
                        </li>
                    </ul>
                </div>
            </div>
            <div class="divModule contact-bar" id="contact">
                <form name="nameForm" action="SaveDatabase2" target="_blank" method="get">
                    <input type="text" id="name_form" name="name_form" size="20" value="Tell us your name.">
                </form>
                <form name="contactForm" action="SaveDatabase2" target="_blank" method="get">
                    <input type="text" id="contact_form" name="contact_form" size="20" value="Enter your e-mail (no spam, ever).">
                </form>
            </div>
            <div id="submit">
                <form accept-charset="UTF-8" action="SaveDatabase2" method="get">
                    <input class="btn primary large" id="submit-button" name="submit" type="submit" value="✔" />
                </form>
            </div>
        </div>
    </body>
</html>

这是Servlet代码。我在doPost中获得的参数值为null。我尝试将所有获取请求更改为在html中发布,但它没有帮助。 doGet函数只是重新路由到doPost。我现在感兴趣的两个主要参数是html末尾的“contact_form”和“name_form”参数。再次感谢!

package web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mysql.jdbc.StringUtils;
import java.sql.*;
import java.io.*;
import java.util.*;

/**
 * Servlet implementation class SaveDatabase2
 */
public class SaveDatabase2 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public SaveDatabase2() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<h1> Thank You </h1>");

        //Get Info for Database     
        int designer=0;
        String clientType="";
        String name= "";
        String email="";
        //Get parameters to put into database
        Enumeration parameterNames = request.getParameterNames();
        if ((request.getParameter("designerUrls")!=null) && !request.getParameter("designerUrls").matches(".*\\w.*") ){
            designer=1;
        }
        if (designer==1){
            clientType="designer";
        }else clientType="client";

        name= request.getParameter("name_form");
        email=request.getParameter("contact_form");

}

2 个答案:

答案 0 :(得分:3)

首先,我认为你应该将doPostdoGet作为两种不同的方法。如果您将表单的属性doPost设置为method,系统会自动触发post

此外,你的表格应该是这样的:

<form name="myForm" accept-charset="UTF-8" action="SaveDatabase2" target="_blank" method="post">
   <input type="text" id="name_form" name="name_form" size="20" value="Tell us your name.">
   <input type="text" id="contact_form" name="contact_form" size="20" value="Enter your e-mail (no spam, ever).">
   <input class="btn primary large" id="submit-button" name="submit" type="submit" value="✔" />
</form>

在您的问题中,提交按钮采用自己的形式,不包含2个输入文本字段:name_formcontact_form。这就是你获得null值的原因。

答案 1 :(得分:0)

我认为你误解了<form>元素。要将数据从页面发送到servlet,您可以使用method="get"method="post" - 两者仍然以一种方式发送数据(客户端到服务器)。差异见When do you use POST and when do you use GET?

<form>可以涵盖许多<input>,但您的代码中缺少的关键部分是提交的<form><input type="submit"/>提交的<input> )不包含您期望的任何<form>元素。它们位于未提交的其他<form>中。只提交包含提交按钮的<form>

您应该在所有字段周围使用一个{{1}}并提交按钮。