Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. package package1;
  2.  
  3. /*
  4. * To change this license header, choose License Headers in Project Properties.
  5. * To change this template file, choose Tools | Templates
  6. * and open the template in the editor.
  7. */
  8.  
  9. import java.io.IOException;
  10. import java.io.PrintWriter;
  11. import javax.servlet.ServletException;
  12. import javax.servlet.annotation.WebServlet;
  13. import javax.servlet.http.HttpServlet;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16.  
  17. /**
  18. *
  19. * @author AbdiGureye
  20. */
  21. @WebServlet(urlPatterns = {"/MyKalkulator"})
  22. public class AddisjonsKalkulator extends HttpServlet {
  23.  
  24.  
  25.  
  26. /**
  27. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  28. * methods.
  29. *
  30. * @param request servlet request
  31. * @param response servlet response
  32. * @throws ServletException if a servlet-specific error occurs
  33. * @throws IOException if an I/O error occurs
  34. */
  35. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  36. throws ServletException, IOException {
  37. response.setContentType("text/html;charset=UTF-8");
  38. try (PrintWriter out = response.getWriter()) {
  39. /* TODO output your page here. You may use following sample code. */
  40. out.println("<!DOCTYPE html>");
  41. out.println("<html>");
  42. out.println("<head>");
  43. out.println("<title>Servlet AddisjonsKalkulator</title>");
  44. out.println("</head>");
  45. out.println("<body>");
  46. String tall1 = request.getParameter("tall1");
  47. String tall2 = request.getParameter("tall2");
  48. int t1;
  49. int t2;
  50. t1 = Integer.parseInt(tall1);
  51. t2 = Integer.parseInt(tall2);
  52. int result = t1 + t2;
  53. out.println("Resultat : " + t1 + " + " + t2 +" er lik: " +result);
  54. out.println("</body>");
  55. out.println("</html>");
  56. }
  57. }
  58.  
  59. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  60. /**
  61. * Handles the HTTP <code>GET</code> method.
  62. *
  63. * @param request servlet request
  64. * @param response servlet response
  65. * @throws ServletException if a servlet-specific error occurs
  66. * @throws IOException if an I/O error occurs
  67. */
  68. @Override
  69. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  70. throws ServletException, IOException {
  71. processRequest(request, response);
  72. }
  73.  
  74. /**
  75. * Handles the HTTP <code>POST</code> method.
  76. *
  77. * @param request servlet request
  78. * @param response servlet response
  79. * @throws ServletException if a servlet-specific error occurs
  80. * @throws IOException if an I/O error occurs
  81. */
  82. @Override
  83. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  84. throws ServletException, IOException {
  85. processRequest(request, response);
  86. }
  87.  
  88. /**
  89. * Returns a short description of the servlet.
  90. *
  91. * @return a String containing servlet description
  92. */
  93. @Override
  94. public String getServletInfo() {
  95. return "Short description";
  96. }// </editor-fold>
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement