This is an article for explaining on how to do the one mentioned in the title of this article. It is about how to print string in a web page. Basically, the main purpose is for printing certain string or characters in a web page from a servlet. By accessing the URL defining the servlet, there will be a string or characters appear in the web page. Below is the actual step for trying to achieve the purpose :
1. First of all, just create the necessary servlet. In this context, creating the servlet is done by using a Java IDE which is the Netbeans Java IDE. The following is the image for creating the servlet :
- Following the above menu execution, the next window will appear as part of the servlet file create process :
Just fill the above entry with the appropriate value. After filling the entry, just click the Next button to proceed.
- After creating the servlet file, modify the file into the following content :
// <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 { // processRequest(request, response); try { response.getWriter().println("Hello World !"); } catch (IOException e) { // handle your error here } }
- When executing the above project, the following output in the web page will appear. Off course, if the execution of the URL representing the servlet. For an example, if the URL representing the servlet is ‘/hello-world-servlet’, then it is going to be part of the URL accessed :
The very definition of the servlet is available inside the servlet file. The following is the part of the definition :
@WebServlet(urlPatterns = {"/hello-world-servlet"}) public class TestServlet extends HttpServlet {
…
}