Java HttpServletRequest get parameters

Description

Java HttpServletRequest get parameters

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 Main extends HttpServlet {

   public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {

      res.setContentType("text/html");
      String numA = req.getParameter("numa");
      String numB = req.getParameter("numb");

      int solution = Integer.valueOf(numA) + Integer.valueOf(numB);

      PrintWriter out = res.getWriter();

      /* Display some response to the user */

      out.println("<html><head>");
      out.println("<title>Test Math Servlet</title>");
      out.println("</head>");
      out.println("<body>");

      out.println("<p>Solution: " + numA + " + " + numB + " = " + solution + "</p>");
      out.println("<br/><br/>");
      out.println("<a href='index.html'>Add Two More Numbers</a>");
      out.println("</body></html>");

      out.close();//  w  ww .  j ava  2 s .com
   }
}



PreviousNext

Related