JSP输入对Servlet为空

ljo96ir5  于 2022-12-07  发布在  其他
关注(0)|答案(7)|浏览(172)

我尝试将参数传递给jsp到servlet。我的代码是:

    • 服务器端:**
String kullanici = (String)request.getParameter("onaylayici");
    • JSP端:**
<input type="text" name ="onaylayici">

当我在本地主机kullanici上运行它时,变量变成了null。有什么解决办法吗?

    • 编辑:**
<form name = "main" method = "POST">
<td class="summary"><b>İsteği Onaylanacak Kişi : 
<input type="text" name ="onaylayici">  <br>
</form>
jv2fixgn

jv2fixgn1#

我想你的意思是你想从一个jsp到一个servlet。如果是这样的话,看看action属性:

<form action='/MyServlet' ...>
  ...
</form>

如果您要从Servlet转到jsp,则可以重用当前请求属性。您可以通过直接在中设置 value 来实现。类似于以下内容:

request.setAttribute("onaylayici", request.getParameter("onaylayici"));

在servlet中。然后,在jsp中:

<input name='onaylayici' type='text' value='${requestScope["onaylayici"]}'/>
wqsoz72f

wqsoz72f2#

只有表单的名称和方法,没有动作,JSP不知道把它的参数发送到哪里,服务器也不能接收参数。例如:

<form action="/servlet/Test" method="post">
  ... 

</form>
epfja78i

epfja78i3#

如果要在Servlet中设置参数值并在JSP中读取它,则需要执行以下操作:
Servlet中:
request.setAttribute("yourParamName", request.getParameter("yourParamValue"));
JSP中:

<input name='yourParamName' type='text' value="<%=request.getAttribute("yourParamName")%>" />
50few1ms

50few1ms4#

我的解决方案是:

<input name='onaylayici' type='text' value='${requestScope["onaylayici"]}'/>
注:谢谢fGo ...

klh5stk1

klh5stk15#

您的代码是正确的,但一个小错误。您还没有设置请求数据到响应页面。所以,您必须设置请求数据到响应如下:-
服务器端更改-

//Get the data from JSP
String myDataInServer = (String)request.getParameter("onaylayici");
//Set the data to response by request.setAttributes
request.setAttributes("onaylayici",myDataInServer);

JSP(客户端)-

String kullanici = (String)request.getParameter("onaylayici");

现在它会起作用的。希望它会对你有帮助。

nwlls2ji

nwlls2ji6#

Don't know why but it worked for me when I provided 'name' attribute to input text.
My Old code which returned null in Servlet:

<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>

Just putting name="closure" worked for me. Now it perfectly returns value of this input text into 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>

And I am getting value of this input text in Servlet as follows:

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

vyu0f0g17#

< input type="text" name ="onaylayici" value="<% if(kullanici!=null){out.print(request.getAttribute(kullanici))} %>">

〈% /希望这对您有所帮助/%〉

相关问题