尽管事先已填充,但textarea和其他输入仍返回null

时间:2018-12-08 12:09:34

标签: java mysql jsp servlets netbeans

我正在尝试将文本区域中的值插入到数据库中,但是当它重定向到负责插入的JSP文件时,返回的值为null。我没有得到的是,我在注册时也做过同样的事情,并且可以这样做。

这是我的表格:

<form action="fileSelection.jsp" method="post" enctype="multipart/form-data">
            <div class = "container-fluid">
                <div class = "col-md-6 col-md-offset-4" id = "articleSection">
                    <div class = "row" id = "title">
                        <input type ="text" name = "name" rows = "2" cols = "50" placeholder = "Name of your Article..." id = "artText">
                    </div>
                    <div class = "row" id = "topic">
                        <select name = "topic">
                            <option value="social">Social</option>
                            <option value="america">America</option>
                            <option value="australia">Australia</option>
                            <option value="asia">Asia</option>
                            <option value="Europe">Europe</option>
                            <option value="middle eastern">Middle Eastern</option>
                        </select>
                    </div>
                    <div class = "row" id = "content">
                        <textarea name = "content" rows = "6" cols = "60" placeholder = "Enter your article..." id = "artText"></textarea>
                    </div>
                    <div id = "image">
                        <input type="file" name="image" id="fileToUpload">
                    </div>
                    <div class = "col-md-2" id = "submit">
                        <button type="submit" id = "submitBtn" name = "submitBtn">Submit</button>
                    </div>
                </div>
            </div>
    </form>

这是JSP:

<%

    //StringBuffer text = new StringBuffer(request.getParameter("content"));
    String content = request.getParameter("content");
    String topic = request.getParameter("topic");
    String title = request.getParameter("name");
    String file = request.getParameter("image");
    int id = (Integer) session.getAttribute("id");      
    out.println("content: " + content);
    imageUpload user = new imageUpload();
    //user.insert(request, response, title, content, topic, id, file);

    //response.sendRedirect("index.jsp");

%>

1 个答案:

答案 0 :(得分:0)

MVC模式将您的业务逻辑与视图部分分开,因此请使用jsp-servlet mvc模式。    index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>File Upload Example in JSP and Servlet - Java web
    application</title>
</head>
<body>
    <div>
        <h3>Choose File to Upload in Server</h3>
        <form action="HomeController" method="post" enctype="multipart/form-data">
            Name : <input type="text" name="name" id="name"/>
            <br>
            File : <input type="file" name="file" id="file" /> 
            <br>
            <input type="submit" value="upload" />
        </form>
    </div>

</body>
</html>

HomeController.java

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

@WebServlet("/HomeController")
public class HomeController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private final String UPLOAD_DIRECTORY = "C:/tmp";

    public HomeController() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

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

        if (ServletFileUpload.isMultipartContent(request)) {
            try {
                List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
                for (FileItem item : multiparts) {
                    if (!item.isFormField()) {
                        String name = new File(item.getName()).getName();
                        item.write(new File(UPLOAD_DIRECTORY + File.separator + name));
                    } else {
                        if (item.getFieldName().equals("name")) {
                            System.out.println(item.getString());
                        }

                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}

Maven依赖

<dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>