Why do we need Java web frameworks like Struts 2?

There are various kinds of Java web frameworks, such as Spring MVC, JavaServer Faces, Struts 2, etc. For a newbie programmer, there is an exponential learning curve.

Why do I need Java web frameworks like Struts 2? This question can be answered by starting from answering how the Servlet API works.

Here is a post which contains code about how to simply program with Servlet API. You would never use this to really program a large project, but it's good to take a look how it looks like originally.

Here is a simple Servlet which process request from client and generate response html.

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class WelcomeServlet extends HttpServlet {
 
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
	}
 
 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//Get the value of form parameter
		String name = request.getParameter("name");
		String welcomeMessage = "Welcome "+name;
 
		// Set the content type(MIME Type) of the response.
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
 
		// Write the HTML to the response
		out.println("<html>");
		out.println("<head>");
		out.println("<title> A very simple servlet example</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<h1>"+welcomeMessage+"</h1>");
		out.println("<a href="/servletexample/pages/form.html">"+"Click here to go back to input page "+"</a>");
		out.println("</body>");
		out.println("</html>");
		out.close();
 
	}
 
 
	public void destroy() {
 
	}
}

This is very simple, real usage won't be easy like this. A real servlet has more work to do as summarized below:

  1. Binding request parameters to Java types.
    String name = request.getParameter("name");
  2. Validating data.
    E.g. There should not be numbers in people's name.
  3. Making calls to business logic.
    E.g. Process the name for some purposes.
  4. Communicate with the data layer.
    E.g. Store user data.
  5. Rendering presentation layer (HTML, and so on).
    E.g. Return results for client browser.

Of course, we can do all of those by ourselves, which is totally possible. However, that would take a lot of time. And very often, those functions are common features which can be implemented in some certain approach. Struts 2 is such an approach. It provides a standard way to implement those common functions following MVC design patterns.

Here is my previous post about a simple Struts2 application.

Reference:
[1] Brown, Donald, Chad Michael Davis, and Scott Stanlick. Struts 2 in action. Dreamtech Press, 2008.

Category >> Struts 2  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>