Servlets/JSP are just two ways to process data in your web application and return a response to the user. Servlets are java with HTML embedded in them. JSP are HTML pages with java code inside them. JSP are more flexible. You can map both to certain URLS in your web.xml file. So when a request comes in for a certain URL the web container will try to send the request to the correct Servlet/JSP. If its a servlet it can be initialized if it has not already been, and then the same Servlet can process many requests. A JSP has to be translated into bit code and then compiled into a java class before it can be run, then it too will be initialized. Each JSP instance is unique so there is no threading issues.
- JSP is a webpage scripting language that can generate dynamic content while Servlets are Java programs that are already compiled which also creates dynamic web content
- Servlets run faster compared to JSP
- JSP can be compiled into Java Servlets
- It’s easier to code in JSP than in Java Servlets
- In MVC, jsp act as a view and servlet act as a controller.
- JSP are generally preferred when there is not much processing of data required. But servlets are best for use when there is more processing and manipulation involved.
- The advantage of JSP programming over servlets is that we can build custom tags which can directly call Java beans. There is no such facility in servlets.
- We can achieve functionality of JSP at client side by running JavaScript at client side. There are no such methods for servlets.
- You do not have member variables in servlets because every request goes to the same servlet object.
Struts 2 is an opensource framework which makes building web applications easier, based on java Servlets and JSP technologies. We need struts because Servlets do not output HTML easily and JSP are useful only for small applications. Struts uses the model 2 MVC architecture, (link here to description).
When using struts2 you do not have to worry about retrieving input parameters from the request object. This is because of the value stack. In struts2 you have action classes instead of servlets. When a new request comes in, it goes to the web.xml file(or deployment descriptor) and will look for the servlet mapping, this is where you can add a struts2 filter. The filter will direct to the struts.xml file which contains the url mappings for the action classes.
When it finds the action class you are looking for it will add a new instance of that action class to the value stack. The input parameters are then mapped to the member variables in the objects on value stack. By default it will map to the first matching member variable it finds. Then you can access your business service and perfrom the required processing. Store the new values in the action class or DTO. You will then return a string code, this code will then be mapped to a jsp inside the struts.xml. Inside the jsp you can request values using the member variable name, this will then be looked up in the value stack.
Overview of Struts and web apps
Spring
Hibernate
This post is a work in progress