Java server pages (JSP) is a server side technology which is used to create web application.JSP consists of HTML and JSP tags. The JSP pages are easier to maintain than servlet because we can separate designing and development. It provides additional features like expression language, tags, etc. It access to the family java APIs and JDBC API to access enterprise database.
JSP stands for Java Server Pages. JSP is java server side technology to create dynamic web pages. JSP is extension of Servlet technology to help developers create dynamic pages with HTML like syntax. We can create user views in servlet also but the code will become very ugly and error prone. Also most of the elements in web page is static, so JSP page is more suitable for web pages. We should avoid business logic in JSP pages and try to use it only for view purpose. JSP scripting elements can be used for writing java code in JSP pages but it’s best to avoid them and use JSP action elements, JSTL tags or custom tags to achieve the same functionalities. One more benefit of JSP is that most of the containers support hot deployment of JSP pages. Just make the required changes in the JSP page and replace the old page with the updated jsp page in deployment directory and container will load the new JSP page. We don’t need to compile our project code or restart server whereas if we make change in servlet code, we need to build the complete project again and deploy it. Although most of the containers now provide hot deployment support for applications but still it’s more work that JSP pages. JSP lifecycle methods are: The jsp comment is called hide comment whereas html comment is called output comment. If user views the source of the page, the jsp comment will not be shown whereas html comment will be shown. JSP pages provide two types of comments that we can use: HTML Comments: Since JSP pages are like HTML, we can use HTML comments like <– HTML Comment –>. These comments are sent to client also and we can see it in HTML source. So we should avoid any code level comments or debugging comments using HTML comments. JSP Comments: JSP Comments are written using scriptlets like <%– JSP Comment –%>. These comments are present in the generated servlet source code and doesn’t sent to client. For any code level or debugging information comments we should use JSP comments. SP implicit objects are created by container while translating JSP page to Servlet source to help developers. We can use these objects directly in scriptlets that goes in service method, however we can’t use them in JSP Declaration because that code will go at class level. We have 9 implicit objects that we can directly use in JSP page. Seven of them are declared as local variable at the start of _jspService() method whereas two of them are part of _jspService() method argument that we can use. JSP exception implicit object is not available in normal JSP pages and it’s used in JSP error pages only to catch the exception thrown by the JSP pages and provide useful message to the client. JSP pages are mostly used for view purposes and all the business logic should be in the servlet or model classes. We should pass parameters to JSP page through attributes and then use them to create the HTML response in JSP page. Most part of the JSP page contains HTML code and to help web designers to easily understand JSP page and develop them, JSP technology provides action elements, JSP EL, JSP Standard Tag Library and custom tags that we should use rather than scripting elements to bridge the gap between JSP HTML part and JSP java part. There are two ways to perform exception handling, one is by the errorPage element of page directive, and second is by the error-page element of web.xml file. JSP is usually used for presentation in the MVC pattern (Model View Controller ) i.e. it plays the role of the view. The controller deals with calling the model and the business classes which in turn get the data, this data is then presented to the JSP for rendering on to the client. There are 4 values: ServletContext gives the information about the container whereas PageContext gives the information about the Request. The Expression Language(EL) is used in JSP to simplify the accessibility of objects. It provides many objects that can be used directly like param, requestScope, sessionScope, applicationScope, request, session etc. JSP Standard Tag Library is library of predefined tags that ease the development of JSP. Based on the JSTL functions, they are categorized into five types. Sometimes JSP EL, Action Tags and JSTL tags are not enough and we might get tempted to write java code to perform some operations in JSP page. Fortunately JSP is extendable and we can create our own custom tags to perform certain operations. We can create JSP Custom Tags with following components: We can add custom tag library in JSP page using taglib directive and then use it. Yes why not, I have seen some developers getting confused with this. Even though JSP is a server side technology, it’s used to generate client side response and we can add javascript or CSS code like any other HTML page. PrintWriter is the actual object responsible for writing the content in response. JspWriter uses the PrintWriter object behind the scene and provide buffer support. When the buffer is full or flushed, JspWriter uses the PrintWriter object to write the content into response.What is JSP and why do we need it?
What are the life-cycle methods for a Jsp?
What is difference between hide comment and output comment?
What are different types of comments in JSP?
What are JSP implicit objects?
Which implicit object is not available in normal JSP pages?
Why use of scripting elements in JSP is discouraged?
How can we handle the exceptions in JSP?
How is JSP used in the MVC model?
What are the different scope values for the <jsp:useBean> tag?
What is the difference between ServletContext and PageContext?
What is EL in JSP?
What is basic differences between the JSP custom tags and java beans?
What is JSTL?
What are the types of JSTL tags?
What is JSP Custom Tag and what are it’s components?
Can we use JavaScript with JSP Pages?
What is difference between JspWriter and Servlet PrintWriter?
Related posts:
- Hibernate Interview Questions and Answers 2021
- JasperReports Interview Questions and Answers
- Java Multithreading Interview Question and Answers
- Java Threading Interview Questions and Answers
- JAX-WS Interview Questions and Answers
- JSON Interview Questions and Answers
- Spring Interview Questions and Answers
- WebLogic Server Interview Questions and Answers