Menambah dan Mengalikan Angka dengan servlet di java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package siregar.romauli.siregar.servlet;
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;
/**
*
* @author toshiba
*/
public class FirstServlet extends HttpServlet {
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response, String str)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String txt = "";
try {
if (str != null) {
txt = str;
}
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet FirstServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet FirstServlet at " + request.getContextPath() + "</h1>");
out.println("<p>");
out.println("<h2>");
out.println("Hello :).... ");
out.println("<p>");
out.println("Hasilnya Adalah " + txt);
out.println("</h2>");
out.println("<form method=\"GET\" action=\"FirstServlet\">");
out.println("<input type=\"text\" name=\"valOne\" value=\"\">");
out.println("OPERATOR");
out.println("<input type=\"text\" name=\"valTwo\" value=\"\">");
out.println("<p>");
out.println("<input type=\"submit\" value=\"ADD\">");
out.println("<p>");
out.println("</form>");
out.println("<form method=\"POST\" action=\"FirstServlet\">");
out.println("<input type=\"text\" name=\"valOne\" value=\"\">");
out.println("OPERATOR");
out.println("<input type=\"text\" name=\"valTwo\" value=\"\">");
out.println("<p>");
out.println("<input type=\"submit\" value=\"MULTIPLY\">");
out.println("</form>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int rslt;
String result = null;
String paramOne = null;
String paramTwo = null;
paramOne = request.getParameter("valOne");
paramTwo = request.getParameter("valTwo");
rslt = Integer.parseInt(paramOne) + Integer.parseInt(paramTwo);
result = String.valueOf(rslt);
processRequest(request, response, result);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int rslt;
String result = null;
String paramOne = null;
String paramTwo = null;
paramOne = request.getParameter("valOne");
paramTwo = request.getParameter("valTwo");
rslt = Integer.parseInt(paramOne) * Integer.parseInt(paramTwo);
result = String.valueOf(rslt);
processRequest(request, response, result);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package siregar.romauli.siregar.servlet;
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;
/**
*
* @author toshiba
*/
public class FirstServlet extends HttpServlet {
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response, String str)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String txt = "";
try {
if (str != null) {
txt = str;
}
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet FirstServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet FirstServlet at " + request.getContextPath() + "</h1>");
out.println("<p>");
out.println("<h2>");
out.println("Hello :).... ");
out.println("<p>");
out.println("Hasilnya Adalah " + txt);
out.println("</h2>");
out.println("<form method=\"GET\" action=\"FirstServlet\">");
out.println("<input type=\"text\" name=\"valOne\" value=\"\">");
out.println("OPERATOR");
out.println("<input type=\"text\" name=\"valTwo\" value=\"\">");
out.println("<p>");
out.println("<input type=\"submit\" value=\"ADD\">");
out.println("<p>");
out.println("</form>");
out.println("<form method=\"POST\" action=\"FirstServlet\">");
out.println("<input type=\"text\" name=\"valOne\" value=\"\">");
out.println("OPERATOR");
out.println("<input type=\"text\" name=\"valTwo\" value=\"\">");
out.println("<p>");
out.println("<input type=\"submit\" value=\"MULTIPLY\">");
out.println("</form>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int rslt;
String result = null;
String paramOne = null;
String paramTwo = null;
paramOne = request.getParameter("valOne");
paramTwo = request.getParameter("valTwo");
rslt = Integer.parseInt(paramOne) + Integer.parseInt(paramTwo);
result = String.valueOf(rslt);
processRequest(request, response, result);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int rslt;
String result = null;
String paramOne = null;
String paramTwo = null;
paramOne = request.getParameter("valOne");
paramTwo = request.getParameter("valTwo");
rslt = Integer.parseInt(paramOne) * Integer.parseInt(paramTwo);
result = String.valueOf(rslt);
processRequest(request, response, result);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Komentar
Posting Komentar