Harshad201
Harshad201

Reputation: 53

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);
  }
}

i want to store the value of player in variable a. Is it possible to get the value of parameter

Upvotes: 0

Views: 11041

Answers (2)

miiiii
miiiii

Reputation: 1620

Since you are using doPost() method, you must pass parameter by a http-post request. Or if you want to pass parameters through URL, then you must catch the parameter in doGet() method in your servlet.

http://example.com?username=Miiii&pass=123

In above URL, username is a parameter having value = Miiii and same with pass too. and two parameters are separated by &.

If you want to hide your parameters and their values, you can send them through a form in HTML and select method=POST in form attribute. This form (POST method) parameters will call doPost() directly, so catch them in doPost().

Also in your servlet; the way to get parameter is :

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

Upvotes: 1

NickJ
NickJ

Reputation: 9579

If you are talking about request parameters, e.g.

http://yoururl.com?player=fred

then you can retrieve them using

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

Upvotes: 0

Related Questions