![](/img/trans.png)
[英]How to retrieve the value of an Http Request parameter into a Servlet?
[英]How to get the value of parameter in servlet?
public class Sender extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Test t=new Test();
StringBuilder a=player;
}
public void attackPlayer(StringBuilder player){
System.out.println("code"+player);
}
}
我想将player
的值存储在变量a
。 是否可以获取参数的值
由于您正在使用doPost()方法,因此必须通过http-post请求传递参数。 或者,如果要通过URL传递参数,则必须在servlet的doGet()方法中捕获该参数。
http://example.com?username=Miiii&pass=123
在上述URL中, username
是一个value
= Miiii
且与pass
相同的参数。 两个参数之间用&
分隔。
如果要隐藏参数及其值,则可以通过HTML表单将其发送并在表单属性中选择method=POST
。 此表单(POST方法)参数将直接调用doPost(),因此请在doPost()中捕获它们。
同样在您的servlet中; 获取参数的方式是:
// see the same way in doGet() and doPost().. no change in catching style
String user = request.getParameter("username");
String password = request.getParameter("pass");
NB:
"username" must be exactly same as **username** = Miiii & pass...
如果您在谈论请求参数,例如
http://yoururl.com?player=fred
然后您可以使用
String player = request.getParameter("player");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.