null值使用jsp中的request.getParameter进入servlet

时间:2014-04-30 14:58:09

标签: java jsp servlets

我有一个如下的jsp表单,

<form action="../Registration" enctype="multipart/form-data">
    <label> First Name:</label>
    <input type="text" class="large-field" name="firstname">

    <label> Last Name:</label>
    <input type="text" class="large-field" name="lastname">

    <label> Gender:</label>
    <label class="radio">
    <input type="radio" name="gender"  value="Male">
        Male
    </label>
    <label class="radio">
    <input type="radio" name="gender" value="Female">
        FeMale
    </label>


    <label> Address :</label>
    <input type="text" class="large-field" name="address">


    <label> City:</label>
    <input type="text" class="large-field" name="city">


    <label> College:</label>
    <select class="large-field" name="college">
    <option value=""> --- Please Select --- </option>
    <option value="XYZ">XYZ</option>

    </select>

    <label> Branch:</label>
    <select class="large-field" name="branch">
        <option value=""> --- Please Select --- </option>
        <option value="ABC">ABC</option>
    </select>
    <br />
    <label> Mobile Number:</label>
    <input type="text" class="large-field" name="mobilenumber">
    <br />

    <label> Email_ID:</label>
    <input type="text" class="large-field" name="email">
    <br />
    <label> Password:</label>
    <input type="password" class="large-field"  name="password">
    <br />
    <label> Re-Enter Password:</label>
    <input type="password" class="large-field"  name="repassword">
    <br />
    <label> Profile Picture:</label>
    <input type="file" name="file">
    <br />

    <button class="btn btn-primary">Continue</button>
</form>

然后调用我的servlet,编码如下:

package Client_Controller;

import CommonData.ComData;

import java.io.File;
import java.io.FileInputStream;
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 org.apache.tomcat.util.http.fileupload.FileItemFactory;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;

import java.util.*;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.RequestDispatcher;

import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileUploadException;


public class Registration extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            String f_name = request.getParameter("firstname");
            String l_name = request.getParameter("lastname");
            String gender = request.getParameter("gender");
            String address = request.getParameter("address");
            String city = request.getParameter("city");
            String college = request.getParameter("college");
            String branch = request.getParameter("branch");
            String mobile = request.getParameter("mobilenumber");
            String email = request.getParameter("email");
            String password = request.getParameter("password");
            String filePath = request.getParameter("file").toString();

            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/table", "root", "root");
            s = con.createStatement();
            s.execute("insert into tblmembers(first_name,last_name,sex,address,city,college_name,branch,mobile,email_id,password) " +
                    "values('" + f_name + "','" + l_name + "','" + gender + "','" + address + "','" + city + "','" + college + "','" + branch + "','" + mobile + "'," + email + ",'" + password + "')");

            out.write("Suceess");
        } catch (Exception e) {
            out.write("" + e);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        processRequest(request, response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        processRequest(request, response);
    }


    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

插入数据时,所有值都为空。我尝试过很多东西,但没有工作,所以需要一些帮助。我使用bootstrap作为前端。

4 个答案:

答案 0 :(得分:11)

使用enctype="multipart/form-data"时,无法使用普通request.getParameter检索参数。看起来您正在使用Servlet 2.5或更早版本,因此您需要使用第三方库来解析请求,该第三方库通过了解enctype来处理它。这可以使用Apache Common FileUpload库轻松完成

请注意,如果使用Servlet 3.0或更新版本,则不应出现此问题。

更多信息:

此外,您必须在当前表单中添加method="POST"才能使其正常运行。您无法使用GET请求上传文件。

答案 1 :(得分:1)

不知道为什么,但当我为输入文字提供'name'属性时,它对我有用。

我的旧代码在Servlet中返回null:

<input id="closure" type="text" size="25"><a
                        href="javascript:NewCal('closure','ddmmyyyy')"><img
                            src="drawables/cal.gif" width="16" height="16" border="0"
                            alt="Pick a date"></a>

只需将 name =“closure”为我工作。现在它完美地将此输入文本的值返回到servlet中。

<input id="closure" name="closure" type="text" size="25"><a
                        href="javascript:NewCal('closure','ddmmyyyy')"><img
                            src="drawables/cal.gif" width="16" height="16" border="0"
                            alt="Pick a date"></a>

我在Servlet中获得这个输入文本的价值如下:

String closure = request.getParameter("closure");

答案 2 :(得分:0)

使用下面的一个来访问文件数据

  public static Hashtable getParamsFromMultipartForm(HttpServletRequest req) throws FileUploadException {
        Hashtable ret = new Hashtable();
        List items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(req);
        for (FileItem item : items) {
            if (item.isFormField()) {
                ret.put(item.getFieldName(), item.getString());
            }
        }
        return ret;
    }

然后,每当我需要任何params的值时,只需写下如下

/在servlet的开头

Hashtable multipartParams = TheClassWhereIPutThatMethod.getParamsFromMultipartForm(req);


String myParamFromForm = multipartParams.get("myParamFromForm");

答案 3 :(得分:-1)

更改以下按钮以输入提交,然后重试。我的猜测是你的按钮没有提交表格。

<button type="submit" class="btn btn-primary">Continue</button>