Yes. They are both used almost always together. In fact JSPs get converted to Servlets at the time of execution
Advantages of JSP 1. HTML friendly simple and easy language and tags. 2. Supports Java Code. 3. Supports standard Web site development tools. Disadvantages of JSP 1. As JSP pages are translated to servlets and compiled, it is difficult to trace errors occurred in JSP pages. 2. JSP pages require double the disk space to hold the JSP page. 3. JSP pages require more time when accessed for the first time as they are to be compiled on the server.
To create an address book using JSP, you can create a form to input contact information like name, email, phone number, etc. When the form is submitted, you can handle the data in a servlet and store it in a database like MySQL using JDBC. Then, you can retrieve and display this information in the JSP using Java servlets.
You can dynamically identify the JSP file in a servlet by using the request URL or request parameters to determine which JSP to forward the request to. You can also store necessary information in session attributes or external configurations to help determine the appropriate JSP to display. Finally, you can use a servlet mapping or URL pattern to route requests to different JSP files based on the URL.
The Servlet ContextA Web application includes many parts. It is more than just one servlet or JSP. Numerous JSPs and one or more Servlets and other supporting java classes together form the web application. To help manage an application, you will sometimes need to set and get information that all of the servlets share together, which we will refer to as context-wide.For Example, if you want a single name using which you can refer to the application, you can set it in the servlet context and have it shared across all instances that use the application.Ex Code:public void init(ServletConfig config) throws ServletException{super.init(config);// Get the ContextServletContext context =config.getServletContext();// Set the attributecontext.setAttribute("appName", "My Test App");}Any time you want, you can refer to this attribute in the context and get its value like below:String appName = context.getAttribute("appName");After the above line of code, the variable appName will have the value "My Test App
Setting and getting session attributes is fairly easy. It is the same in both Servlets and JSPs with one exception. In a JSP, you already have access to the session object, and you do not have to declare it. In a Servlet, you must get the session like this: javax.servlet.http.HttpSession session = request.getSession(); Once you have done that, you can set a session object like this: session.setAttribute("name","value"); To retrieve the value, do this: String foo = (String) session.getAttribute("name"); A couple of things to keep in mind: * The second parameter in the setAttribute method is an Object, not a String. When you retrieve the value, you have to cast it. In the example above, I am casting it to a String. * If you try to perform a getAttribute on a session attribute that does not exist, or was not set, it will return a null. * Session attributes are not available using JavaScript. You can not set or get an attribute in JavaScript. * You do NOT need to do the 'session = request.getSession() in a JSP. It is only necessary in a Servlet.