| Page 3 of 3 
 
   
 
 After pressing Next button, we are in the last step of configuring
our Java Servlet. The last step is intended to map our Java Servlet in
web.xml. Leave the checkbox as checked. It means that we want to apply
the changes to web.xml where all the Java Servlets are registered.
Remember that every Java Servlet that we create, we must register that
particular Java Servlet in web.xml. Web.xml itself is actually a
deployment descriptor. It contains the necessary configurations for our
web application. Web.xml is not only used for Java Servlet but also for
other purposes such as security, parameters and etc.  Back to the Java Servlet’s configuration wizard, there are two
fields available i.e. Servlet Name and URL Mapping. We need to set both
of these variables. Servlet Name is used to associate our actual Java
Servlet (which is Java class) into a name. The URL mapping is used to
identify on how the Java Servlet should be called. This is the URL that
is called in your <form></form> tag in JSP file. If you set
the URL Mapping into /test/GreetingServlet, you need to call it as
/test/GreetingServlet from your <form></form> tag. For our
project, set it into /GreetingServlet. We have completed configuring
our Java Servlet. You may also be interested to see how the web.xml
looks like now. You can go to your Web Pages > WEB-INF > web.xml. Now, if you see, you can see that there is one Java file created which is GreetingServlet.java as shown in below picture. 
 
   
 
 This GreetingServlet.java contains our implementation of the Java
Servlet. If you carefully pay attention to this file, you should
realize that this class extends HttpServlet. So wherever you found Java
classes that extends HttpServlet, those classes must definitely be a
Java Servlet and there must be an entry in web.xml. There are few Java methods created by default in GreetingServlet.java i.e.  
   
  
   | 
protected void processRequest(HttpServletRequest request, HttpServletResponse response)protected void doGet(HttpServletRequest request, HttpServletResponse response)
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
 |  and many more. However, let’s focus on the doGet and doPost methods as these two
methods are the most important. Do you remember that in our
<form></form> tag, we actually specify the method attribute
as POST? For your information, we can also set the method in the
<form></form> tag into GET. If we specify it as POST, the
doPost method will be called. On the other hand, if we specify the
<form> tag as GET, doGet method will be executed. Then, what
would be the difference between GET and POST? Each method has its own advantages and disadvantages. GET has
limited length for the information that is submitted but it is easily
appended on the last URL of your Java Servlet. POST does not have any
limitation of the length of information sent and it is hidden from the
URL. For example, GET will display something like
http://localhost:7001/MyFirstServlet/GreetingServlet?firstName=david&surname=test
while POST would not display firstName and surname in the end of the
URL. POST has URL like
“http://localhost:7001/MyFirstServlet/GreetingServlet”. It is obvious
that GET may have some issues on security as everyone may clearly see
the information sent to the Java Servlet. POST would be a better choice
but sometimes, there would just be a situation where we need to use GET
to make our development easier. I am sure that as you go along, you
will meet a situation where you need to use GET for sending some
information to Servlet. As our JSP used POST, let’s add some Java codes into our doPost
method. Add these two lines into your doPost method so it should be
like below. 
   
  
   | 
    protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
 String firstName = request.getParameter("firstName").toString();
 System.out.println("firstName = " + firstName);
 processRequest(request, response);
 }
 
 String firstName = request.getParameter("firstName").toString();
 |  What does above code means? If you remember, the textbox that we created in our JSP contains the name attribute. 
   
  
   | 
  <input type="text" name="firstName" size="20"> |  This is where the name attribute is useful. We can get the value
entered by the user in the index.jsp by executing
request.getParameter(“firstName”). Remember that firstName is case
sensitive so firstName is different with FirstName or firstname. The
next line of code is used to print out the value into your Tomcat
console.  
   
  
   | 
  System.out.println("firstName = " + firstName); |  You should be able to see the output in your Tomcat console in your
NetBeans. Deploy your project again, refresh your Internet Browser and
see what’s happening now. As your Tomcat has run previously, you just
need to re-deploy the project and simply refresh your browser. 
 
   
 
 After you have pressed the submit button, the page will go blank.
Yes, it is because we have not specified where Java Servlet should
redirect. We will go to this later on. But if you see at the Tomcat
logs, you will notice something interesting.  
 
   
 
   
 
 Now, you see that in the Tomcat console, there is “firstName = John”
line displayed. This is executed and created by our Java Servlet. It
also means that our Java Servlet has successfully been executed. Okay, let’s move to the last step. We need to greet the users. Now, clear the doPost method and add some more codes to processRequest method as below. 
   
  
   | 
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
 response.setContentType("text/html;charset=UTF-8");
 PrintWriter out = response.getWriter();
 String firstName = request.getParameter("firstName").toString();
 String surname = request.getParameter("surname").toString();
 
 out.println("<html>");
 out.println("<head>");
 out.println("<title>Servlet GreetingServlet</title>");
 out.println("</head>");
 out.println("<body>");
 out.println("<h1>Servlet GreetingServlet at " + request.getContextPath () + "</h1>");
 out.println("<p>Welcome " + firstName + " " + surname + "</p>");
 out.println("</body>");
 out.println("</html>");
 
 out.close();
 }
 |  Let’s see on what the codes do. 
   
  
   | 
        String firstName = request.getParameter("firstName").toString();String surname = request.getParameter("surname").toString();
 |  The two lines above are used to get the firstname and the lastname from our JSP.  Then, these two variables are used and displayed into JSP and
located in below codes. The out.println(“”); is used to render the HTML
into JSP page. 
   
  
   | 
        out.println("<html>");out.println("<head>");
 out.println("<title>Servlet GreetingServlet</title>");
 out.println("</head>");
 out.println("<body>");
 out.println("<h1>Servlet GreetingServlet at " + request.getContextPath () + "</h1>");
 out.println("<p>Welcome " + firstName + " " + surname + "</p>");
 out.println("</body>");
 out.println("</html>");
 |  You need to re-deploy your Web Application and refresh your Internet
Browser. Go to index.jsp and provide first name and surname and press
Submit button. Now, you will see a page that greets the user.
Congratulations, you have successfully created your Java Servlet.  Conclusion Well, I am sure that you now have been able to create a Servlet
using NetBeans. By reading this tutorial, you should be able to send
information to Java Servlet, know how to get this information from your
Java Servlet and redirect the user to the success page. However, you
should practice more and more as your skills will be improved along
your experiences. There are still a lot of things in Java world that
you need to know such as Filter, Listener, Struts, EJB and etc. If you
grab the concept, you are good to go to the next level. I wish you all
the best luck.  You can find the NetBeans project for the tutorial source codes here. The README file for the sources is available here.
 
  Related Tips 
 << Start < Prev 1 2 3 Next > End >> 
 | 
Excellent topic included with Netbean 5.0 editor