Java Reference
In-Depth Information
</TR>
</TABLE>
<BR><BR><BR>
<INPUT TYPE="Submit" VALUE = "Submit">
<INPUT TYPE="Reset" VALUE="Clear">
</FORM>
</CENTER>
</BODY>
</HTML>
Since the user may enter a non-numeric value, the servlet must cater for a pos-
sible NumberFormatException . In addition, method getParameter will need to con-
vert the strings it receives into integers by using the parseInt method of the Integer
wrapper class. Now for the codeā€¦
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
@WebServlet("/AdderServlet")
public class AdderServlet extends HttpServlet
{
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException,ServletException
{
try
{
String value1 = request.getParameter("Num1");
String value2 = request.getParameter("Num2");
int num1 = Integer.parseInt(value1);
int num2 = Integer.parseInt(value2);
int sum = num1 + num2;
sendPage(response,"Result = " + sum);
}
catch(NumberFormatException nfEx)
{
sendPage(response,"*** Invalid entry! ***");
}
}
private void sendPage(HttpServletResponse reply,
String result) throws IOException
Search WWH ::




Custom Search