request.getparameter(), Retrieve Parameters
Example of
request.getparameter(), Retrieve Parameters from HTML Form
. index.html
1
2
3
4
5
6
7
|
<font face="verdana"
size="2px">
<form action="sum"
method="post">
Number1:<input type="text" name="n1"><br>
Number2:<input type="text" name="n2"><br>
<input
type="submit" value="Calculate Sum">
</form>
</font>
|
OngetParameter.java
package java4s;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class OngetParameter extends HttpServlet
{
protected void doPost(HttpServletRequest
req,HttpServletResponse res)throws ServletException,IOException
{
PrintWriter
pw=res.getWriter();
res.setContentType("text/html");
String
n1=req.getParameter("n1");
String
n2=req.getParameter("n2");
int
result=Integer.parseInt(n1)+Integer.parseInt(n2);
pw.println("Sum
of two numbers " +result);
pw.close();
}
}
web.xml
<web-app>
<servlet>
<servlet-name>sumOfTwoNumbers</servlet-name>
<servlet-class>java4s.OngetParameter</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sumOfTwoNumbers</servlet-name>
<url-pattern>/sum</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Comments
Post a Comment