简体   繁体   English

JSP request.getParameter

[英]JSP request.getParameter

I have the following page in JSP: 我在JSP中有以下页面:

    <%String a= request.getParameter("Test"); 
    System.out.println(a);%>

    <form > 
    <input type="text" name="Test" value= "Whatever" >
    </form>

If I start, it shows me on the consule "null" why? 如果我开始,它在领事上向我显示“ null”为什么? Sorry for this very basic question! 很抱歉这个非常基本的问题!

If I start, it shows me on the consule "null" why? 如果我开始,它在领事上向我显示“ null”为什么?

Everyone else has answered, how to do the right thing. 其他人都回答了,如何做正确的事。 Let me tell, why it is doing what it is doing. 让我说说为什么它在做什么。 Since, there is nothing in the request and everything is just plain GET request, so request.getParameter("Test"); 由于请求中没有任何内容,所有内容都只是普通的GET请求,因此request.getParameter("Test"); resolves nothing and returns null in response. 不解决任何问题并返回null

You can probably try to invoke this page as: 您可能可以尝试通过以下方式调用此页面:

http://whatever.com:PORT_IF_ANY/CONTEXT?Test=Whatever

You will then see your page printing Whatever instead of null. 然后,您将看到页面打印“ Whatever而不是null。 So, long story short, since your request doesn't has the parameter named as Test , it evaluates to null and you print that null . 因此,长话短说,由于您的请求没有名为Test的参数,因此它的评估结果为null ,您可以打印出null

You need to have this code distributed in two different JSPs: 您需要将此代码分发到两个不同的JSP中:

First JSP: 第一个JSP:

<form > 
  <input type="text" name="Test" value= "Whatever" >
</form>

Then you need to submit this form from your browser. 然后,您需要通过浏览器提交此表单。 In your servlet doPost handler, you need to dispatch the second JSP which will have the following code: 在servlet doPost处理程序中,您需要调度第二个JSP,它将具有以下代码:

 <%String a= request.getParameter("Test"); 
    System.out.println(a);%>

Update : 更新

As one of the fellow reviewers notate, you can always use the same JSP before and after the form submission. 正如一位审阅者一样,您可以始终在提交表单之前和之后使用相同的JSP。 In this case, the first one will still print null while the second one will print the desired output. 在这种情况下,第一个将打印为空,而第二个将打印所需的输出。 The key is that the form must be submitted in order for the form parameters to be populated to the request context automatically. 关键是必须提交表单,以便表单参数自动填充到请求上下文中。

I think you should do as follow code: 我认为您应该按照以下代码进行操作:

 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"       "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
   <%String a= request.getParameter("Test"); 
    System.out.println(a);%>
    <form action="#"> 
    <input type="text" name="Test" value= "Whatever" >
    <input type="submit" value="submit">
    </form>
</body>
</html>

When you click submit the console will print Whatever。But when you first visit the page, the console is printed as null 单击提交时,控制台将打印“任何内容”。但是,当您第一次访问该页面时,控制台将显示为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM