Linux / PHP / Java / Designer

As melhores dicas estão aqui.

Um simples Servlet

| 1 Comentário

Bom após ter que ficar muito tempo procurando no google um simples exemplo que funciona-se de servlet decidi fazer um próprio:

Este simples servlet irá retornar  a  data do servidor como exemplo:

Data: Sun Apr 05 14:19:36 BRT 2009

  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5.  
  6. import java.io.IOException;
  7. import java.io.PrintWriter;
  8. import java.util.Date;
  9. import javax.servlet.ServletException;
  10. import javax.servlet.http.HttpServlet;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13.  
  14. /**
  15. *
  16. * @author blog script seguro
  17. */
  18. <span id="more-121"></span>public class Aula1 extends HttpServlet {
  19.  
  20. /**
  21. * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
  22. * @param request servlet request
  23. * @param response servlet response
  24. * @throws ServletException if a servlet-specific error occurs
  25. * @throws IOException if an I/O error occurs
  26. */
  27. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  28. throws ServletException, IOException {
  29. Date data = new Date();
  30. response.setContentType("text/html;charset=UTF-8");
  31. PrintWriter out = response.getWriter();
  32. try {
  33. out.println("<html>");
  34. out.println("<head>");
  35. out.println("<title>Servlet Aula1</title>");
  36. out.println("</head>");
  37. out.println("<body>");
  38. out.println("<h1>Data: " + data +"</h1>");
  39. out.println("</body>");
  40. out.println("</html>");
  41. } finally {
  42. out.close();
  43. }
  44. }
  45.  
  46. // <editor-fold defaultstate="collapsed" desc="Métodos HttpServlet. Clique no sinal de + à esquerda para editar o código.">
  47. /**
  48. * Handles the HTTP <code>GET</code> method.
  49. * @param request servlet request
  50. * @param response servlet response
  51. * @throws ServletException if a servlet-specific error occurs
  52. * @throws IOException if an I/O error occurs
  53. */
  54. @Override
  55. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  56. throws ServletException, IOException {
  57. processRequest(request, response);
  58. }
  59.  
  60. /**
  61. * Handles the HTTP <code>POST</code> method.
  62. * @param request servlet request
  63. * @param response servlet response
  64. * @throws ServletException if a servlet-specific error occurs
  65. * @throws IOException if an I/O error occurs
  66. */
  67. @Override
  68. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  69. throws ServletException, IOException {
  70. processRequest(request, response);
  71. }
  72.  
  73. /**
  74. * Returns a short description of the servlet.
  75. * @return a String containing servlet description
  76. */
  77. @Override
  78. public String getServletInfo() {
  79. return "Short description";
  80. }// </editor-fold>
  81.  
  82. }

One Comment

Deixe uma resposta

Campos requeridos estão marcados *.