Java 如何从 JSP 页面检索文本框值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21537827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:14:51  来源:igfitidea点击:

How to retrieve text box value from a JSP page

javajspservletsweb-applications

提问by user3254893

I am creating a web application in Java and am having trouble retrieving a value from a text box. My aim is to ask the user to enter their email address and then use the value entered for the rest of my application. I do this by attempting to pass the value as part of the URL in the jsp file (and retrieve it using request.getParameter()). However, the value that I keep retrieving is null.

我正在用 Java 创建一个 Web 应用程序,但无法从文本框中检索值。我的目标是要求用户输入他们的电子邮件地址,然后将输入的值用于我的应用程序的其余部分。为此,我尝试将值作为 URL 的一部分传递到 jsp 文件中(并使用 request.getParameter() 检索它)。但是,我不断检索的值为空。

Here is my code:

这是我的代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<HTML>
<HEAD>
<TITLE>Display file upload form to the user</TITLE>
</HEAD>

        <center>
                <label for="email">Enter email address</label>
                <input id="email" name="email">

        </center>

<% String mail = request.getParameter("email");
                        System.out.println(mail);%>
<BODY>
    <FORM ENCTYPE="multipart/form-data" ACTION="upload.jsp?e=<%=mail%>" METHOD=POST>
        <br> <br> <br>

        <center>
            <table border="0" bgcolor=#ccFDDEE>
                <tr>
                    <center>
                        <td colspan="2" align="center"><B>UPLOAD THE FILE</B>
                            <center></td> 
                </tr>
                <tr>
                    <td colspan="2" align="center"></td>
                </tr>
                <tr>
                    <td><b>Choose the WebEx File To Upload and Convert:</b></td>
                    <td><INPUT NAME="file" TYPE="file"></td>
                </tr>
                <tr>
                    <td colspan="2" align="center"></td>
                </tr>
                <tr>
                    <td colspan="2" align="center"><input type="submit"
                        value="Upload and Convert Recording"></td>
                </tr>
                <table>
                    </center>
                    </FORM>
</BODY>
</HTML>

I am trying to pass the mail value into the jsp file so that I can use it in my application

我正在尝试将邮件值传递到 jsp 文件中,以便我可以在我的应用程序中使用它

回答by mo sean

you are requesting for upload.jsp?e=<%=mail%>where the parameter name is eand an other side you are getting the request.getParameter("email");try this request.getParameter("e");or use this instead upload.jsp?email=<%=mail%>for request.getParameter("email");

你所要求的,upload.jsp?e=<%=mail%>其中参数的名字是e和您购买的另一边的request.getParameter("email");尝试这个request.getParameter("e");或用这个代替upload.jsp?email=<%=mail%>request.getParameter("email");

回答by sev7nx

you can try :

你可以试试 :

<form ....  ACTION="upload.jsp?e=${mail}" method="POST">

And send parameters :

并发送参数:

request.setAttribute("mail", "[email protected]");

回答by Mitul Maheshwari

Put the following code between form tag.

将以下代码放在表单标签之间。

<form ENCTYPE="multipart/form-data" ACTION="upload.jsp" METHOD=POST>
    <center>
            <label for="email">Enter email address</label>
            <input id="email" name="email">
    </center>
   -------
   -------
 </form>

on Server side write

在服务器端写入

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

this will definitely solved your problem.

这肯定会解决您的问题。

回答by Mahesh Modukuru

As per my understanding you are sending both parameters and file to the server by specifying multipart/form-data as form encryption type. As per my knowledge in this mode, normal parameters except files are not available directly on server side. If you can use third party libraries in your application, you can use Apche Commons and Commons IO libraries. You can also use the example specified here commons file uploadingto overcome your problem.

根据我的理解,您通过将 multipart/form-data 指定为表单加密类型来将参数和文件发送到服务器。据我所知,在这种模式下,除文件外的正常参数在服务器端不直接可用。如果您可以在应用程序中使用第三方库,则可以使用 Apche Commons 和 Commons IO 库。您还可以使用此处指定的公共文件上传示例来解决您的问题。

You can visit multipart/form-data structurefor more details.

您可以访问multipart/form-data structure了解更多详细信息。