How to send parameter to the servlet from html?

If we are sending some parameter from html to servlet then we have to use the POST method(for security).
The html page:
<form action="./" method="GET">
Name:<input type=”text” name=”u”/><br>
Password:<input type=”password” name=”p”/><br>
<input type="submit" value="click me"/>

The above HTML page have two fields. When you will give some data and press submit then the request will go to the servlet.
How to get the parameters in servlet

Public void doPost(HttpServletRequest request,HttpServletResponse response){
String username=request.getParameter(“u”);// “u” is the name given in the HTML
String password=request.getParameter(“p”);}
The method request.getParameter(String str) method is used to get the parameters from the HTML.
If the parameter exists but no value then empty String is returned. If the parameter is not exist then null will be returned.
If there is more than one value is associated with a single parameter(e.g. check box) then you have to call Request.getParameterValues(String str) which returns an array of String.

No comments:

Post a Comment